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

Select element last element cant be set programatically #2530

Closed
2 tasks
voidpumpkin opened this issue Mar 19, 2022 · 5 comments · Fixed by #2819
Closed
2 tasks

Select element last element cant be set programatically #2530

voidpumpkin opened this issue Mar 19, 2022 · 5 comments · Fixed by #2819
Labels
A-yew Area: The main yew crate bug
Milestone

Comments

@voidpumpkin
Copy link
Member

Problem

use yew::prelude::*;

#[function_component]
fn App() -> Html {
    let selection_handle = use_state(|| 2_usize);

    html! {
        <div>
            <select >
                <option value=1 selected={*selection_handle == 1}>{ "Option 1" }</option>
                <option value=2 selected={*selection_handle == 2}>{ "Option 2" }</option>
                <option value=3 selected={*selection_handle == 3}>{ "Option 3" }</option>
                <option value=4 selected={*selection_handle == 4}>{ "Option 4" }</option>
            </select>

            <button onclick={ {let selection_handle = selection_handle.clone();  Callback::from(move |_| selection_handle.set(1))} }>{"Set to 1"}</button>
            <button onclick={ {let selection_handle = selection_handle.clone();  Callback::from(move |_| selection_handle.set(2))} }>{"Set to 2"}</button>
            <button onclick={ {let selection_handle = selection_handle.clone();  Callback::from(move |_| selection_handle.set(3))} }>{"Set to 3"}</button>
            <button onclick={ {let selection_handle = selection_handle.clone();  Callback::from(move |_| selection_handle.set(4))} }>{"Set to 4"}</button>
        </div>
    }
}

fn main() {
    yew::start_app::<App>();
}

click "Set to 4"
observe how select shows "Option 1" not 4

Environment:

  • Yew version: [0.19, master]

Questionnaire

  • I'm interested in fixing this myself but don't know where to start
  • I would like to fix and I have a solution
  • [ x] I don't have time to fix this right now, but maybe later
@voidpumpkin
Copy link
Member Author

On MDN it says selected is only used for first load.

Its possible we need to introduce custom yew attribute for select so we could pass value
I believe react solved this exactly that way, with introduction of value and defaultValue on select tag

@voidpumpkin voidpumpkin changed the title Select element last element can be set programatically Select element last element cant be set programatically Mar 19, 2022
@WorldSEnder
Copy link
Member

Related? #1322

@voidpumpkin
Copy link
Member Author

voidpumpkin commented Mar 19, 2022

Related? #1322

i dont think so

@voidpumpkin
Copy link
Member Author

For now this bug can be worked around:

use web_sys::HtmlSelectElement;
use yew::prelude::*;

#[function_component]
fn App() -> Html {
    let selection_handle = use_state(|| 2_usize);

    let select_node_ref = use_node_ref();

    {
        let select_node_ref = select_node_ref.clone();
        use_effect_with_deps(
            move |selection| {
                if let Some(element) = select_node_ref.cast::<HtmlSelectElement>() {
                    element.set_value(&(selection.to_string()));
                }
                || {}
            },
            *selection_handle,
        )
    };

    html! {
        <div>
            <select ref={select_node_ref}>
                <option value=1>{ "Option 1" }</option>
                <option value=2>{ "Option 2" }</option>
                <option value=3>{ "Option 3" }</option>
                <option value=4>{ "Option 4" }</option>
            </select>

            <button onclick={ {let selection_handle = selection_handle.clone();  Callback::from(move |_| selection_handle.set(1))} }>{"Set to 1"}</button>
            <button onclick={ {let selection_handle = selection_handle.clone();  Callback::from(move |_| selection_handle.set(2))} }>{"Set to 2"}</button>
            <button onclick={ {let selection_handle = selection_handle.clone();  Callback::from(move |_| selection_handle.set(3))} }>{"Set to 3"}</button>
            <button onclick={ {let selection_handle = selection_handle.clone();  Callback::from(move |_| selection_handle.set(4))} }>{"Set to 4"}</button>
        </div>
    }
}

fn main() {
    yew::start_app::<App>();
}

@ranile
Copy link
Member

ranile commented Mar 19, 2022

Related? #1322

i dont whink so

Going by your fix of calling set_value, I'd say it is related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew Area: The main yew crate bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants