# Field

## Usage

`Field` provides structure around a control:

- A label.
- A description.
- Required or optional indicators.
- A status message.

`TextInput` includes `Field`. Pass field props directly to `TextInput`.

```vue
<script setup lang="ts">
import { Field } from '@astryx-vue/core/Field'
import { shallowRef, useId } from 'vue'

const volume = shallowRef(50)
const inputId = `volume-${useId()}`
</script>

<template>
  <Field :input-id="inputId" label="Volume" description="Set playback volume.">
    <template #default="field">
      <input
        :id="field.inputId" v-model.number="volume" type="range"
        :aria-labelledby="field.labelId" :aria-describedby="field.describedBy"
      >
    </template>
  </Field>
</template>
```

### Anatomy

- **Label**: Text identifying the field. Always rendered for accessibility, optionally hidden visually.
- **Description**: Helper text between the label and input explaining what to enter.
- **Control slot**: A custom, native, or third-party control that does not already render a field shell.
- **Status message**: Inline validation feedback showing error, warning, or success with a message.
- **Optional/Required indicator**: Badge next to the label showing whether the field is optional or required.
- **Label tooltip**: Info icon at the end of the label with a tooltip explaining the field.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Wrap custom controls, native inputs, or third-party widgets that need labeling, helper text, optional/required indicators, or validation status. |
| Do | Always provide a label for accessibility, even if visually hidden with isLabelHidden. |
| Do | Use inputID and descriptionID to connect the label and description to the inner control with htmlFor and aria-describedby. |
| Don't | Nest Field around styled inputs such as TextInput, Typeahead, Select, DateInput, or TextArea; those components already render their own Field shell. |
| Don't | Use the attached status variant on non-bordered controls such as sliders, switches, or checkboxes; use detached so the message does not overlap the control. |
| Don't | Set both isOptional and isRequired on the same field. |
| Don't | Hide the label without providing an alternative way for the user to understand the field purpose. |

## Examples

### Required and optional

A required username next to an optional backup email.

```vue
<script setup lang="ts">
import { VStack } from '@astryx-vue/core/Stack'
import { TextInput } from '@astryx-vue/core/TextInput'
import { ref } from 'vue'

const username = ref('')
const email = ref('')
</script>

<template>
  <VStack :gap="4" width="100%" style="max-width: 320px">
    <TextInput v-model="username" label="Username" is-required placeholder="Enter your username" />
    <TextInput v-model="email" label="Backup email" is-optional placeholder="you@example.com" />
  </VStack>
</template>
```

### Validation states

Error, warning, and success on email, username, and API key.

```vue
<script setup lang="ts">
import { VStack } from '@astryx-vue/core/Stack'
import { TextInput } from '@astryx-vue/core/TextInput'
import { ref } from 'vue'

const email = ref('bad-email')
const username = ref('admin')
const apiKey = ref('sk-live-abc123')
</script>

<template>
  <VStack :gap="4" width="100%" style="max-width: 320px">
    <TextInput
      v-model="email"
      label="Email"
      description="Enter your work email"
      :status="{ type: 'error', message: 'Please enter a valid email address' }"
    />
    <TextInput
      v-model="username"
      label="Username"
      description="Choose a unique username"
      :status="{ type: 'warning', message: 'This username is reserved for administrators' }"
    />
    <TextInput
      v-model="apiKey"
      label="API Key"
      description="Paste your API key"
      :status="{ type: 'success', message: 'API key is valid and active' }"
    />
  </VStack>
</template>
```

### Description

Helper text under the label for the email and password fields.

```vue
<script setup lang="ts">
import { VStack } from '@astryx-vue/core/Stack'
import { TextInput } from '@astryx-vue/core/TextInput'
import { ref } from 'vue'

const email = ref('')
const password = ref('')
</script>

<template>
  <VStack :gap="4" width="100%" style="max-width: 320px">
    <TextInput
      v-model="email"
      label="Email"
      description="We'll send a confirmation link to this address"
      placeholder="you@example.com"
    />
    <TextInput
      v-model="password"
      label="Password"
      description="At least 8 characters with one uppercase letter"
      placeholder="Create a password"
    />
  </VStack>
</template>
```

## Association IDs

The default slot provides:

- `inputId`: the control ID.
- `labelId`: the label ID.
- `descriptionId`: the description ID.
- `statusId`: the status message ID.
- `describedBy`: the description and status message IDs.

1. Pass `inputId` to the control's `id`.
2. Pass `labelId` to the control's `aria-labelledby`.
3. Pass `describedBy` to the control's `aria-describedby`.

`Field` does not modify controls in its slot automatically.

## Group labels

1. Set `isGroupLabel` on `Field`.
2. Pass `labelId` to the group's `aria-labelledby`.

If the label is hidden, provide visible context that explains the field's purpose.

## Scope

The application is responsible for:

- Validating control values.
- Submitting forms.
- Disabling controls.

## Field Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | Required | Visible label. Still names the control when visually hidden. |
| `isLabelHidden` | `boolean` | false | Visually hides the label and description without removing accessible associations. |
| `description` | `string` | — | Helper text between the label and control. |
| `isDisabled` | `boolean` | false | Disabled state. Field/FieldLabel only style the label. Consumers disable their custom control. |
| `isOptional` | `boolean` | false | Shows an optional indicator and takes precedence over isRequired. |
| `isRequired` | `boolean` | false | Shows a required indicator; TextInput also sets aria-required. |
| `labelIcon` | `IconName \| Component` | — | Vue icon component before the label. The labelIcon slot takes precedence. |
| `labelTooltip` | `string` | — | Info button beside the label, available on hover, keyboard focus and click. |
| `optionalLabel` | `string` | Optional | Localized optional indicator. |
| `requiredLabel` | `string` | Required | Localized required indicator. |
| `inputId` | `string` | Required | ID of the labeled control. Must match the control id. |
| `labelId` | `string` | {inputId}-label | Label element ID, also used with aria-labelledby for groups. |
| `descriptionId` | `string` | {inputId}-description | Description ID, referenced by aria-describedby. |
| `isGroupLabel` | `boolean` | false | Uses a span to label groups such as radiogroup instead of a single-control label. |
| `status` | `FieldStatusInput` | — | Status type, optional message and custom messageId. |
| `statusVariant` | `'attached' \| 'detached' \| 'tooltip'` | attached | Tooltip hides the inline message but retains its description and announcement. The control supplies its own tooltip button. |
| `width` | `number \| string` | auto | Whole-field width. Numbers become pixels. |

## FieldLabel Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | Required | Visible label. Still names the control when visually hidden. |
| `isLabelHidden` | `boolean` | false | Visually hides the label and description without removing accessible associations. |
| `description` | `string` | — | Helper text between the label and control. |
| `isDisabled` | `boolean` | false | Disabled state. Field/FieldLabel only style the label. Consumers disable their custom control. |
| `isOptional` | `boolean` | false | Shows an optional indicator and takes precedence over isRequired. |
| `isRequired` | `boolean` | false | Shows a required indicator; TextInput also sets aria-required. |
| `labelIcon` | `IconName \| Component` | — | Vue icon component before the label. The labelIcon slot takes precedence. |
| `labelTooltip` | `string` | — | Info button beside the label, available on hover, keyboard focus and click. |
| `optionalLabel` | `string` | Optional | Localized optional indicator. |
| `requiredLabel` | `string` | Required | Localized required indicator. |
| `inputId` | `string` | Required | ID of the labeled control. Must match the control id. |
| `labelId` | `string` | {inputId}-label | Label element ID, also used with aria-labelledby for groups. |
| `descriptionId` | `string` | {inputId}-description | Description ID, referenced by aria-describedby. |
| `isGroupLabel` | `boolean` | false | Uses a span to label groups such as radiogroup instead of a single-control label. |

## FieldStatus Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `type` | `'error' \| 'warning' \| 'success'` | Required | Status meaning and color. |
| `message` | `string` | Required | Status text. An empty string hides the message box. |
| `variant` | `'attached' \| 'detached'` | attached | Attached overlaps a bordered control. Detached suits sliders, switches and similar controls. |
| `isVisuallyHidden` | `boolean` | false | Keeps only the accessible description and announcement when the control supplies its own tooltip. |

## Slots

Field requires a default slot receiving the following properties:

```ts
interface FieldSlotProps {
  inputId: string
  labelId: string
  descriptionId?: string
  statusId?: string
  describedBy?: string
}
```

- `Field` and `FieldLabel` provide a `labelIcon` slot.
- The `labelIcon` slot overrides the matching prop.
- The icon is decorative.
- `FieldStatus` has no slots. Pass its text through `message`.

## Status messages

```ts
interface FieldStatusInput {
  type: 'error' | 'warning' | 'success'
  message?: string
  messageId?: string
}
```

### Announcements

- `error` uses an `aria-live="assertive"` region.
- `warning` uses an `aria-live="polite"` region.
- `success` uses an `aria-live="polite"` region.
- When the message changes, the component announces it again.
- An empty message clears the announcement region.
- The visual message does not duplicate `aria-live`.

### Message layout

- `attached`: joins the message to the control. Place it beneath a bordered control inside `Field`.
- `detached`: separates the message from the control. The message includes a status icon.
- `Field` in `tooltip` mode: hidden text remains available for descriptions and announcements. The inner control supplies the tooltip button.

For a standalone `FieldStatus`, use `detached`.

## Events

These three components declare no custom events.

## Template refs

Each component exposes `element`:

| Component | Element |
| --- | --- |
| `Field` | The field's root `div`. |
| `FieldLabel` | The label element or group `span`. |
| `FieldStatus` | The visual message element. |

Before mounting, `element` is `null`.
Without a message, `FieldStatus.element` is `null`.

## Native attributes

| Component | Attribute target |
| --- | --- |
| `Field` | The field root. |
| `FieldLabel` | The label group. |
| `FieldStatus` | The message element. |

Pass these directly to the inner control:

- `name`.
- `required`.
- `disabled`.
- Control event listeners.

## Imports

```ts
import type { FieldProps, FieldSlotProps, FieldStatusInput } from '@astryx-vue/core/Field'
import { Field, FieldLabel } from '@astryx-vue/core/Field'
import { FieldStatus } from '@astryx-vue/core/FieldStatus'
import '@astryx-vue/themes/neutral.css'
```

## Theme

- `--astryx-spacing-1`: spacing between field contents.
- `--astryx-spacing-2`: padding for detached status messages.
- `--astryx-radius-element`: status message corner radius.
- `--astryx-color-text-secondary`: supporting text color.
- `--astryx-text-label-size`: label font size.
- `--astryx-text-supporting-size`: description and status message font size.

Status colors use the `TextInput` variables. See [TextInput validation states](/en/components/text-input/props#validation-states) for the complete list.
