@akhatmirzo/ui

Field

Wraps a form control with a label, hint and error message, and wires up ARIA automatically.

Tasdiqlash kodi SMS orqali yuboriladi

+998

Email manzil noto'g'ri

Import

import { Field } from '@akhatmirzo/ui';

When to use

  • Around every form control — Input, Textarea, Select, Checkbox, RadioGroup, Switch and the rest. No more hand-written id, htmlFor, aria-describedby, aria-invalid or aria-required.
  • Pass errors to error, not to a separate <p> — that way the control turns red and screen readers announce the message.
  • Keep description short — a format, a limit, or why the field is needed. Move longer text into the page.
  • One Field, one control. Don't put two inputs under a single label.

Validation

When error is not empty, the field switches to its error state: the control gets aria-invalid, its border turns red and the message appears below with an icon. Errors live in an aria-live region, so screen readers announce them as they appear. Try submitting the form empty.

Horizontal layout

orientation="horizontal" puts the label on the left (160px) and the control on the right. Useful for settings pages and wide forms.

Chekda ko'rinadi

Disabled

disabled on Field disables the control inside it too and dims the label.

Hudud administrator tomonidan belgilanadi

Your own control (useFieldControl)

Every form component in the library connects to Field through the useFieldControl hook. You can connect your own control (a native <select>, a third-party widget) the same way: spread controlProps onto the element and the rest is automatic.

Shaharni tanlang

useFieldControl also works outside a Field — it then generates its own id.

Props

Field accepts every other <div> attribute.

PropTypeDefaultDescription
childrenrequiredReactNode—The control (Input, Select, Checkbox ...)
labelReactNode—Field name, linked to the control with <label htmlFor>
descriptionReactNode—Hint text under the label (aria-describedby)
errorReactNode—When set, the control becomes invalid and the message shows with an icon
requiredbooleanfalseAdds * to the label and aria-required to the control
disabledbooleanfalseDisables the control inside as well
orientation'vertical' | 'horizontal''vertical'Label above or to the left

useFieldControl

function useFieldControl(options?: FieldControlOptions): FieldControlResult;

options are the control's own values; they're merged with the ones coming from Field (invalid, required and disabled are combined with a logical OR).

PropTypeDefaultDescription
idstring—Own id — when set, the label points to it
invalidboolean—Error state
requiredboolean—Required field
disabledboolean—Disabled
aria-describedbystring—Extra description ids, appended to the Field's own
aria-invalidboolean | 'true' | 'false' | 'grammar' | 'spelling'—true or 'true' works like invalid

Return value:

PropTypeDefaultDescription
idstring—The id to put on the control
labelIdstring | undefined—The Field label's id — for aria-labelledby where htmlFor doesn't work
invalidboolean—Resolved error state
requiredboolean—Resolved required state
disabledboolean—Resolved disabled state
controlProps{ id; 'aria-describedby'?; 'aria-invalid'?: true; 'aria-required'?: true }—Attributes to spread directly onto the control

Data attributes

AttributeValue
data-slotfield
data-orientationvertical · horizontal
data-invalidPresent when error set
data-disabledPresent when disabled

Accessibility

  • Linked label. <label htmlFor> points to the control's id; if you give the control your own id, the label follows it.
  • Hint and error are read through aria-describedby — when the control gets focus, screen readers announce them after the label.
  • Error announcements. The message sits in an aria-live="polite" region that is always in the DOM, so even the first error gets announced.
  • Required fields. The * is aria-hidden; the control gets aria-required instead.
  • <div>-based controls like RadioGroup can't use htmlFor, so they get aria-labelledby from labelId automatically.

On this page