|
1 | | -use leptos::{ev::MouseEvent, prelude::*}; |
2 | | - |
3 | | -pub struct UseLabelProps { |
4 | | - on_mouse_down: Option<Callback<MouseEvent>>, |
5 | | -} |
6 | | - |
7 | | -pub struct UseLabelAttrs { |
8 | | - on_mouse_down: Callback<MouseEvent>, |
9 | | -} |
10 | | - |
11 | | -pub fn use_label(props: UseLabelProps) -> UseLabelAttrs { |
12 | | - UseLabelAttrs { |
13 | | - on_mouse_down: Callback::new(move |event: MouseEvent| { |
14 | | - // Only prevent text selection if clicking inside the label itself. |
15 | | - let target = event_target::<web_sys::Element>(&event); |
16 | | - if target |
17 | | - .closest("button, input, select, textarea") |
18 | | - .expect("Element should be able to query closest.") |
19 | | - .is_some() |
20 | | - { |
21 | | - return; |
22 | | - } |
23 | | - |
24 | | - if let Some(on_mouse_down) = props.on_mouse_down { |
25 | | - on_mouse_down.run(event.clone()); |
26 | | - } |
27 | | - |
28 | | - // Prevent text selection when double clicking label. |
29 | | - if !event.default_prevented() && event.detail() > 1 { |
30 | | - event.prevent_default(); |
31 | | - } |
32 | | - }), |
33 | | - } |
34 | | -} |
| 1 | +use leptos::{ev::MouseEvent, html, prelude::*}; |
| 2 | +use leptos_maybe_callback::MaybeCallback; |
| 3 | +use leptos_node_ref::AnyNodeRef; |
| 4 | +use radix_leptos_primitive::Primitive; |
35 | 5 |
|
36 | 6 | #[component] |
37 | 7 | pub fn Label( |
38 | | - #[prop(into, optional)] on_mouse_down: Option<Callback<MouseEvent>>, |
39 | | - #[prop(optional)] children: Option<Children>, |
| 8 | + #[prop(into, optional)] on_mouse_down: MaybeCallback<MouseEvent>, |
| 9 | + #[prop(into, optional)] as_child: MaybeProp<bool>, |
| 10 | + #[prop(into, optional)] node_ref: AnyNodeRef, |
| 11 | + children: TypedChildrenFn<impl IntoView + 'static>, |
40 | 12 | ) -> impl IntoView { |
41 | | - let UseLabelAttrs { on_mouse_down } = use_label(UseLabelProps { on_mouse_down }); |
42 | | - |
43 | 13 | view! { |
44 | | - <label on:mousedown=move |event| on_mouse_down.run(event)> |
45 | | - {children.map(|children| children())} |
46 | | - </label> |
47 | | - } |
48 | | -} |
| 14 | + <Primitive |
| 15 | + element=html::label |
| 16 | + as_child=as_child |
| 17 | + node_ref=node_ref |
| 18 | + children=children |
| 19 | + on:mousedown=move |event: MouseEvent| { |
| 20 | + // Only prevent text selection if clicking inside the label itself. |
| 21 | + let target = event_target::<web_sys::Element>(&event); |
| 22 | + if target |
| 23 | + .closest("button, input, select, textarea") |
| 24 | + .expect("Element should be able to query closest.") |
| 25 | + .is_some() |
| 26 | + { |
| 27 | + return; |
| 28 | + } |
49 | 29 |
|
50 | | -#[component] |
51 | | -pub fn LabelAsChild<R, RV>( |
52 | | - #[prop(into, optional)] on_mouse_down: Option<Callback<MouseEvent>>, |
53 | | - render: R, |
54 | | -) -> impl IntoView |
55 | | -where |
56 | | - R: Fn(UseLabelAttrs) -> RV, |
57 | | - RV: IntoView, |
58 | | -{ |
59 | | - let attrs = use_label(UseLabelProps { on_mouse_down }); |
| 30 | + on_mouse_down.run(event.clone()); |
60 | 31 |
|
61 | | - render(attrs) |
| 32 | + // Prevent text selection when double clicking label. |
| 33 | + if !event.default_prevented() && event.detail() > 1 { |
| 34 | + event.prevent_default(); |
| 35 | + } |
| 36 | + } |
| 37 | + /> |
| 38 | + } |
62 | 39 | } |
0 commit comments