Field

Ties a form control to its label, help text, and error message.

Usage

Code
import { Field } from '@leverui/components';
Code
<Field.Root>
  <Field.Label>
    <Field.RequiredIndicator />
  </Field.Label>
  <Input />
  <Field.HelperText />
  <Field.ErrorText />
</Field.Root>

For one component that takes the label and messages as props, see the snippet below.

Examples

Error Text

Pass the invalid prop to Field.Root and use the Field.ErrorText to indicate that the field is invalid.

Enter a valid email address.

Error Icon

Field.ErrorIcon inherits the surrounding text color and defaults to 1em, keeping its size consistent even when Field.ErrorText stretches full width.

Enter a valid email address.

Helper Text

Use the Field.HelperText to add helper text to the field.

We only use your email to sign you in.

Horizontal

Use the orientation="horizontal" prop to align the label and input horizontally.

Disabled

Use the disabled prop to disable the field.

Textarea

Here's how to use the field component with a textarea.

Native Select

Here's how to use the field component with a native select.

Target Control

Set the target prop on Field.Root to the item the label should point to.

Required

Pass the required prop to Field.Root and use the Field.RequiredIndicator to indicate that the field is required.

Optional

Pass the fallback prop to the Field.RequiredIndicator to add optional text.

Closed Component

Takes the label, helper text, and error text as props.

Code
import { Show, omit } from 'solid-js';
import type { JSX } from '@solidjs/web';
import { Field as FieldParts, type FieldRootProps } from '@leverui/components';

export type FieldProps = FieldRootProps & {
  label?: JSX.Element;
  helperText?: JSX.Element;
  errorText?: JSX.Element;
  optionalText?: JSX.Element;
};

export function Field(props: FieldProps) {
  const root = omit(props, 'label', 'helperText', 'errorText', 'optionalText', 'children');

  return (
    <FieldParts.Root {...root}>
      <Show when={props.label}>
        <FieldParts.Label>
          {props.label}
          <FieldParts.RequiredIndicator fallback={props.optionalText} />
        </FieldParts.Label>
      </Show>
      {props.children}
      <Show when={props.helperText}>
        <FieldParts.HelperText>{props.helperText}</FieldParts.HelperText>
      </Show>
      <Show when={props.errorText}>
        <FieldParts.ErrorText>{props.errorText}</FieldParts.ErrorText>
      </Show>
    </FieldParts.Root>
  );
}

Guides

The label is linked to the control inside the field. required, disabled, readOnly, or invalid on Field.Root sets the same state on the control. Helper and error text on the page are part of the control's accessible description. Field.ErrorText shows only while invalid is true.

Field.PropsProvider sets default props for the fields inside it. The defaults can change while the page is open. A prop on a field wins. To own the state elsewhere, create it with useField and pass it to Field.RootProvider. Read the nearest field's state with Field.Context or useFieldContext.

Props

Root

PropDefaultTypeDescription
orientation'vertical''vertical' | 'horizontal'Label above the control or beside it.
asdivNative tag nameHTML tag to render.
asChild—Solid render functionRender your own element with the field's props.
unstyled—booleanTurn off the Lever UI styles.
disabled—booleanDisable the field.
id—stringUnique id for the field.
ids—FieldElementIdsIds of the field's parts.
invalid—booleanMark the field invalid.
readOnly—booleanMake the field read-only.
required—booleanMark the field required.
target—stringvalue of the Field.Item the label points to.

Customize with CSS

Load the package stylesheet first. Classes: .leverui-field__root, __label, __helperText, __errorText, __requiredIndicator. Or add your own class to a part and style it after the Lever UI stylesheet.

--leverui-field-label-width sets the label column width in a horizontal field. Default: 80px.

Code
.account-form {
  --leverui-colors-fg-error: #a02fd0;
  --leverui-field-label-width: 96px;
}

.account-form .leverui-field__label {
  font-weight: 700;
}

unstyled turns off the styles so you can write your own. The root and the parts pass native HTML attributes, style, and refs to their elements. as picks the HTML tag. asChild renders your own element with Field's attributes.

Explorer

Click a part name to highlight it in the preview.

Component Anatomy

Hover to highlight, click to select parts