@dax-ui/field
v0.8.0
Published
dax-ui form field chrome: label above the control, required/optional markers, helper and error text, character counter, and shared input styles
Maintainers
Readme
@dax-ui/field
The chrome around a form control: a label above it (with an icon, a red required asterisk or
"(optional)"), and helper or error text and a character counter under it. FormField wires the
control's id, aria-describedby, aria-invalid and aria-required for you.
@dax-ui/text-field, @dax-ui/dropdown and @dax-ui/async-dropdown are built on it, so every
dax-ui field shares the same props, colors and defaults. Use it directly to give any other
control (a date picker, a color input, a group of radios) the same look.
Installation
pnpm add @dax-ui/fieldMUI and Emotion are included; the app only needs React 18 or 19.
One-time setup per app (skip if the app already uses dax-ui): install
@dax-ui/theme and wrap the app in its UIProvider.
pnpm add @dax-ui/themeUsage
import { FormField } from '@dax-ui/field';
// The child function receives the ids and state; spread `inputProps` on the control.
<FormField label="Title" required helperText="Shown on the calendar.">
{(field) => <input {...field.inputProps} />}
</FormField>
// "(optional)" after the label, an error message in place of the helper text
<FormField label="Department" optional error={missing && 'Choose a department'}>
{(field) => <select {...field.inputProps}>{/* … */}</select>}
</FormField>
// Character counter under the control, "12/2000"
<FormField label="Meeting Details" optional counter={{ current: text.length, max: 2000 }}>
{(field) => <textarea {...field.inputProps} value={text} onChange={(e) => setText(e.target.value)} />}
</FormField>
// Label icon, something at the end of the label row, the label beside the control
<FormField label="Date" labelIcon={<CalendarDays />} labelEnd={<a href="/help">Time zones</a>} labelPlacement="start">
{(field) => <DatePicker {...field.inputProps} />}
</FormField>A control nested deeper: useFormField
import { FormField, useFormField } from '@dax-ui/field';
function ColorInput() {
const field = useFormField(); // null outside a FormField
return <input type="color" {...field?.inputProps} />;
}
<FormField label="Accent">
<ColorInput />
</FormField>Radio buttons and checkboxes: group
group makes the root a role="group" named by the label, instead of a <label for>.
<FormField label="Repeat" group>
<RadioGroup>{/* … */}</RadioGroup>
</FormField>Colors
Every color is a CSS variable (--dax-field-<key>) set on the field's root, with defaults in
DEFAULT_FIELD_COLORS (and DEFAULT_FIELD_COLORS_DARK for dark mode). Override any with
colors:
<FormField label="Brand color" required colors={{ label: '#5b21b6', asterisk: '#be123c', helper: '#6d28d9' }}>
{(field) => <input {...field.inputProps} />}
</FormField>| Key | Used for |
| --- | --- |
| label, labelIcon, asterisk, optional | The label row |
| helper, error, errorRing, counter | Messages under the control, the error border and glow |
| text, placeholder, background, border, borderHover | The control (background and border: outline variant) |
| filledBackground, focusBackground, errorBackground | The filled variant at rest, focused and in error |
| focusBorder, focusRing | The focused control (focusRing is transparent, no glow, by default) |
| icon | Icons inside the control |
| disabledBackground, disabledText, disabledBorder | Disabled and locked controls |
The defaults read the app's theme colors, so a field follows the brand set with
createAppTheme({ colors }) without any colors prop.
Variants: filled and outline
The controls (TextField, Dropdown, AsyncDropdown) take variant:
| State | outline (default) | filled |
| --- | --- | --- |
| Rest | background with a border | filledBackground, no border |
| Focused | focusBorder | focusBackground and focusBorder |
| Error | error border | errorBackground and error border |
| Disabled | background and disabledBorder, faded text | disabledBackground, faded text |
filled uses the theme's background.base, the same color as the page, so place filled fields
on a surface (a card, dialog or panel), or set colors.filledBackground. MUI's floating label
(TextField labelPlacement="floating") sits on the border and is meant for outline.
Helper icon
Helper and error text start with an ⓘ icon. Replace it with helperIcon={<MyIcon />}, or
remove it with helperIcon={null}, on one field or for all of them through FieldProvider.
Defaults for a whole form: FieldProvider
import { FieldProvider } from '@dax-ui/field';
<FieldProvider variant="filled" colors={{ label: '#0b3554' }} size="small" optionalText="(not required)" radius={8}>
<TextField label="Title" required />
<Dropdown label="Department" optional options={departments} />
</FieldProvider>Providers nest (an inner one is merged over the outer one), and each field's own props win.
Building your own field
useFieldIds(id?) returns stable inputId, labelId, helperTextId and counterId, to wire
the parts yourself:
import { FieldCounter, FieldHelperText, FieldLabel, useFieldIds } from '@dax-ui/field';
const ids = useFieldIds();
<FieldLabel htmlFor={ids.inputId} id={ids.labelId} required>Subject</FieldLabel>
<input id={ids.inputId} aria-describedby={`${ids.helperTextId} ${ids.counterId}`} />
<FieldHelperText id={ids.helperTextId}>Shown as the email subject.</FieldHelperText>
<FieldCounter id={ids.counterId} current={subject.length} max={60} />getFieldInputSx({ variant, size, multiline, locked, radius }) returns the sx that gives a MUI
TextField (or anything rendering .MuiOutlinedInput-root) the dax-ui input look.
FormField props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| children | ReactNode \| ((field: FormFieldControl) => ReactNode) | | The control. Either an element wired up through useFormField(), or a function that receives the ids and state to wire it up: {(field) => <input {...field.inputProps} />}. |
| className | string | | Class name for the field's root element. |
| colors | Partial<FieldColors> | | Overrides any of the field's colors, e.g. { label: '#6d28d9', focusBorder: '#6d28d9' }. Unset keys keep the defaults (DEFAULT_FIELD_COLORS, or DEFAULT_FIELD_COLORS_DARK in dark mode). |
| counter | FieldCounterValue | | Character counter shown under the control on the right, e.g. { current: text.length, max: 2000 }. |
| disabled | boolean | false | Disables the field. |
| error | ReactNode | false | Error state: true turns the field red and marks the control aria-invalid; a message (string or node) does that and shows the message in place of helperText. |
| fullWidth | boolean | true | Stretches the field to the full width of its container. |
| group | boolean | false | The control is a group of inputs (radio buttons, checkboxes, a segmented control) rather than one input: the root becomes role="group" named by the label, and the label stops being a <label for>. |
| helperIcon | ReactNode | an info icon (ⓘ) | Icon before the helper or error text. Pass null to show the text without one. |
| helperText | ReactNode | | Text under the control: a hint normally. Replaced by the error message when error is a message. |
| hideLabel | boolean | false | Hides the label visually; it stays the control's accessible name. |
| id | string | | Id of the control, which the label points at. Generated when not set. |
| label | ReactNode | | Label shown above the control. Also the control's accessible name, so always set it (use hideLabel to hide it visually). |
| labelEnd | ReactNode | | Content at the far end of the label row, e.g. a "Forgot password?" link or an info tooltip. |
| labelIcon | ReactNode | | Decorative icon before the label text, e.g. <CalendarIcon />. Sized to 16px. |
| labelPlacement | "top" \| "start" | top | Where the label sits: top (above the control) or start (beside it, labelWidth wide). |
| labelWidth | string \| number | 160 | Width of the label column when labelPlacement is start. A number is px. |
| optional | boolean | false | Adds "(optional)" after the label, for forms where most fields are required. |
| optionalText | ReactNode | (optional) | Text shown after the label when optional is set. |
| required | boolean | false | Marks the field as required: a red asterisk after the label, and aria-required on the control. |
| requiredIndicator | ReactNode | * | What marks a required label. Hidden from screen readers (the control is announced as required instead). |
| slotProps | FormFieldSlotProps | | Style overrides for the label row, label, helper text and counter. |
| style | CSSProperties | | Inline styles for the field's root element. |
| sx | SxProps<Theme> | | Style overrides for the field's root element. |
ref is forwarded to the root element.
FieldLabel props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| children | ReactNode | | Label text. |
| className | string | | Class name for the label element. |
| component | ElementType | | Element to render. Defaults to label when htmlFor is set, else span. |
| disabled | boolean | false | Dims the label for a disabled control. |
| htmlFor | string | | Id of the control this labels (rendered as for). |
| icon | ReactNode | | Decorative icon before the text. |
| id | string | | Id of the label element. |
| optional | boolean | false | Shows optionalText after the text. |
| optionalText | ReactNode | (optional) | Text shown when optional is set. |
| required | boolean | false | Shows the required indicator after the text. |
| requiredIndicator | ReactNode | * | What marks a required label. Hidden from screen readers. |
| sx | SxProps<Theme> | | Style overrides for the label element. |
FieldHelperText props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| children | ReactNode | | The hint or error message. |
| className | string | | Class name for the element. |
| error | boolean | false | Shows the text in the error color. |
| icon | ReactNode | | Decorative icon before the text, sized to 14px. |
| id | string | | Id, referenced by the control's aria-describedby. |
| sx | SxProps<Theme> | | Style overrides for the element. |
FieldCounter props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| current (required) | number | | Characters (or items) used so far. |
| className | string | | Class name for the element. |
| getAccessibleText | ((current: number, max: number) => string) | (current: number, max: number \| undefined) => max === undefined ? ${current} characters:${current} of ${max} characters used`` | What screen readers hear, built from current and max. |
| id | string | | Id, referenced by the control's aria-describedby. |
| max | number | | The limit. Leave out to show the count alone. Over the limit, the counter turns error colored. |
| sx | SxProps<Theme> | | Style overrides for the element. |
FieldProvider props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| children | ReactNode | | The form, or any part of the app, whose fields get these defaults. |
| colors | Partial<FieldColors> | | Colors for every field inside, merged under each field's own colors. |
| helperIcon | ReactNode | | Default icon before helper and error text; null for none. |
| labelPlacement | "top" \| "start" | | Default labelPlacement. |
| labelWidth | string \| number | | Default labelWidth (with labelPlacement="start"). |
| optionalText | ReactNode | | Default text after optional labels. |
| radius | number | | Default corner radius of controls, in px. |
| requiredIndicator | ReactNode | | Default required indicator. |
| size | "small" \| "medium" \| "large" | | Default size of controls. |
| variant | "outline" \| "filled" | | Default variant of controls (filled or outline). |
Other exports
| Export | What it is |
| --- | --- |
| useFormField() | Inside a FormField: the ids and state to wire a control (FormFieldControl), else null |
| useFieldIds(id?) | Stable ids for the parts of a field (FieldIds) |
| useFieldDefaults() | The defaults set by the nearest FieldProvider |
| joinIds(...ids) | Joins the ids that are set into an aria-describedby value |
| getFieldInputSx(options) | sx giving a MUI TextField the dax-ui input look |
| DEFAULT_FIELD_COLORS, DEFAULT_FIELD_COLORS_DARK | Default colors (light and dark mode) |
| fieldCssVar | var(--dax-field-<key>, <default>) for every color, for your own styles |
| fieldColorVars(colors), colorSchemeVars(...), colorVarName(prefix, key) | sx callbacks that set the color variables, and the variable name for a color key |
| FIELD_SIZES, DEFAULT_FIELD_RADIUS | Control heights and font sizes per size; the default radius |
| InfoIcon | The default helper-text icon (ⓘ) |
| hasContent, errorMessage, resolveHelperIcon, visuallyHidden | Small helpers shared by the dax-ui fields |
| Types | FieldChromeProps (props every field shares), FieldColors, FieldSize, FieldVariant, FieldLabelPlacement, FieldCounterValue, FormFieldControl, FormFieldSlotProps, FieldDefaults |
