Form
Coordinates multi-field validation, async submission, named value serialization, and form reset.
Coordinates validation, submission, and reset across form fields, supporting both synchronous and asynchronous checks, named serialization, and nested fieldset disabling. The examples on this page use the Shadcn Rhea style preset.
Preview
Section titled “Preview”<div class="w-full max-w-md">
<proto-ui-shadcn-form-root id="shadcn-hero-form">
<proto-ui-shadcn-form-field id="shadcn-display-name">
<proto-ui-shadcn-field-label>
Display name
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control
id="shadcn-display-input"
></proto-ui-shadcn-field-control>
<proto-ui-shadcn-field-error></proto-ui-shadcn-field-error>
</proto-ui-shadcn-form-field>
<proto-ui-shadcn-form-field id="shadcn-quantity">
<proto-ui-shadcn-field-label>
Team seats
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-number-field-root id="shadcn-number">
<proto-ui-shadcn-number-field-decrement>
−
</proto-ui-shadcn-number-field-decrement>
<proto-ui-shadcn-number-field-control></proto-ui-shadcn-number-field-control>
<proto-ui-shadcn-number-field-increment>
+
</proto-ui-shadcn-number-field-increment>
</proto-ui-shadcn-number-field-root>
</proto-ui-shadcn-form-field>
<proto-ui-shadcn-form-field id="shadcn-channels">
<proto-ui-shadcn-field-label>
Notification channels
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-checkbox-group-root
id="shadcn-channels-group"
>
<proto-ui-shadcn-checkbox-group-item id="shadcn-ch-email">
Email
</proto-ui-shadcn-checkbox-group-item>
<proto-ui-shadcn-checkbox-group-item id="shadcn-ch-sms">
SMS
</proto-ui-shadcn-checkbox-group-item>
</proto-ui-shadcn-checkbox-group-root>
</proto-ui-shadcn-form-field>
<div class="flex flex-wrap items-center gap-2 pt-1">
<proto-ui-shadcn-form-submit>
Save changes
</proto-ui-shadcn-form-submit>
<proto-ui-shadcn-form-reset>Reset</proto-ui-shadcn-form-reset>
</div>
</proto-ui-shadcn-form-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-display-name'), {
name: 'displayName',
required: true,
});
setElementProps(document.querySelector('#shadcn-display-input'), {
placeholder: 'Ada Lovelace',
});
setElementProps(document.querySelector('#shadcn-quantity'), {
name: 'quantity',
});
setElementProps(document.querySelector('#shadcn-number'), {
defaultValue: 2,
});
setElementProps(document.querySelector('#shadcn-channels'), {
name: 'channels',
});
setElementProps(
document.querySelector('#shadcn-channels-group'),
{ defaultValue: ['email'] }
);
setElementProps(document.querySelector('#shadcn-ch-email'), {
value: 'email',
});
setElementProps(document.querySelector('#shadcn-ch-sms'), {
value: 'sms',
});
setElementProps(document.querySelector('#shadcn-hero-form'), {
ariaLabel: 'Account preferences form',
onSubmit: ({ values }) => console.log(values),
});
</script>Installation
Section titled “Installation”Available starting in 0.3.0-rc.1 and later releases via @proto.ui/prototypes-shadcn/form.
import { ShadcnFormRoot, ShadcnFormField, ShadcnFormSubmit, ShadcnFormReset, ShadcnFieldLabel, ShadcnFieldControl, ShadcnFieldError,} from '../proto-ui/components/react';
export function ExampleForm() { return ( <ShadcnFormRoot styleProfile="rhea" ariaLabel="Account settings" onSubmit={({ values }) => console.log(values)} > <ShadcnFormField styleProfile="rhea" name="username" required> <ShadcnFieldLabel styleProfile="rhea">Username</ShadcnFieldLabel> <ShadcnFieldControl styleProfile="rhea" placeholder="ada_lovelace" /> <ShadcnFieldError styleProfile="rhea" /> </ShadcnFormField> <div className="flex items-center gap-2"> <ShadcnFormSubmit styleProfile="rhea">Save</ShadcnFormSubmit> <ShadcnFormReset styleProfile="rhea" variant="outline"> Reset </ShadcnFormReset> </div> </ShadcnFormRoot> );}Anatomy
Section titled “Anatomy”- Root: Collects explicitly registered
FormFieldparts, validates enabled fields, and emitssubmitwith form values keyed by field name once all checks pass. Disabled fields (including those inside a disabledFieldset) are excluded, and duplicate field names throwFORM_DUPLICATE_NAMEduring serialization. SettingdisabledonRootcascades to all contained fields and actions. - Field: Extends
FieldRootwith a submissionname. StandaloneCheckbox,RadioGroup, andSwitchcontrols stay independent unless wrapped withfieldCheckbox,fieldRadioGroup, orfieldSwitchfrom@proto.ui/prototypes-shadcn/field. - Submit / Reset: Styled with matching button presets (
variant,size, andstyleProfile), supporting pointer and keyboard activation and exposingdisabled,hovered,pressed,focused,focusVisible, andfocusSelf().
Keyboard & Accessibility
Section titled “Keyboard & Accessibility”- Implicit Enter submit: Pressing
Enterinside a focused single-lineField,Number Field, orInput OTPsubmits the form (ignoring IME composition, modifier keys, and key repeat). - Focus on invalid: When validation fails,
invalidis emitted and focus moves to the first invalid field control. - Reset vs clear validation:
ResetorrequestReset()restores uncontrolled initial values, proposes initial values to controlled owners, cancels pending validation, and emitsreset. CallresetValidation()to clear validation errors without modifying field values.
Examples
Section titled “Examples”Profile form
Section titled “Profile form”Combine Fieldset, a text input, and a multiline Textarea with a primary submit and an outline reset action:
<div class="w-full max-w-md">
<proto-ui-shadcn-form-root id="shadcn-profile-form">
<proto-ui-shadcn-fieldset-root>
<proto-ui-shadcn-fieldset-legend>
Profile
</proto-ui-shadcn-fieldset-legend>
<proto-ui-shadcn-fieldset-description>
This information appears on your public profile and
invoices.
</proto-ui-shadcn-fieldset-description>
<proto-ui-shadcn-form-field id="shadcn-username-field">
<proto-ui-shadcn-field-label>
Username
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control
id="shadcn-username-control"
></proto-ui-shadcn-field-control>
<proto-ui-shadcn-field-description>
Choose a unique username for your account.
</proto-ui-shadcn-field-description>
<proto-ui-shadcn-field-error></proto-ui-shadcn-field-error>
</proto-ui-shadcn-form-field>
<proto-ui-shadcn-form-field id="shadcn-bio-field">
<proto-ui-shadcn-field-label>
Bio
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-textarea
id="shadcn-bio-control"
></proto-ui-shadcn-field-textarea>
</proto-ui-shadcn-form-field>
</proto-ui-shadcn-fieldset-root>
<div class="flex flex-wrap items-center gap-2 pt-1">
<proto-ui-shadcn-form-submit>
Save profile
</proto-ui-shadcn-form-submit>
<proto-ui-shadcn-form-reset id="shadcn-profile-reset">
Reset
</proto-ui-shadcn-form-reset>
</div>
</proto-ui-shadcn-form-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(
document.querySelector('#shadcn-username-field'),
{ name: 'username', required: true }
);
setElementProps(
document.querySelector('#shadcn-username-control'),
{ placeholder: 'ada_lovelace' }
);
setElementProps(document.querySelector('#shadcn-bio-field'), {
name: 'bio',
});
setElementProps(document.querySelector('#shadcn-bio-control'), {
placeholder: 'Tell us a little about yourself...',
rows: 3,
});
setElementProps(document.querySelector('#shadcn-profile-reset'), {
styleProfile: 'rhea',
variant: 'outline',
});
setElementProps(document.querySelector('#shadcn-profile-form'), {
ariaLabel: 'Profile settings',
onSubmit: ({ values }) => console.log(values),
});
</script>Action variants and sizes
Section titled “Action variants and sizes”FormSubmit and FormReset accept variant (default, secondary, outline, destructive, ghost) and size (sm, default, lg):
<div class="w-full max-w-md">
<proto-ui-shadcn-form-root id="shadcn-actions-form">
<proto-ui-shadcn-form-field id="shadcn-project-field">
<proto-ui-shadcn-field-label>
Project name
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control
id="shadcn-project-control"
></proto-ui-shadcn-field-control>
</proto-ui-shadcn-form-field>
<div class="flex flex-wrap items-center gap-2">
<proto-ui-shadcn-form-submit id="shadcn-submit-default">
Save
</proto-ui-shadcn-form-submit>
<proto-ui-shadcn-form-reset id="shadcn-reset-outline">
Reset
</proto-ui-shadcn-form-reset>
<proto-ui-shadcn-form-submit id="shadcn-submit-destructive">
Delete
</proto-ui-shadcn-form-submit>
<proto-ui-shadcn-form-reset id="shadcn-reset-ghost">
Cancel
</proto-ui-shadcn-form-reset>
</div>
</proto-ui-shadcn-form-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-project-field'), {
name: 'project',
required: true,
});
setElementProps(
document.querySelector('#shadcn-project-control'),
{ defaultValue: 'proto-ui-docs' }
);
setElementProps(
document.querySelector('#shadcn-submit-default'),
{ styleProfile: 'rhea', variant: 'default' }
);
setElementProps(document.querySelector('#shadcn-reset-outline'), {
styleProfile: 'rhea',
variant: 'outline',
});
setElementProps(
document.querySelector('#shadcn-submit-destructive'),
{ styleProfile: 'rhea', variant: 'destructive', size: 'sm' }
);
setElementProps(document.querySelector('#shadcn-reset-ghost'), {
styleProfile: 'rhea',
variant: 'ghost',
size: 'sm',
});
</script>Disabled fieldset
Section titled “Disabled fieldset”Disabling a nested Fieldset cascades to its contained fields and automatically excludes them from submitted values:
<div class="w-full max-w-md">
<proto-ui-shadcn-form-root id="shadcn-disabled-form">
<proto-ui-shadcn-form-field id="shadcn-email-field">
<proto-ui-shadcn-field-label>
Contact email
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control
id="shadcn-email-control"
></proto-ui-shadcn-field-control>
</proto-ui-shadcn-form-field>
<proto-ui-shadcn-fieldset-root id="shadcn-billing-group">
<proto-ui-shadcn-fieldset-legend>
Billing details
</proto-ui-shadcn-fieldset-legend>
<proto-ui-shadcn-form-field id="shadcn-company-field">
<proto-ui-shadcn-field-label>
Company
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control
id="shadcn-company-control"
></proto-ui-shadcn-field-control>
</proto-ui-shadcn-form-field>
</proto-ui-shadcn-fieldset-root>
<div class="flex flex-wrap items-center gap-2 pt-1">
<proto-ui-shadcn-form-submit>
Submit
</proto-ui-shadcn-form-submit>
<proto-ui-shadcn-form-reset>Reset</proto-ui-shadcn-form-reset>
</div>
</proto-ui-shadcn-form-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-email-field'), {
name: 'email',
required: true,
});
setElementProps(document.querySelector('#shadcn-email-control'), {
defaultValue: 'ada@example.com',
});
setElementProps(document.querySelector('#shadcn-billing-group'), {
disabled: true,
});
setElementProps(document.querySelector('#shadcn-company-field'), {
name: 'company',
});
setElementProps(
document.querySelector('#shadcn-company-control'),
{ defaultValue: 'Analytical Engine Co.' }
);
</script>Versioned async validation and nested controls
Section titled “Versioned async validation and nested controls”Demonstrates external validation requests delivered via resolveValidation with their requestId. Editing, resetting, or toggling readOnly before an older reply arrives automatically discards the stale result:
<div class="w-full max-w-md">
<proto-ui-shadcn-form-root id="shadcn-async-form">
<proto-ui-shadcn-form-field id="shadcn-account-field">
<proto-ui-shadcn-field-label>
Username
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control
id="shadcn-account-control"
></proto-ui-shadcn-field-control>
<proto-ui-shadcn-field-error></proto-ui-shadcn-field-error>
</proto-ui-shadcn-form-field>
<div class="flex flex-wrap items-center gap-2 pt-1">
<proto-ui-shadcn-form-submit>
Submit
</proto-ui-shadcn-form-submit>
<proto-ui-shadcn-form-reset>Reset</proto-ui-shadcn-form-reset>
</div>
</proto-ui-shadcn-form-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const field = document.querySelector('#shadcn-account-field');
const control = document.querySelector('#shadcn-account-control');
setElementProps(control, {
value: 'ada',
onValueChange: ({ value }) =>
setElementProps(control, { value }),
});
setElementProps(field, {
name: 'account',
required: true,
externalValidation: true,
validationMode: 'onChange',
onValidationRequest: (req) => {
const taken = req.value === 'admin';
field.getExposes().resolveValidation(req.requestId, {
invalid: taken,
errors: taken ? ['This username is already taken.'] : [],
});
},
});
</script>Explicit choice fields
Section titled “Explicit choice fields”Use fieldCheckbox, fieldRadioGroup, and fieldSwitch from @proto.ui/prototypes-shadcn/field inside a FormField to wire required validation, read-only behavior, fieldset disabling, and reset for choice controls:
<proto-ui-shadcn-form-root id="shadcn-choice-form">
<proto-ui-shadcn-form-field id="shadcn-agree-field">
<proto-ui-shadcn-field-label>
Accept updates
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-checkbox id="shadcn-agree">
<proto-ui-shadcn-checkbox-indicator></proto-ui-shadcn-checkbox-indicator>
</proto-ui-shadcn-field-checkbox>
<proto-ui-shadcn-field-error></proto-ui-shadcn-field-error>
</proto-ui-shadcn-form-field>
<proto-ui-shadcn-form-field id="shadcn-alerts-field">
<proto-ui-shadcn-field-label>
Alerts
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-switch id="shadcn-alerts">
<proto-ui-shadcn-switch-thumb></proto-ui-shadcn-switch-thumb>
</proto-ui-shadcn-field-switch>
</proto-ui-shadcn-form-field>
<div class="flex flex-wrap gap-2">
<proto-ui-shadcn-form-submit>
Submit
</proto-ui-shadcn-form-submit>
<proto-ui-shadcn-form-reset>Reset</proto-ui-shadcn-form-reset>
</div>
</proto-ui-shadcn-form-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-agree-field'), {
name: 'agreement',
required: true,
});
setElementProps(document.querySelector('#shadcn-alerts-field'), {
name: 'alerts',
});
</script>FormRoot
Section titled “FormRoot”| Prop | Type | Default |
|---|---|---|
disabled | boolean | false |
ariaLabel | string | '' |
styleProfile | ShadcnStyleId | 'nova' |
Exposed states: disabled, pending, submitted. Methods: getValues(), requestSubmit(), requestReset(), resetValidation(), cancelSubmit(). Events: submit ({ values, submissionId }), invalid ({ names }), reset.
FormField
Section titled “FormField”| Prop | Type | Default |
|---|---|---|
name | string | '' |
required | boolean | false |
disabled | boolean | false |
readOnly | boolean | false |
validationMode | 'onSubmit' 'onBlur' 'onChange' | 'onSubmit' |
externalValidation | boolean | false |
styleProfile | ShadcnStyleId | 'nova' |
FormSubmit / FormReset
Section titled “FormSubmit / FormReset”| Prop | Type | Default |
|---|---|---|
disabled | boolean | false |
variant | 'default' 'secondary' 'outline' 'destructive' 'ghost' | 'default' / 'secondary' |
size | 'default' 'sm' 'lg' | 'default' |
styleProfile | ShadcnStyleId | 'nova' |
Exposed states: disabled, hovered, pressed, focused, focusVisible. Methods: focusSelf(options?).