fix: custom events on components (#1648)

This commit is contained in:
Nya 2023-09-04 19:27:33 +02:00 committed by GitHub
parent 2ca1c51fdc
commit 495862e9f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 877 additions and 11 deletions

View file

@ -522,17 +522,7 @@ pub(crate) fn event_from_attribute_node(
let handler = attribute_value(attr);
#[allow(unused_variables)]
let (name, name_undelegated) = parse_event(&event_name);
let event_type = TYPED_EVENTS
.binary_search(&name)
.map(|_| (name))
.unwrap_or(CUSTOM_EVENT);
let Ok(event_type) = event_type.parse::<TokenStream>() else {
abort!(attr.key, "couldn't parse event name");
};
let (event_type, _, name_undelegated) = parse_event_name(&event_name);
let event_type = if force_undelegated || name_undelegated {
quote! { ::leptos::leptos_dom::ev::undelegated(::leptos::leptos_dom::ev::#event_type) }

View file

@ -0,0 +1,19 @@
---
source: leptos_macro/src/view/tests.rs
assertion_line: 101
expression: pretty(result)
---
fn view() {
::leptos::component_view(
&ExternalComponent,
::leptos::component_props_builder(&ExternalComponent).build(),
)
.into_view()
.on(
::leptos::leptos_dom::ev::undelegated(
::leptos::leptos_dom::ev::Custom::new("custom.event.clear"),
),
move |_: Event| set_value(0),
)
}

View file

@ -0,0 +1,272 @@
---
source: leptos_macro/src/view/tests.rs
assertion_line: 101
expression: result
---
TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: component_view,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: '&',
spacing: Alone,
},
Ident {
sym: ExternalComponent,
span: bytes(11..28),
},
Punct {
char: ',',
spacing: Alone,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: component_props_builder,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: '&',
spacing: Alone,
},
Ident {
sym: ExternalComponent,
span: bytes(11..28),
},
],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: build,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
},
],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: into_view,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: on,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos_dom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: ev,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: undelegated,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos_dom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: ev,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: Custom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: new,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Literal {
lit: "custom.event.clear",
},
],
},
],
},
Punct {
char: ',',
spacing: Alone,
},
Ident {
sym: move,
span: bytes(51..55),
},
Punct {
char: '|',
spacing: Alone,
span: bytes(56..57),
},
Ident {
sym: _,
span: bytes(57..58),
},
Punct {
char: ':',
spacing: Alone,
span: bytes(58..59),
},
Ident {
sym: Event,
span: bytes(60..65),
},
Punct {
char: '|',
spacing: Alone,
span: bytes(65..66),
},
Ident {
sym: set_value,
span: bytes(67..76),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Literal {
lit: 0,
span: bytes(77..78),
},
],
span: bytes(76..79),
},
],
},
]

View file

@ -0,0 +1,19 @@
---
source: leptos_macro/src/view/tests.rs
assertion_line: 101
expression: pretty(result)
---
fn view() {
::leptos::component_view(
&ExternalComponent,
::leptos::component_props_builder(&ExternalComponent).build(),
)
.into_view()
.on(
::leptos::leptos_dom::ev::undelegated(
::leptos::leptos_dom::ev::Custom::new("custom.event.clear"),
),
move |_: Event| set_value(0),
)
}

View file

@ -0,0 +1,272 @@
---
source: leptos_macro/src/view/tests.rs
assertion_line: 101
expression: result
---
TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: component_view,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: '&',
spacing: Alone,
},
Ident {
sym: ExternalComponent,
span: bytes(11..28),
},
Punct {
char: ',',
spacing: Alone,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: component_props_builder,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: '&',
spacing: Alone,
},
Ident {
sym: ExternalComponent,
span: bytes(11..28),
},
],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: build,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
},
],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: into_view,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: on,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos_dom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: ev,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: undelegated,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos_dom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: ev,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: Custom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: new,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Literal {
lit: "custom.event.clear",
},
],
},
],
},
Punct {
char: ',',
spacing: Alone,
},
Ident {
sym: move,
span: bytes(51..55),
},
Punct {
char: '|',
spacing: Alone,
span: bytes(56..57),
},
Ident {
sym: _,
span: bytes(57..58),
},
Punct {
char: ':',
spacing: Alone,
span: bytes(58..59),
},
Ident {
sym: Event,
span: bytes(60..65),
},
Punct {
char: '|',
spacing: Alone,
span: bytes(65..66),
},
Ident {
sym: set_value,
span: bytes(67..76),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Literal {
lit: 0,
span: bytes(77..78),
},
],
span: bytes(76..79),
},
],
},
]

View file

@ -0,0 +1,19 @@
---
source: leptos_macro/src/view/tests.rs
assertion_line: 101
expression: pretty(result)
---
fn view() {
::leptos::component_view(
&ExternalComponent,
::leptos::component_props_builder(&ExternalComponent).build(),
)
.into_view()
.on(
::leptos::leptos_dom::ev::undelegated(
::leptos::leptos_dom::ev::Custom::new("custom.event.clear"),
),
move |_: Event| set_value(0),
)
}

View file

@ -0,0 +1,272 @@
---
source: leptos_macro/src/view/tests.rs
assertion_line: 101
expression: result
---
TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: component_view,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: '&',
spacing: Alone,
},
Ident {
sym: ExternalComponent,
span: bytes(11..28),
},
Punct {
char: ',',
spacing: Alone,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: component_props_builder,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: '&',
spacing: Alone,
},
Ident {
sym: ExternalComponent,
span: bytes(11..28),
},
],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: build,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
},
],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: into_view,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
},
Punct {
char: '.',
spacing: Alone,
},
Ident {
sym: on,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos_dom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: ev,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: undelegated,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: leptos_dom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: ev,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: Custom,
},
Punct {
char: ':',
spacing: Joint,
},
Punct {
char: ':',
spacing: Alone,
},
Ident {
sym: new,
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Literal {
lit: "custom.event.clear",
},
],
},
],
},
Punct {
char: ',',
spacing: Alone,
},
Ident {
sym: move,
span: bytes(51..55),
},
Punct {
char: '|',
spacing: Alone,
span: bytes(56..57),
},
Ident {
sym: _,
span: bytes(57..58),
},
Punct {
char: ':',
spacing: Alone,
span: bytes(58..59),
},
Ident {
sym: Event,
span: bytes(60..65),
},
Punct {
char: '|',
spacing: Alone,
span: bytes(65..66),
},
Ident {
sym: set_value,
span: bytes(67..76),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Literal {
lit: 0,
span: bytes(77..78),
},
],
span: bytes(76..79),
},
],
},
]

View file

@ -112,5 +112,8 @@ for_all_modes! {
initial_value=0
step=1
/>
"#,
test_custom_event => r#"
<ExternalComponent on:custom.event.clear=move |_: Event| set_value(0) />
"#
}