Components

Field

A component that provides labeling and validation for form controls.

Visible on your profile

Anatomy#

Import the component and assemble its parts:

Anatomytsx
import { Field } from '@egoist/solid-base/field';

<Field.Root>
  <Field.Label />
  <Field.Control />
  <Field.Description />
  <Field.Item />
  <Field.Error />
  <Field.Validity />
</Field.Root>;

Examples#

Custom validation#

Pass a validate function to <Field.Root> to run your own checks. Return an error message (or an array of messages) when the value is invalid, and null when it is valid; <Field.Error> without a match prop shows the returned message. validationMode controls when it runs:

<Field.Root
  validationMode="onChange"
  validate={(value) => (String(value).length < 3 ? 'Use at least 3 characters' : null)}
>
  <Field.Label>Username</Field.Label>
  <Field.Control />
  <Field.Error />
</Field.Root>

Reading validity#

<Field.Validity> takes a function as its child and calls it with the field's validity state, for rendering custom feedback:

<Field.Validity>
  {(state) => <span>{state.validity.valid ? 'Looks good' : 'Not yet'}</span>}
</Field.Validity>

API reference#

<Field.Root>

Groups all parts of the field. Renders a <div> element.

PropTypeDefault
disabledbooleanfalse

Whether the component should ignore user interaction. Takes precedence over the disabled prop on the <Field.Control> component.

namestring—

Identifies the field when a form is submitted. Takes precedence over the name prop on the <Field.Control> component.

validate| (( value: unknown, formValues: Form.Values, ) => string | string[] | null | void | Promise<string | string[] | null | void>)—

A function for custom validation. Return a string or an array of strings with the error message(s) if the value is invalid. Returning nothing, null, an empty string, or an empty array means the value is valid. Asynchronous functions are supported, but they do not prevent form submission when using validationMode="onSubmit".

validationModeForm.ValidationMode'onSubmit'

Determines when the field should be validated. This takes precedence over the validationMode prop on <Form>.

- onSubmit: triggers validation when the form is submitted, and re-validates on change after submission. - onBlur: triggers validation when the control loses focus. - onChange: triggers validation on every change to the control value.

validationDebounceTimenumber0

How long to wait between validate callbacks if validationMode="onChange" is used. Specified in milliseconds.

invalidboolean—

Whether the field is invalid. Useful when the field state is controlled by an external library.

dirtyboolean—

Whether the field's value has been changed from its initial value. Useful when the field state is controlled by an external library.

touchedboolean—

Whether the field has been touched. Useful when the field state is controlled by an external library.

actionsRefActionsRef<FieldRoot.Actions>—

A ref to imperative actions (a { current } object or a callback). - validate: Validates the field when called.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the field is disabled.

data-touched

Present when the field has been touched.

data-dirty

Present when the field's value has changed.

data-valid

Present when the field is valid.

data-invalid

Present when the field is invalid.

data-filled

Present when the field is filled.

data-focused

Present when the field control is focused.

<Field.Label>

An accessible label that is automatically associated with the field control. Renders a <label> element.

PropTypeDefault
idstring—

The id of the element. A generated id is used by default.

nativeLabelbooleantrue

Whether the component renders a native <label> element when replacing it via the render prop. Set to false if the rendered element is not a label (for example, <div>).

This is useful to avoid inheriting label behaviors on <button> controls (such as <Select.Trigger> and <Combobox.Trigger>), including avoiding :hover on the button when hovering the label, and preventing clicks on the label from firing on the button.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an invalid state.

data-touched

Present when the field has been touched.

data-dirty

Present when the field's value has changed.

data-filled

Present when the field is filled.

data-focused

Present when the field control is focused.

<Field.Error>

An error message displayed if the field control fails validation. Renders a <div> element.

PropTypeDefault
idstring—

The id of the element. A generated id is used by default.

matchboolean | keyof ValidityState—

Determines whether to show the error message according to the field's ValidityState. Specifying true will always show the error message, and lets external libraries control the visibility.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an invalid state.

data-touched

Present when the field has been touched.

data-dirty

Present when the field's value has changed.

data-filled

Present when the field is filled.

data-focused

Present when the field control is focused.

<Field.Description>

A paragraph with additional information about the field. Renders a <p> element.

PropTypeDefault
idstring—

The id of the element. A generated id is used by default.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an invalid state.

data-touched

Present when the field has been touched.

data-dirty

Present when the field's value has changed.

data-filled

Present when the field is filled.

data-focused

Present when the field control is focused.

<Field.Control>

The form control to label and validate. Renders an <input> element.

You can omit this part and use any Solid Base input component instead. For example, Input, Checkbox, or Select, among others, will work with Field out of the box.

PropTypeDefault
idstring—

The id of the input. A generated id is used by default.

namestring—

Identifies the field when a form is submitted. The name prop on <Field.Root> takes precedence over this.

disabledbooleanfalse

Whether the component should ignore user interaction. The disabled prop on <Field.Root> takes precedence over this.

onValueChange| ((value: string, eventDetails: FieldControl.ChangeEventDetails) => void)—

Callback fired when the value changes. Use when controlled.

defaultValuestring | number | readonly string[]—

The default value of the input. Use when uncontrolled.

valuestring | number | readonly string[] | null—

The value of the input. Use when controlled.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an invalid state.

data-touched

Present when the field has been touched.

data-dirty

Present when the field's value has changed.

data-filled

Present when the field is filled.

data-focused

Present when the field control is focused.

<Field.Validity>

Used to display a custom message based on the field's validity. Requires children to be a function that accepts field validity state as an argument.

<Field.Item>

Groups individual items in a checkbox group or radio group with a label and description. Renders a <div> element.

PropTypeDefault
disabledbooleanfalse

Whether the wrapped control should ignore user interaction. The disabled prop on <Field.Root> takes precedence over this.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an invalid state.

data-touched

Present when the field has been touched.

data-dirty

Present when the field's value has changed.

data-filled

Present when the field is filled.

data-focused

Present when the field control is focused.