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

Support binding to a radio input value #658

Open
vi opened this issue Feb 8, 2024 · 2 comments
Open

Support binding to a radio input value #658

vi opened this issue Feb 8, 2024 · 2 comments
Labels
C-enhancement Category: new feature or improvement to existing feature D-easy Difficulty: easy S-actionable Actionable

Comments

@vi
Copy link

vi commented Feb 8, 2024

Supposing I have an enum to be chosen by user using radio boxes. What is the best way to do with with Sycamore?

enum SomeChoice {
   Qqq,
   Www,
   Eee,
}

let thesignal = create_signal(SomeChoice::Qqq);

view! {
    label { "Qqq" input(type="radio" , name="somechoice", value="qqq", bind:checked=???)  }
    label { "Www" input(type="radio",  name="somechoice", value="www")  } 
    label { "Eee" input(type="radio",  name="somechoice", value="eee") }
}
@vi
Copy link
Author

vi commented Feb 8, 2024

Workaround I use now:

        let somechoice_qqq = create_signal(false);
        let somechoice_www = create_signal(false);
        let somechoice_eee = create_signal(false);

        let the_choice = create_signal(SomeChoice::Qqq);

        create_effect(move || { if somechoice_qqq.get() { the_choice.set(SomeChoice::Qqq)} });
        create_effect(move || { if somechoice_www.get() { the_choice.set(SomeChoice::Www)} });
        create_effect(move || { if somechoice_eee.get() { the_choice.set(SomeChoice::Eee)} });

and using usual bind:checked=somechoice_qqq binders. It seems to work, but only in one direction.

Also for some reason bool signals for individual variants (like somechoice_qqq) only get set to true, never back to false.

@lukechu10
Copy link
Collaborator

lukechu10 commented Feb 8, 2024

Also for some reason bool signals for individual variants (like somechoice_qqq) only get set to true, never back to

This sounds like a bug. Will need to investigate this. This might be caused by the event that we listen to for the checked being different when the input is a radio, although I'll have to do a bit more research to be sure.

Unfortunately we don't really have a good way to do this yet. What we want eventually is probably something like Svelte's bind:group. (In fact, the current bind: syntax is inspired by Svelte.)

For now, I would suggest that a more idiomatic and simpler workaround would be to use on:click event handlers to directly set the thesignal to the appropriate value. So something like:

view! {
    label { "Qqq" input(type="radio", name="somechoice", value="qqq", on:click=move |_| thesignal.set(SomeChoice::Qqq)) }
    label { "Www" input(type="radio", name="somechoice", value="www", on:click=move |_| thesignal.set(SomeChoice::Www)) } 
    label { "Eee" input(type="radio", name="somechoice", value="eee", on:click=move |_| thesignal.set(SomeChoice::Eee)) }
}

@lukechu10 lukechu10 changed the title What is the idiomatic way to bind radio to enum? Support binding to a radio input value Feb 8, 2024
@lukechu10 lukechu10 added C-enhancement Category: new feature or improvement to existing feature D-easy Difficulty: easy S-actionable Actionable labels Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: new feature or improvement to existing feature D-easy Difficulty: easy S-actionable Actionable
Projects
None yet
Development

No branches or pull requests

2 participants