Form

A form whose fields check their values with the browser's own validation before it submits, with a message for each way a value can fail, on Radix Form.

import {
  Form,
  FormControl,
  FormField,
  FormLabel,
  FormMessage,
  FormSubmit,
} from "@produkto/ui/Form";
import { Input } from "@produkto/ui/Input";

const Example = () => (
  <Form
    className="w-full max-w-xs"
    onSubmit={(event) => event.preventDefault()}
  >
    <FormField name="email">
      <FormLabel>Label</FormLabel>
      <FormControl>
        <Input placeholder="Placeholder" required type="email" />
      </FormControl>
      <FormMessage match="valueMissing">Description</FormMessage>
      <FormMessage match="typeMismatch">Description</FormMessage>
    </FormField>
    <FormSubmit>Button</FormSubmit>
  </Form>
);

export default Example;

Usage

import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormLabel,
  FormMessage,
  FormSubmit,
} from "@produkto/ui/Form"
import { Input } from "@produkto/ui/Input"
<Form>
  <FormField name="email">
    <FormLabel>Label</FormLabel>
    <FormControl>
      <Input required type="email" />
    </FormControl>
    <FormDescription>Description</FormDescription>
    <FormMessage match="valueMissing">Enter an email.</FormMessage>
    <FormMessage match="typeMismatch">Enter a valid email.</FormMessage>
  </FormField>
  <FormSubmit>Button</FormSubmit>
</Form>

Examples

Messages

A FormMessage with no children writes Radix's own words for its match, as the empty field here shows, and children put yours in their place, as a wrong address shows.

import {
  Form,
  FormControl,
  FormField,
  FormLabel,
  FormMessage,
  FormSubmit,
} from "@produkto/ui/Form";
import { Input } from "@produkto/ui/Input";

const Example = () => (
  <Form
    className="w-full max-w-xs"
    onSubmit={(event) => event.preventDefault()}
  >
    <FormField name="website">
      <FormLabel>Label</FormLabel>
      <FormControl>
        <Input placeholder="Placeholder" required type="url" />
      </FormControl>
      <FormMessage match="valueMissing" />
      <FormMessage match="typeMismatch">Description</FormMessage>
    </FormField>
    <FormSubmit>Button</FormSubmit>
  </Form>
);

export default Example;

Match

match takes one of the browser's checks, such as valueMissing, or a function of the value and the form's data, which may be async, and the message shows while it returns true.

import {
  Form,
  FormControl,
  FormField,
  FormLabel,
  FormMessage,
  FormSubmit,
} from "@produkto/ui/Form";
import { Input } from "@produkto/ui/Input";

const Example = () => (
  <Form
    className="w-full max-w-xs"
    onSubmit={(event) => event.preventDefault()}
  >
    <FormField name="password">
      <FormLabel>Label</FormLabel>
      <FormControl>
        <Input type="password" />
      </FormControl>
    </FormField>
    <FormField name="confirm">
      <FormLabel>Label</FormLabel>
      <FormControl>
        <Input type="password" />
      </FormControl>
      <FormMessage
        match={(value, formData) => value !== formData.get("password")}
      >
        Description
      </FormMessage>
    </FormField>
    <FormSubmit>Button</FormSubmit>
  </Form>
);

export default Example;

Server invalid

serverInvalid on a FormField and forceMatch on its FormMessage show an error the server sent back, here half a second after any submit, until onClearServerErrors clears it on the next one.

import {
  Form,
  FormControl,
  FormField,
  FormLabel,
  FormMessage,
  FormSubmit,
} from "@produkto/ui/Form";
import { Input } from "@produkto/ui/Input";
import { useState } from "react";

const Example = () => {
  const [serverInvalid, setServerInvalid] = useState(false);

  return (
    <Form
      className="w-full max-w-xs"
      onClearServerErrors={() => setServerInvalid(false)}
      onSubmit={(event) => {
        event.preventDefault();
        setTimeout(() => setServerInvalid(true), 500);
      }}
    >
      <FormField name="email" serverInvalid={serverInvalid}>
        <FormLabel>Label</FormLabel>
        <FormControl>
          <Input placeholder="Placeholder" required type="email" />
        </FormControl>
        <FormMessage match="valueMissing">Description</FormMessage>
        <FormMessage forceMatch={serverInvalid} match="typeMismatch">
          Description
        </FormMessage>
      </FormField>
      <FormSubmit>Button</FormSubmit>
    </Form>
  );
};

export default Example;

API Reference

It takes the props of Radix Form.Root.

FormField

FormFieldProps · takes the props of Radix Form.Field

FormLabel

FormLabelProps · takes the props of Radix Form.Label

FormControl

FormControlProps · takes the props of Radix Form.Control

FormDescription

FormDescriptionProps · takes the props of Radix Form.Message

FormMessage

FormMessageProps · takes the props of Radix Form.Message

FormValidityState

FormValidityStateProps · takes the props of Radix Form.ValidityState

FormSubmit

FormSubmitProps · also takes the props of <button>
PropTypeDefault
hrefstring-
targetHTMLAttributeAnchorTarget-
relstring-
prefetchboolean-
appearanceButtonAppearance"solid" | "soft" | "outline" | "transparent"-
toneButtonTone"neutral" | "accent" | "destructive" | "success" | "warning" | "info"-
sizeButtonSize"xs" | "sm" | "base" | "lg"-
wrapboolean-
blockboolean-
mutedboolean-
alignButtonAlign"start" | "center" | "end"-
borderRadiusButtonBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full"-
loadingboolean-
onClickMouseEventHandler<HTMLElement>-