DioxusLabs / dioxus

Fullstack GUI library for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
19.37k stars 746 forks source link

Progressively Enhanced Fullstack forms #1996

Open ealmloff opened 4 months ago

ealmloff commented 4 months ago

Specific Demand

It would be nice if dioxus provided a type safe form that works with the router and dioxus fullstack server functions.

Implement Suggestion

#[derive(Variants)]
enum Options {
    #[variant("first-option")]
    First,
    #[variant("second-option")]
    Second,
}

#[derive(Form)]
struct FormData {
    #[hint = "enter your username"]
    username: String,
    #[hint = "enter your password"]
    password: String,
    #[default = Option::Second]
    dropdown: Options,
    checkable: bool
}

#[server]
fn server(data: FormData) -> Result<()> {
    todo!()
}

let data = use_form::<FormData>();

// You can read form values easily before the form is submitted
let checkable_class = if data.read().checkable { "checked" } else { "unchecked" };

rsx! {
    Form {
        action: server,
        input {
            // You can spread fields to automatically add listeners
            ..data.username()
        }
        input {
            // You can style and layout forms however you want to
            class: "ring-1 ring-inset ring-gray-300",
            ..data.password()
        }
        select {
            ..data.dropdown()
        }
    }
}
jkelleyrtp commented 4 months ago

I like the API you have with the spreading!