Skip to content
Sapphire UIv4.1.0

Form Field

Keep a label, control, help text and validation message together. Use it for forms where every input needs an accessible name and nearby guidance.

Examples

States

JSX usage

Install this example’s dependencies, load the package stylesheet from Setup, and add Example CSS where shown. The snippet includes its local helpers.

bun add @kongmy-dev/sapphire-ui react react-dom
"use client";
import { Input } from "@kongmy-dev/sapphire-ui/input";
import { FormField } from "@kongmy-dev/sapphire-ui/form-field";

function FieldStates() {
  return (
    <div className="docs-form-field-demo sp-stack">
      <FormField label="Invalid email" error="Enter a valid email." required>
        <Input type="email" defaultValue="invalid" />
      </FormField>
      <FormField label="Locked field" hint="This value cannot be changed.">
        <Input disabled defaultValue="Locked" />
      </FormField>
    </div>
  );
}

export default FieldStates;
Example CSS

Load these demo styles alongside the package stylesheet for the same layout and surfaces.

.docs-form-field-demo {
  max-width: calc(var(--space-1) * 100);
}
HTML markup

Initial rendered HTML only. React handlers and portalled content require the JSX example; static HTML does not reproduce those interactions.

Composition

JSX usage

Install this example’s dependencies, load the package stylesheet from Setup, and add Example CSS where shown. The snippet includes its local helpers.

bun add @kongmy-dev/sapphire-ui react react-dom
"use client";
import { Input } from "@kongmy-dev/sapphire-ui/input";
import { NativeSelect } from "@kongmy-dev/sapphire-ui/native-select";
import { Textarea } from "@kongmy-dev/sapphire-ui/textarea";
import { FormField } from "@kongmy-dev/sapphire-ui/form-field";

function FieldComposition() {
  return (
    <div className="docs-form-field-demo sp-stack">
      <FormField
        label="Email"
        required
        hint="We will send a verification link."
      >
        <Input type="email" placeholder="you@example.com" />
      </FormField>
      <FormField label="Message" hint="Maximum 280 characters.">
        <Textarea maxLength={280} />
      </FormField>
      <FormField label="Plan">
        <NativeSelect>
          <option>Free</option>
          <option>Pro</option>
        </NativeSelect>
      </FormField>
    </div>
  );
}

export default FieldComposition;
Example CSS

Load these demo styles alongside the package stylesheet for the same layout and surfaces.

.docs-form-field-demo {
  max-width: calc(var(--space-1) * 100);
}
HTML markup

Initial rendered HTML only. React handlers and portalled content require the JSX example; static HTML does not reproduce those interactions.

Compound Control

JSX usage

Install this example’s dependencies, load the package stylesheet from Setup, and add Example CSS where shown. The snippet includes its local helpers.

bun add @kongmy-dev/sapphire-ui react react-dom radix-ui
"use client";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@kongmy-dev/sapphire-ui/select";
import { FormField } from "@kongmy-dev/sapphire-ui/form-field";

function CompoundField() {
  return (
    <FormField
      label="Billing country"
      required
      hint="Used for tax calculations."
    >
      {({ controlProps, required }) => (
        <Select required={required} defaultValue="my" name="billing-country">
          <SelectTrigger {...controlProps}>
            <SelectValue />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="my">Malaysia</SelectItem>
            <SelectItem value="sg">Singapore</SelectItem>
          </SelectContent>
        </Select>
      )}
    </FormField>
  );
}

export default CompoundField;
Example CSS

Load these demo styles alongside the package stylesheet for the same layout and surfaces.

.docs-form-field-demo {
  max-width: calc(var(--space-1) * 100);
}
HTML markup

Initial rendered HTML only. React handlers and portalled content require the JSX example; static HTML does not reproduce those interactions.

Validation

JSX usage

Install this example’s dependencies, load the package stylesheet from Setup, and add Example CSS where shown. The snippet includes its local helpers.

bun add @kongmy-dev/sapphire-ui react react-dom
"use client";
import { useState } from "react";
import { Input } from "@kongmy-dev/sapphire-ui/input";
import { FormField } from "@kongmy-dev/sapphire-ui/form-field";

function FieldValidation() {
  const [value, setValue] = useState('');
  const error =
    value && !value.includes('@') ? 'Enter an email with @.' : undefined;
  return (
    <FormField
      label="Validated email"
      hint="Use your work email."
      error={error}
    >
      <Input
        type="email"
        value={value}
        onChange={(event) => setValue(event.currentTarget.value)}
      />
    </FormField>
  );
}

export default FieldValidation;
Example CSS

Load these demo styles alongside the package stylesheet for the same layout and surfaces.

.docs-form-field-demo {
  max-width: calc(var(--space-1) * 100);
}
HTML markup

Initial rendered HTML only. React handlers and portalled content require the JSX example; static HTML does not reproduce those interactions.

Props

Generated from the current public TypeScript signatures, including composed behaviour props. “—” means no default is declared in the local signature; forwarded behaviour defaults are not inferred.

FormField

FormField component props
PropTypeRequiredLocal default
labelReactNodeNo—
requiredboolean | undefinedNo—
hintReactNodeNo—
errorReactNodeNo—
childrenReactElement<unknown, string | import("react").JSXElementConstructor<any>> | ((field: { controlProps: Pick<ComponentProps<"button">, "id" | "aria-describedby" | "aria-invalid" | "aria-required">; required: boolean; }) => ReactNode)Yes—
Inherited HTML / React props (279)
FormField common platform props
PropTypeRequiredLocal default
styleimport("react").CSSProperties | undefinedNo—
refimport("react").Ref<HTMLDivElement> | undefinedNo—
defaultValuestring | number | readonly string[] | undefinedNo—
classNamestring | undefinedNo—
idstring | undefinedNo—
tabIndexnumber | undefinedNo—
roleimport("react").AriaRole | undefinedNo—
aria-labelstring | undefinedNo—
onChangeimport("react").ChangeEventHandler<HTMLDivElement, Element> | undefinedNo—

All inherited prop names:

slot style title ref key defaultChecked defaultValue suppressContentEditableWarning suppressHydrationWarning accessKey autoCapitalize autoFocus className contentEditable contextMenu dir draggable enterKeyHint hidden id lang nonce spellCheck tabIndex translate radioGroup role about content datatype inlist prefix property rel resource rev typeof vocab autoCorrect autoSave color itemProp itemScope itemType itemID itemRef results security unselectable popover popoverTargetAction popoverTarget inert inputMode is exportparts part aria-activedescendant aria-atomic aria-autocomplete aria-braillelabel aria-brailleroledescription aria-busy aria-checked aria-colcount aria-colindex aria-colindextext aria-colspan aria-controls aria-current aria-describedby aria-description aria-details aria-disabled aria-dropeffect aria-errormessage aria-expanded aria-flowto aria-grabbed aria-haspopup aria-hidden aria-invalid aria-keyshortcuts aria-label aria-labelledby aria-level aria-live aria-modal aria-multiline aria-multiselectable aria-orientation aria-owns aria-placeholder aria-posinset aria-pressed aria-readonly aria-relevant aria-required aria-roledescription aria-rowcount aria-rowindex aria-rowindextext aria-rowspan aria-selected aria-setsize aria-sort aria-valuemax aria-valuemin aria-valuenow aria-valuetext dangerouslySetInnerHTML onCopy onCopyCapture onCut onCutCapture onPaste onPasteCapture onCompositionEnd onCompositionEndCapture onCompositionStart onCompositionStartCapture onCompositionUpdate onCompositionUpdateCapture onFocus onFocusCapture onBlur onBlurCapture onChange onChangeCapture onBeforeInput onBeforeInputCapture onInput onInputCapture onReset onResetCapture onSubmit onSubmitCapture onInvalid onInvalidCapture onLoad onLoadCapture onError onErrorCapture onKeyDown onKeyDownCapture onKeyPress onKeyPressCapture onKeyUp onKeyUpCapture onAbort onAbortCapture onCanPlay onCanPlayCapture onCanPlayThrough onCanPlayThroughCapture onDurationChange onDurationChangeCapture onEmptied onEmptiedCapture onEncrypted onEncryptedCapture onEnded onEndedCapture onLoadedData onLoadedDataCapture onLoadedMetadata onLoadedMetadataCapture onLoadStart onLoadStartCapture onPause onPauseCapture onPlay onPlayCapture onPlaying onPlayingCapture onProgress onProgressCapture onRateChange onRateChangeCapture onSeeked onSeekedCapture onSeeking onSeekingCapture onStalled onStalledCapture onSuspend onSuspendCapture onTimeUpdate onTimeUpdateCapture onVolumeChange onVolumeChangeCapture onWaiting onWaitingCapture onAuxClick onAuxClickCapture onClick onClickCapture onContextMenu onContextMenuCapture onDoubleClick onDoubleClickCapture onDrag onDragCapture onDragEnd onDragEndCapture onDragEnter onDragEnterCapture onDragExit onDragExitCapture onDragLeave onDragLeaveCapture onDragOver onDragOverCapture onDragStart onDragStartCapture onDrop onDropCapture onMouseDown onMouseDownCapture onMouseEnter onMouseLeave onMouseMove onMouseMoveCapture onMouseOut onMouseOutCapture onMouseOver onMouseOverCapture onMouseUp onMouseUpCapture onSelect onSelectCapture onTouchCancel onTouchCancelCapture onTouchEnd onTouchEndCapture onTouchMove onTouchMoveCapture onTouchStart onTouchStartCapture onPointerDown onPointerDownCapture onPointerMove onPointerMoveCapture onPointerUp onPointerUpCapture onPointerCancel onPointerCancelCapture onPointerEnter onPointerLeave onPointerOver onPointerOverCapture onPointerOut onPointerOutCapture onGotPointerCapture onGotPointerCaptureCapture onLostPointerCapture onLostPointerCaptureCapture onScroll onScrollCapture onScrollEnd onScrollEndCapture onWheel onWheelCapture onAnimationStart onAnimationStartCapture onAnimationEnd onAnimationEndCapture onAnimationIteration onAnimationIterationCapture onToggle onBeforeToggle onTransitionCancel onTransitionCancelCapture onTransitionEnd onTransitionEndCapture onTransitionRun onTransitionRunCapture onTransitionStart onTransitionStartCapture

ARIA attributes and native event handlers keep their React types. data-* attributes are also accepted by React.

Setup

bun add @kongmy-dev/sapphire-ui react react-dom

For an app using package components throughout, load the full stylesheet once. Add this component import where you use it.

import "@kongmy-dev/sapphire-ui/sapphire.css";
import { FormField } from "@kongmy-dev/sapphire-ui/form-field";

Mixing package and vendored components? Follow application setup.

Search documentation