@k8ordo/ui

Form

A wrapper for <form>. action takes a Server Action or a URL.

Import

ts
import { Form } from '@k8ordo/ui';

Usage

tsx
<Form
  action={async (formData) => {
    const name = formData.get('name');
    await save(name);
  }}
>
  <FormControl
    label="Name"
    renderInput={(props) => <TextField {...props} name="name" />}
  />
  <Button type="submit">Submit</Button>
</Form>

With useActionState

tsx
const [message, formAction] = useActionState(
  async (_prev, formData) => {
    const name = formData.get('name');
    await sleep(1000);
    return `Hello, ${name}!`;
  },
  '',
);

return (
  <Form action={formAction}>
    <FormControl
      label="Name"
      renderInput={(props) => <TextField {...props} name="name" />}
    />
    <Button type="submit">Submit</Button>
    <p>{message}</p>
  </Form>
);

Props

children
Type: ReactNode
Default: -
action?
Type: ((formData: FormData) => void | Promise<void>)|string
Default: -
ref?
Type: Ref<HTMLFormElement>
Default: -
PropTypeDefault
childrenReactNode-
action?((formData: FormData) => void | Promise<void>)|string-
ref?Ref<HTMLFormElement>-

Inherited attributes (except the ones set internally): FormHTMLAttributes<HTMLFormElement>

k8ordo

React libraries that use Baseline features without holding back.