Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkbox group reacts to change in initial vlue #78

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions thaw/src/checkbox/checkbox_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ pub fn CheckboxItem(
let checked = checkbox_group
.0
.with_untracked(|checkbox_group| checkbox_group.contains(&key));

let checked = create_rw_signal(checked);
let initial = create_rw_signal(false);
let item_key = store_value(key);

create_effect(move |_| {
initial.set(checkbox_group.0.get().contains(&item_key.get_value()));
});

_ = checked.watch(move |checked| {
checkbox_group.0.update(move |checkbox_group| {
if *checked {
Expand All @@ -32,9 +38,8 @@ pub fn CheckboxItem(
} else {
item_key.get_value()
};

view! {
<Checkbox class value=checked>
<Checkbox class value=checked initial>
{label}
</Checkbox>
}
Expand Down
16 changes: 13 additions & 3 deletions thaw/src/checkbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use leptos::*;
#[component]
pub fn Checkbox(
#[prop(optional, into)] value: RwSignal<bool>,
#[prop(optional, into)] initial: RwSignal<bool>,
#[prop(optional, into)] class: MaybeSignal<String>,
children: Children,
) -> impl IntoView {
Expand All @@ -33,19 +34,28 @@ pub fn Checkbox(
css_vars
});

let is_checked = Signal::derive(move || value.get() || initial.get());

view! {
<div
class=class_list![
"thaw-checkbox", ("thaw-checkbox--checked", move || value.get()), move || class
"thaw-checkbox", ("thaw-checkbox--checked", move || value.get() || initial.get()), move || class
.get()
]

style=move || css_vars.get()
on:click=move |_| value.set(!value.get_untracked())
on:click=move |_| {
if initial.get() {
value.set(false);
} else {
value.set(!value.get_untracked())
}
initial.set(false);
}
>
<input class="thaw-checkbox__input" type="checkbox"/>
<div class="thaw-checkbox__dot">
<If cond=value>
<If cond=is_checked>
<Then slot>
<Icon icon=Icon::from(AiIcon::AiCheckOutlined) style="color: white"/>
</Then>
Expand Down
Loading