Form
Schema validation and a submit lifecycle around fields
Installation
npx shadcn@latest add @sudokw/components-formimport { Form } from "@gpuixui/components";Usage
import { Form } from "@gpuixui/components/form";
import { Field, FieldInput } from "@gpuixui/components/field";
import { Button } from "@gpuixui/primitives";
import { Schema } from "effect";
import { useState } from "react";
const Account = Schema.Struct({
email: Schema.NonEmptyString,
name: Schema.NonEmptyString,
});
export const CreateAccount = () => {
const [draft, setDraft] = useState({ email: "", name: "" });
return (
<Form
label="Create account"
schema={Account}
value={draft}
onSubmit={(account) => save(account.email, account.name)}
>
{(state) => (
<>
<Field label="Email">
<FieldInput
value={draft.email}
onChange={(event) =>
setDraft({ ...draft, email: event.value ?? "" })
}
/>
</Field>
<Field label="Name">
<FieldInput
value={draft.name}
onChange={(event) =>
setDraft({ ...draft, name: event.value ?? "" })
}
/>
</Field>
<Button
onClick={state.submit}
disabled={state.pending}
loading={state.pending}
>
Create account
</Button>
</>
)}
</Form>
);
};Composition
Form
├── message block
└── children (fields)API reference
| Prop | Type | Default | Consequence |
|---|---|---|---|
schema | Schema.ConstraintDecoder<T> | none | decides whether the value may be submitted; its messages are the ones painted |
value | unknown | none | the caller's draft; the form never reads a value from the runtime |
onSubmit | (value: T) => void | none | receives the decoded value, only when the schema accepts it |
pending | boolean | false | refuses submit while true, so one press is one save |
label | string | none | makes the root role="form" with aria-label; without it the root is a plain container |
className | string | none | joined after the recipe, so it wins conflicts |
children | (state: FormState) => ReactNode | none | the render prop: errors, pending, submit, form |
style | StateStyle | none | merged last on the root |
Edit this page on GitHubapps/docs/content/docs/components/form.mdx