@dax-ui/text-field
v0.8.0
Published
dax-ui TextField: text input and textarea with a label above, icons, character counter, locked state and custom colors
Maintainers
Readme
@dax-ui/text-field
Single- or multi-line text input in the filled or outline style: label above it, icons
inside, a hint with an ⓘ icon and a character counter under it, and error, disabled, locked
and password states. Wraps MUI's TextField; medium height and full width by default.
Every color comes from the app's theme (createAppTheme({ colors })), so each app gets its
own brand. Set an app's defaults (variant, size, radius, colors, helper icon) once with
FieldProvider from @dax-ui/field.
Installation
pnpm add @dax-ui/text-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 { TextField } from '@dax-ui/text-field';
// Controlled
const [email, setEmail] = useState('');
<TextField label="Email" type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
// Filled style (outline is the default)
<TextField label="Email" variant="filled" />
// Hint, and an error message that replaces it
<TextField label="Email" helperText="We'll send the invite here" error={isValid ? false : 'Enter a valid email address'} />
// Required
<TextField label="Company name" required />
// Password: comes with a show-password button
<TextField label="Password" type="password" />
// Icons inside, interactive content at the end
<TextField label="Search" startIcon={<SearchIcon />} endAdornment={<IconButton aria-label="Clear"><XIcon /></IconButton>} />
// Text area with a counter
<TextField label="Notes" multiline minRows={3} maxRows={8} showCount maxLength={2000} />
// Read-only with the disabled look, still submitted with the form
<TextField label="Site" locked defaultValue="ABC-01" />
// Uncontrolled, inside a form
<form onSubmit={handleSubmit}>
<TextField label="Name" name="name" defaultValue="Ada" />
</form>One look per app
import { FieldProvider } from '@dax-ui/field';
// Every TextField and Dropdown inside gets these defaults; a field's own props still win.
<FieldProvider variant="filled" size="small" radius={8} helperIcon={null}>
<App />
</FieldProvider>Colors come from the theme. To restyle fields only, pass colors to FieldProvider or to one
field, e.g. colors={{ focusBorder: '#6d28d9', focusBackground: '#f5f3ff' }}
(see FieldColors in @dax-ui/field).
Props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| 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). |
| defaultValue | unknown | | Initial value, for an uncontrolled field. |
| disabled | boolean | false | Disables the field. |
| endAdornment | ReactNode | | Interactive content inside the input, after the text, e.g. a clear <IconButton aria-label="Clear">. |
| endIcon | ReactNode | | Decorative icon inside the input, after the text, e.g. <CalendarIcon /> or <ClockIcon />. Hidden from screen readers. |
| 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. |
| fieldSlotProps | FormFieldSlotProps | | Style overrides for the label row, label, helper text and counter. Ignored with labelPlacement="floating". |
| fullWidth | boolean | true | Stretches the field to the full width of its container. |
| 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 input's accessible name. Ignored with labelPlacement="floating". |
| 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. Ignored with labelPlacement="floating". |
| labelIcon | ReactNode | | Decorative icon before the label text, e.g. <CalendarIcon />. Sized to 16px. |
| labelPlacement | "top" \| "start" \| "floating" | top | Where the label sits: top (above the input), start (beside it) or floating (MUI's label inside the border, the earlier default). |
| labelWidth | string \| number | 160 | Width of the label column when labelPlacement is start. A number is px. |
| locked | boolean | false | Read-only with the disabled look: grey background and text. Unlike disabled, the input stays focusable, readable by screen readers and is submitted with the form. For values fixed after creation, e.g. "Site". |
| maxLength | number | | Maximum number of characters the input accepts. Shown in the counter with showCount. |
| maxRows | string \| number | | Maximum number of rows of a multiline textarea before it scrolls. |
| minRows | string \| number | | Minimum number of rows of a multiline textarea. |
| multiline | boolean | false | Renders a growing <textarea> instead of an <input>. Use minRows / maxRows to bound it. |
| name | string | | Name submitted with the form. |
| onChange | ChangeEventHandler<HTMLInputElement \| HTMLTextAreaElement, Element> | | Called on every change; read the new value from event.target.value. |
| 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. |
| passwordToggle | boolean | true | With type="password", adds a button at the end of the input that shows or hides the password. |
| passwordToggleIcons | PasswordToggleIcons | | Replaces the eye icons of the show-password button. |
| passwordToggleLabel | string | Show password | Accessible name of the show-password button. It is a toggle (aria-pressed), so the name stays the same. |
| placeholder | string | | Hint shown inside the empty input. Don't use it instead of label. |
| radius | number | 12 | Corner radius of the input, in px. |
| readOnly | boolean | false | Makes the input read-only while keeping its normal look. |
| 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. Ignored with labelPlacement="floating" (MUI shows *). |
| showCount | boolean | false | Shows a character counter under the input on the right: "12/2000" with maxLength, "12" without. |
| size | "small" \| "medium" \| "large" | medium | Input height: small (40px), medium (52px) or large (60px). With labelPlacement="floating" the default is small, and large is the same as medium (56px). |
| startAdornment | ReactNode | | Interactive content inside the input, before the text, e.g. a currency <select>. |
| startIcon | ReactNode | | Decorative icon inside the input, before the text, e.g. <SearchIcon />. Hidden from screen readers. |
| style | CSSProperties | | Inline styles for the field's root element. |
| sx | SxProps<Theme> | | Style overrides for the field's root element. |
| type | HTMLInputTypeAttribute | text | HTML input type, e.g. email, password, number. password adds a show-password button (see passwordToggle). |
| value | unknown | | Current value, for a controlled field. Use with onChange. |
| variant | "filled" \| "outline" | outline | Input style: outline (surface background with a border) or filled (tinted background, no border until focused or in error). Set it for a whole app with <FieldProvider variant="filled">. |
Any other MUI TextField prop (autoComplete,
inputRef, slotProps, ...) is passed through to MUI. sx, className and style go on the
field's root element. ref is forwarded to the root element.
