# YearPickerTextField

Year field: type the year or choose it in a scrolling grid of years. The value is `selectedYear`, a number. Typing accepts four digits; a year outside `min` and `max` (and an unfinished one when the field loses focus) turns the field to the error state with `invalidText`, and the form refuses it. On desktop, focusing or clicking the field opens a `YearSelector` in a menu; Arrow Down moves the focus into it, Escape closes it and Tab leaves it. On screens up to 1024px wide the calendar button opens it in a bottom drawer instead (`mobileDrawer`). Choosing a year closes the menu (`closeOnSelect`). Its state is exposed as `data-state`, `data-disabled`, `data-readonly` and `data-open` for app CSS.

```ts
import { YearPickerTextField } from '@likable-hair/svelte';
```

Category: composed. Source: `src/lib/components/composed/forms/YearPickerTextField.svelte`.

## Examples

### Type or choose a year

Type four digits or pick the year in the grid, which opens on the chosen year. `min` and `max` limit both.

```svelte
<script lang="ts">
	import { YearPickerTextField } from '@likable-hair/svelte';

	let selectedYear = $state<number | undefined>(2026);
</script>

<div class="example">
	<YearPickerTextField
		label="Model year"
		min={1990}
		max={2030}
		clearable
		hint="From 1990 to 2030"
		bind:selectedYear
	/>
</div>

<style>
	.example {
		width: 340px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `selectedYear` (bindable) | `number` |  | The year. `undefined` while the field is empty or its text is not a valid year. |
| `open` (bindable) | `boolean` |  | Whether the menu of years is open. `undefined` counts as closed. |
| `min` | `number` | `1900` | First year that can be chosen or typed. |
| `max` | `number` | `2100` | Last year that can be chosen or typed. |
| `locale` | `string` |  | BCP 47 locale of the texts: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `label` | `string` |  | Visible label above the field, linked to the input. In the mobile drawer it is the title. |
| `hint` | `string` |  | Text below the field. With `state` it becomes the error or success message. |
| `state` | `'error' \| 'success'` |  | Validation state: colors the border and the hint and shows an icon. `error` also sets `aria-invalid`. Without it the field still shows the error state for an invalid year. |
| `invalidText` | `string` |  | Message shown below the field, and given to the form, when the typed year is not valid. Defaults to "Enter a valid year", in the language of `locale`. |
| `placeholder` | `string` |  | Native `placeholder`. |
| `disabled` | `boolean` | `false` | Native `disabled`. |
| `readonly` | `boolean` | `false` | Native `readonly`: the year is shown but cannot be changed. |
| `required` | `boolean` | `false` | Native `required`. |
| `id` | `string` |  | `id` of the input. Generated when missing, so the label always points to it. |
| `name` | `string` |  | Name of a hidden input that submits the year (empty when there is none). |
| `clearable` | `boolean` | `false` | Shows a button that empties the field. |
| `clearLabel` | `string` |  | Accessible name of the clear button. Defaults to "Clear", in the language of `locale`. |
| `closeOnSelect` | `boolean` | `true` | Closes the menu when a year is chosen. |
| `placement` | `'bottom-start' \| 'bottom' \| 'bottom-end' \| 'top-start' \| 'top' \| 'top-end' \| 'left-start' \| 'left' \| 'left-end' \| 'right-start' \| 'right' \| 'right-end'` | `'bottom-start'` | Side of the field where the menu opens. It flips when there is not enough space. |
| `mobileDrawer` | `boolean` | `true` | On screens up to 1024px wide, opens the years in a bottom drawer from the calendar button instead of a menu, and focusing the field does not open it. |
| `drawerTitle` | `string` |  | Title of the mobile drawer. Defaults to `label`. |
| `closeLabel` | `string` |  | Accessible name of the close button of the mobile drawer. Defaults to "Close", in the language of `locale`. |
| `openLabel` | `string` |  | Accessible name of the calendar button. Defaults to "Choose year", in the language of `locale`. |
| `input` (bindable) | `HTMLInputElement` |  | The input element. |
| `class` | `{ container?: string; label?: string; row?: string; field?: string; input?: string; hint?: string; picker?: string; }` | `{}` | Extra classes for each part. `picker` goes on the `YearSelector`. |

It also accepts the native attributes and events of `<input>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the label content. It stays linked to the input. |
| `hintSnippet` | `{ hint: string \| undefined }` | Replaces the hint below the field. |
| `stateIconSnippet` | `{ state: 'error' \| 'success' }` | Replaces the icon shown for the validation state. Render nothing to hide it. |
| `iconSnippet` |  | Replaces the calendar icon. The button around it keeps opening the years. |
| `chevronSnippet` |  | Replaces the chevron at the end of the field. |
| `prependSnippet` |  | Content outside the field, on the left. |
| `appendInnerSnippet` |  | Content inside the field, before the clear button and the chevron. |
| `appendSnippet` |  | Content outside the field, on the right. |
| `clearSnippet` |  | Replaces the icon of the clear button. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(event: { year: number \| undefined }) => void` | Called when the user changes the year: in the grid, by typing a whole valid year (or making it invalid again) and with the clear button. |

## CSS variables

For a single instance, pass them as style props: `<YearPickerTextField --year-picker-text-field-background="tomato">`. The `--year-picker-text-field-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--year-picker-text-field-border-color` | `--year-picker-text-field-default-border-color`: `var(--global-input-border-color)` |
| `--year-picker-text-field-hover-border-color` | `--year-picker-text-field-default-hover-border-color`: `var(--global-input-hover-border-color)` |
| `--year-picker-text-field-focus-border-color` | `--year-picker-text-field-default-focus-border-color`: `var(--global-input-focus-border-color)` |
| `--year-picker-text-field-focus-ring-color` | `--year-picker-text-field-default-focus-ring-color`: `var(--global-input-focus-ring-color)` |
| `--year-picker-text-field-hint-color` | `--year-picker-text-field-default-hint-color`: `var(--global-input-hint-color)` |
| `--year-picker-text-field-icon-size` | `--year-picker-text-field-default-icon-size`: `18px` |
| `--year-picker-text-field-gap` | `--year-picker-text-field-default-gap`: `6px` |
| `--year-picker-text-field-width` | `--year-picker-text-field-default-width`: `100%` |
| `--year-picker-text-field-max-width` | `--year-picker-text-field-default-max-width`: `none` |
| `--year-picker-text-field-error-color` | `--year-picker-text-field-default-error-color`: `var(--global-input-error-color)` |
| `--year-picker-text-field-success-color` | `--year-picker-text-field-default-success-color`: `var(--global-input-success-color)` |
| `--year-picker-text-field-label-color` | `--year-picker-text-field-default-label-color`: `var(--global-input-label-color)` |
| `--year-picker-text-field-label-font-size` | `--year-picker-text-field-default-label-font-size`: `var(--global-input-label-font-size)` |
| `--year-picker-text-field-label-font-weight` | `--year-picker-text-field-default-label-font-weight`: `var(--global-input-label-font-weight)` |
| `--year-picker-text-field-outer-gap` | `--year-picker-text-field-default-outer-gap`: `8px` |
| `--year-picker-text-field-icon-color` | `--year-picker-text-field-default-icon-color`: `var(--global-input-icon-color)` |
| `--year-picker-text-field-inner-gap` | `--year-picker-text-field-default-inner-gap`: `var(--global-input-gap)` |
| `--year-picker-text-field-height` | `--year-picker-text-field-default-height`: `var(--global-input-height)` |
| `--year-picker-text-field-padding` | `--year-picker-text-field-default-padding`: `0 var(--global-input-padding-x)` |
| `--year-picker-text-field-background` | `--year-picker-text-field-default-background`: `var(--global-input-background)` |
| `--year-picker-text-field-border-width` | `--year-picker-text-field-default-border-width`: `var(--global-input-border-width)` |
| `--year-picker-text-field-border-radius` | `--year-picker-text-field-default-border-radius`: `var(--global-input-radius)` |
| `--year-picker-text-field-box-shadow` | `--year-picker-text-field-default-box-shadow`: `var(--global-input-shadow)` |
| `--year-picker-text-field-focus-ring-width` | `--year-picker-text-field-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--year-picker-text-field-color` | `--year-picker-text-field-default-color`: `var(--global-input-color)` |
| `--year-picker-text-field-font-family` | `--year-picker-text-field-default-font-family`: `var(--global-font-family-mono)` |
| `--year-picker-text-field-font-size` | `--year-picker-text-field-default-font-size`: `var(--global-input-font-size)` |
| `--year-picker-text-field-font-weight` | `--year-picker-text-field-default-font-weight`: `400` |
| `--year-picker-text-field-placeholder-color` | `--year-picker-text-field-default-placeholder-color`: `var(--global-input-placeholder-color)` |
| `--year-picker-text-field-hint-font-size` | `--year-picker-text-field-default-hint-font-size`: `var(--global-input-hint-font-size)` |
| `--year-picker-text-field-disabled-background` | `--year-picker-text-field-default-disabled-background`: `var(--global-input-disabled-background)` |
| `--year-picker-text-field-disabled-opacity` | `--year-picker-text-field-default-disabled-opacity`: `var(--global-input-disabled-opacity)` |
| `--year-picker-text-field-menu-width` | `--year-picker-text-field-default-menu-width`: `300px` |
| `--year-picker-text-field-menu-padding` | `--year-picker-text-field-default-menu-padding`: `8px` |
