maciejhirsz / kobold

Easy declarative web interfaces.
https://docs.rs/kobold/
Mozilla Public License 2.0
388 stars 7 forks source link

Unable to render using nested if else statement #51

Open ltfschoen opened 1 year ago

ltfschoen commented 1 year ago

I can get it to render if i do a single level of if, else if, and else statements

Screen Shot 2023-04-03 at 12 29 38 pm

But if i use the code that has been commented out instead, which seems to be the same code, but so i have just nested if else statements, then it gives the following error. is the idea that i should proceed trying to figure out how to use Box::new if i want to use nested if else statements?

Screen Shot 2023-04-03 at 12 32 26 pm
maciejhirsz commented 1 year ago

Yeah, that's stretching the limits of what auto_branch can do atm, the control flow analysis is pretty dumb because I don't want to pull in syn and kill compile times. It's fixable, but for now you might have to disable auto_branch and wrap the view! invocations in appropriate BranchN enum.

ltfschoen commented 1 year ago

Yeah, that's stretching the limits of what auto_branch can do atm, the control flow analysis is pretty dumb because I don't want to pull in syn and kill compile times. It's fixable, but for now you might have to disable auto_branch and wrap the view! invocations in appropriate BranchN enum.

Thanks, I got it to work with Branch3

...
use kobold::branching::Branch3;
...
#[component]
fn Cell(col: usize, row: usize, state: &Hook<State>) -> impl View + '_ {
    let value = state.source.get_text(&state.rows[row][col]);

    if state.editing == (Editing::Cell { row, col }) {
        let onchange = state.bind(move |state, e: Event<InputElement>| {
            state.rows[row][col] = Text::Owned(e.target().value().into());
            state.editing = Editing::None;
        });

        Branch3::A(view! {
            <td.edit>
                { ref value }
                <input.edit {onchange} value={ ref value } />
            </td>
        })
    // https://github.com/maciejhirsz/kobold/issues/51
    } else {
        let ondblclick = state.bind(move |s, _| s.editing = Editing::Cell { row, col });

        if value.contains("0x") {
            Branch3::B(view! {
                <td {ondblclick}>
                    { ref value }
                    <QRForTask {value} />
                </td>
            })
        } else {
            Branch3::C(view! {
                <td {ondblclick}>{ ref value }</td>
            })
        }
    }
}