hyuskui

Forms

Form state, validation and async actions for GPUI, in one Rust file you own. What React Hook Form, Zod and server actions give React apps.

Forms are hard to get right from scratch: validation timing, error display, focus, resets, lists of fields, and a submit that waits on the network without being sent twice. hyuskui ships all of it as src/ui/form.rs, plain Rust you copy into your app and change as you like. There is no form crate to depend on, and the demos below are the real code running in GPUI.

Loading GPUI…
use gpui::{div, prelude::*, px, App, Window};

use crate::ui::button::{Button, ButtonVariant};
use crate::ui::card::Card;
use crate::ui::form::{debug_json, field, string, Form, FormExt, Schema};
use crate::ui::input_group::InputGroup;
use crate::ui::toast::{toast, Toast, Toaster};

#[derive(Clone, Default, Debug, PartialEq)]
struct BugReport {
    title: String,
    description: String,
}

fn bug_report_schema() -> Schema<BugReport> {
    Schema::<BugReport>::new()
        .field(
            field!(title),
            string()
                .min(5, "Bug title must be at least 5 characters.")
                .max(32, "Bug title must be at most 32 characters."),
        )
        .field(
            field!(description),
            string()
                .min(20, "Description must be at least 20 characters.")
                .max(100, "Description must be at most 100 characters."),
        )
}

pub fn form_demo(window: &mut Window, cx: &mut App) -> impl IntoElement {
    let form = window.use_keyed_state("form-demo", cx, |_, _| {
        Form::new(BugReport::default())
            .schema(bug_report_schema())
            .on_success(|values, _, cx| {
                toast(
                    Toast::new("You submitted the following values:").code(debug_json(values)),
                    cx,
                )
            })
    });
    let title = form.input(field!(title), window, cx, |input| {
        input.placeholder("Login button not working on mobile")
    });
    let description = form.input(field!(description), window, cx, |input| {
        input
            .multi_line(4)
            .bare()
            .placeholder("I'm having an issue with the login button on mobile.")
    });

    let f = form.read(cx);
    let count = f.values().description.chars().count();

    div()
        .w(px(448.))
        .flex()
        .flex_col()
        .child(
            Card::new()
                .title("Bug Report")
                .description("Help us improve by reporting bugs you encounter.")
                .child(
                    div()
                        .flex()
                        .flex_col()
                        .gap_6()
                        .child(f.field("title").label("Bug Title").child(title))
                        .child(
                            f.field("description")
                                .label("Description")
                                .description("Include steps to reproduce, expected behavior, and what actually happened.")
                                .child(InputGroup::new(description).block_end(format!("{count}/100 characters"))),
                        ),
                )
                .footer(
                    div()
                        .flex()
                        .gap_2()
                        .child(
                            Button::new("form-demo-reset", "Reset")
                                .variant(ButtonVariant::Outline)
                                .on_click(form.handle_reset()),
                        )
                        .child(Button::new("form-demo-submit", "Submit").on_click(form.handle_submit())),
                ),
        )
        .child(Toaster::new())
}
$ cargo hyuskui add form

How it fits together

SchemaThe rules for your values struct, each with its message, like a Zod schema.
FormAn entity holding the values, errors, touched fields, submit count and pending action, like useForm.
FieldThe existing component that lays out a label, the control, helper text and the error.
ActionsAn async function that runs after validation, like a server action, with pending state and server errors.
#[derive(Clone, Default, Debug, PartialEq)]
struct SignUp { email: String }

let form = cx.new(|_| {
    Form::new(SignUp::default())
        .schema(Schema::<SignUp>::new().field(field!(email), string().email("Enter a valid email address.")))
        .action(|values, cx| async move { /* call your backend */ Ok(()) })
        .on_success(|values, _, cx| toast(Toast::new("Welcome aboard"), cx))
});

Beyond what React Hook Form does out of the box, a failed submit focuses the first invalid field, and an action's errors can land on a field or on the whole form.

Guides