Skip to content

Commit

Permalink
Update Label to Leptos 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreygarrett committed Jan 8, 2025
1 parent 0b05c84 commit b0598d9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 43 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ yew = "0.21.0"
yew-router = "0.18.0"
yew-struct-component = "0.1.4"
yew-style = "0.1.4"
radix-leptos-primitive = "0.0.2"

# Subcrate packages (paths remain the same; you can comment out any subcrate not yet upgraded).
# We centralize shared dependencies in [workspace.dependencies] for a single source of truth,
Expand Down Expand Up @@ -124,4 +123,3 @@ radix-leptos-primitive.path = "./packages/primitives/leptos/primitive"
yew = { git = "https://github.com/RustForWeb/yew.git", branch = "feature/use-composed-ref" }
yew-router = { git = "https://github.com/RustForWeb/yew.git", branch = "feature/use-composed-ref" }
leptos-node-ref = { git = "https://github.com/geoffreygarrett/leptos-utils", branch = "feature/any-node-ref" }
radix-leptos-primitive = { git = "https://github.com/geoffreygarrett/radix", branch = "update/leptos-0.7-primitive" }
1 change: 1 addition & 0 deletions packages/primitives/leptos/label/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ version.workspace = true
[dependencies]
leptos.workspace = true
leptos-node-ref.workspace = true
leptos-maybe-callback.workspace = true
radix-leptos-primitive.workspace = true
web-sys.workspace = true
9 changes: 5 additions & 4 deletions packages/primitives/leptos/label/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use leptos::{ev::MouseEvent, html, prelude::*};
use radix_leptos_primitive::Primitive;
use leptos_maybe_callback::MaybeCallback;
use leptos_node_ref::prelude::*;

/* -------------------------------------------------------------------------------------------------
Expand All @@ -14,7 +15,7 @@ const NAME: &str = "Label";
pub fn Label(
children: TypedChildrenFn<impl IntoView + 'static>,
#[prop(into, optional)] as_child: MaybeProp<bool>,
#[prop(into, optional)] on_mouse_down: Option<Callback<MouseEvent>>,
#[prop(into, optional)] on_mouse_down: MaybeCallback<MouseEvent>,
#[prop(into, optional)] node_ref: AnyNodeRef,
) -> impl IntoView {
view! {
Expand All @@ -33,9 +34,9 @@ pub fn Label(
return;
}

if let Some(on_mouse_down) = on_mouse_down {
on_mouse_down.run( event.clone());
}
// run callback if provided
on_mouse_down.run(event.clone());

// prevent text selection when double-clicking label
if !event.default_prevented() && event.detail() > 1 {
event.prevent_default();
Expand Down
10 changes: 6 additions & 4 deletions stories/leptos/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ pub fn App() -> impl IntoView {
Label

<ul class="list-none m-0 ms-4 p-0">
<li><NavLink href="/label/styled">Styled</NavLink></li>
<li><NavLink href="/label/basic">Basic</NavLink></li>
<li><NavLink href="/label/with-control">With Control</NavLink></li>
<li><NavLink href="/label/with-input-number">With Input Number</NavLink></li>
<li><NavLink href="/label/with-inputs">With Inputs</NavLink></li>
<li><NavLink href="/label/with-events">With Events</NavLink></li>
</ul>
</li>
// <li>
Expand Down Expand Up @@ -239,9 +240,10 @@ pub fn App() -> impl IntoView {
// <Route path="/focus-scope/basic" view=focus_scope::Basic />
// <Route path="/focus-scope/multiple" view=focus_scope::Multiple />

<Route path=path!("/label/styled") view=label::Styled />
<Route path=path!("/label/basic") view=label::Basic />
<Route path=path!("/label/with-control") view=label::WithControl />
<Route path=path!("/label/with-input-number") view=label::WithInputNumber />
<Route path=path!("/label/with-inputs") view=label::WithInputs />
<Route path=path!("/label/with-events") view=label::WithEvents />

// <Route path="/menu/styled" view=menu::Styled />

Expand Down
86 changes: 53 additions & 33 deletions stories/leptos/src/primitives/label.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,79 @@
use leptos::ev;
use leptos::prelude::*;
use radix_leptos_label::*;
use tailwind_fuse::*;

/// Styled example with a basic label
#[component]
pub fn Styled() -> impl IntoView {
let root_class = Memo::new(move |_| RootClass::default().to_class());

pub fn Basic() -> impl IntoView {
view! {
<Label attr:class=root_class>Label</Label>
<Label attr:class="inline-block align-middle cursor-default border border-solid border-gray-200 p-2">
"Basic Label"
</Label>
}
}

/// Shows both wrapping and referencing controls
#[component]
pub fn WithControl() -> impl IntoView {
let control_class = Memo::new(move |_| ControlClass::default().to_class());
view! {
<div class="space-y-4">
<h2>"Wrapping control"</h2>
<Label attr:class="inline-flex gap-2">
<Control /> " Label with wrapped control"
</Label>

<h2>"Referencing control"</h2>
<Control attr:id="control" />
<Label attr:r#for="control" attr:class="ml-2">"Label referencing control"</Label>
</div>
}
}

/// Demonstrates using Label with various form inputs
#[component]
pub fn WithInputs() -> impl IntoView {
view! {
<h1>Wrapping control</h1>
<Label>
<Control attr:class=control_class /> Label
</Label>
<div class="space-y-4">
<Label attr:class="flex items-center gap-2">
<span>"Name:"</span>
<input r#type="text" class="border p-1" />
</Label>

<h1>Referencing control</h1>
<Control attr:id="control" attr:class=control_class />
<Label attr:r#for="control">Label</Label>
<Label attr:class="flex items-center gap-2">
<span>"Quantity:"</span>
<input r#type="number" class="border p-1" />
</Label>
</div>
}
}

/// Shows label with custom event handling
#[component]
pub fn WithInputNumber() -> impl IntoView {
pub fn WithEvents() -> impl IntoView {
let (count, set_count) = signal(0);

view! {
<Label>
<span>Name:</span>
<input type="number" />
</Label>
<div class="space-y-4">
<Label
attr:class="inline-block p-2 border"
on_mouse_down=move |_: ev::MouseEvent| set_count.update(|mut c| *c = *c + 1)
>
<span>"Click counter: "</span>
<span>{count}</span>
</Label>
</div>
}
}

/// Helper component for the control examples
#[component]
fn Control() -> impl IntoView {
view! {
<button on:click=move |_| window().alert_with_message("clicked").expect("Alert should be successful.")>
Control
<button
class="inline-flex border border-solid border-gray-200 p-2 hover:bg-red-100"
on:click=move |_| window().alert_with_message("clicked").expect("Alert should work")
>
"Control"
</button>
}
}

#[derive(TwClass, Default, Clone, Copy)]
#[tw(
class = "inline-block align-middle cursor-default border-[1px] border-solid border-[gainsboro] p-[10px]"
)]
struct RootClass {}

#[derive(TwClass, Default, Clone, Copy)]
#[tw(
class = "inline-flex border-[1px] border-solid border-[gainsboro] p-[10px] align-middle m-[0px_10px] hover:bg-[red]"
)]
struct ControlClass {}
}

0 comments on commit b0598d9

Please sign in to comment.