# Time Input

逐段输入时间，时钟跟随用户语言的写法。

把 Time Input 用于一天中的某个时间，比如会议时间或开门时间。它和 [Date Input](/docs/svelte/components/date-input) 一样，为时、分各设一个分段，12 小时制下还有 AM 或 PM。

```svelte title="time.svelte"
<script lang="ts">
  import { Field, FieldLabel } from "@ui-sdk/svelte/field"
  import { TimeInput } from "@ui-sdk/svelte/time-input"
</script>

<Field>
  <FieldLabel>Meeting time</FieldLabel>
  <TimeInput defaultValue="09:30" />
</Field>
```

点击小时并输入 230，然后输入 P。在某个部分上按 ArrowUp 或 ArrowDown，可以按步进调整它。

## 安装
```bash
npm install @ui-sdk/svelte @ui-sdk/core
```

## 组合
用下面的组合构建 `TimeInput`：

```text
TimeInput
├── TimeInputSegment
└── TimeInputSegment
```

`TimeInput` 渲染一个分组，时间的每一部分对应一个 `TimeInputSegment`，分段之间的文本也在其中。

## 用法
把输入框放进 [Field](/docs/svelte/components/field)，并给它一个标签：

```svelte
<script lang="ts">
  import { Field, FieldLabel } from "@ui-sdk/svelte/field"
  import { TimeInput } from "@ui-sdk/svelte/time-input"
</script>

<Field>
  <FieldLabel>Meeting time</FieldLabel>
  <TimeInput defaultValue="09:30" />
</Field>
```

值是 `"14:30"` 这样的 24 小时制时间，和原生时间输入框一样，无论输入框显示哪种时钟。输入、步进和粘贴的行为与 [Date Input](https://ui-sdk.cn/docs/svelte/components/date-input#usage) 相同。

## 时钟与秒
输入框按 locale 的时钟显示时间。把 `hourCycle` 设为 `12` 或 `24` 可以选择一种，把 `granularity` 设为 `"second"` 可以加上秒，设为 `"hour"` 则只显示小时。

```svelte title="hour-cycle.svelte"
<script lang="ts">
  import { Field, FieldLabel } from "@ui-sdk/svelte/field"
  import { TimeInput } from "@ui-sdk/svelte/time-input"
</script>

<div data-slot="stack">
  <Field>
    <FieldLabel>12-hour clock</FieldLabel>
    <TimeInput hourCycle={12} defaultValue="18:45" />
  </Field>
  <Field>
    <FieldLabel>24-hour clock</FieldLabel>
    <TimeInput hourCycle={24} defaultValue="18:45" />
  </Field>
  <Field>
    <FieldLabel>With seconds</FieldLabel>
    <TimeInput granularity="second" hourCycle={24} defaultValue="18:45:30" />
  </Field>
</div>
```

同一个傍晚时间在 12 小时制、24 小时制和带秒时的样子。三者提交的都是 24 小时制的值。

## 最小与最大
`min` 和 `max` 接受 `"09:00"` 这样的时间。超出范围的时间无效：输入框会设置 `data-invalid`，它的表单也不会提交。

## 表单
隐藏的输入框会以 `name` 提交时间。设置 `required` 后，表单需要先有时间才能提交；把 `form` 设为页面中另一个表单的 id，就能用那个表单提交。[Date Input 的表单示例](https://ui-sdk.cn/docs/svelte/components/date-input#form)里就有一个时间。

## 受控与控制器
用 `bind:value` 绑定值，或用 `createTimeInputController()` 驱动输入框，做法与 [Date Input](https://ui-sdk.cn/docs/svelte/components/date-input#controlled) 相同。

## 样式
像 [Date Input](https://ui-sdk.cn/docs/svelte/components/date-input#styling) 那样设置样式。AM/PM 分段会设置 `data-type="dayPeriod"`。

每个部件都有一个 `data-slot`，比如 `time-input-segment`，它的状态属性列在[数据属性](https://ui-sdk.cn/docs/svelte/components/time-input#data-attributes)下。

## 数据属性
### TimeInput
`[data-slot="time-input"]`

- `data-disabled`: Present when disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when read-only.
- `data-required`: Present when required.
- `data-invalid`: Present while the time is before `min` or after `max`.
- `data-empty`: Present while every segment is empty.

### TimeInputSegment
`[data-slot="time-input-segment"]`

- `data-type` (`hour`, `minute`, `second`, `dayPeriod`, `literal`): The segment's unit, or `literal` for the text between segments.
- `data-disabled`: Present when disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when read-only.
- `data-required`: Present when required.
- `data-invalid`: Present while the time is before `min` or after `max`.
- `data-empty`: Present while the segment is empty. It shows its placeholder.

## API 参考
属性（props）与下面相同，都作为 Svelte props。`value` 可以绑定。

### TimeInput
- `value?` (`string | null`): The value: a time like `"14:30"`, or `"14:30:15"` with seconds, or `null`. Pass it to control the input.
- `defaultValue?` (`string | null`): The value it starts with, when uncontrolled, and goes back to when its form resets.
- `onValueChange?` (`((value: string | null, details: ChangeDetails<TimeInputValueChangeReason>) => void)`): Runs when the value wants to change: once every segment holds something, and again with `null` when one is emptied. `details.cancel()` keeps the value.
- `granularity?` (`TimeInputGranularity`): The smallest unit it shows.
- `hourCycle?` (`HourCycle`): A 12 or 24 hour clock. Defaults to the locale's.
- `min?` (`string`): The earliest time, like `"09:00"`. A time before it is invalid.
- `max?` (`string`): The latest time, like `"17:30"`. A time after it is invalid.
- `locale?` (`string`): The language whose separators and clock it shows. Defaults to the page's.
- `disabled?` (`boolean`): Whether it can't be used.
- `readOnly?` (`boolean`): Whether the value can be seen but not changed.
- `required?` (`boolean`): Whether a time must be entered before its form submits.
- `name?` (`string`): The name the value submits under.
- `form?` (`string`): The id of the form it belongs to, when it isn't inside it.
- `controller?` (`object`): Drives it from anywhere. Create one with `createTimeInputController()`.
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.

### 状态
- `value` (`string | null`): The value, or `null` while empty or partly filled.
- `segments` (`DateInputSegmentData[]`): The segments in the order the locale shows them, with the text between them. Render one part for each.
- `disabled` (`boolean`): Whether it can't be used, by itself or by a `<fieldset>`.
- `readOnly` (`boolean`): Whether the value can be seen but not changed.
- `invalid` (`boolean`): Whether the value is before `min`, after `max` or an unavailable date.

### 原因
- `input-change` (`Event`): Typing, pasting or dropping changed a text field's value; in a combobox it can also open or close it, or Enter keeps the typed text as the value.
- `step-key` (`KeyboardEvent`): Arrow, Page Up, Page Down, Home or End stepped a slider or a number input.
- `paste` (`ClipboardEvent`): Files were pasted, like a screenshot, or text and tokens into a token input.
- `reset` (`none`): A form reset.
- `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
- `imperative` (`none`): A controller or `api` call.

### 控制器
- `getValue()` (`() => string | null`): The value, or `null` while empty or partly filled.
- `setValue(value, options?)` (`(value: string | null, options?: DateInputChangeOptions | undefined) => boolean`): Sets the value. Returns false when it isn't a value the input can show, or when canceled.
- `clear(options?)` (`(options?: DateInputChangeOptions | undefined) => boolean`): Empties every segment.
- `focus(segment?)` (`(segment?: string | undefined) => void`): Focuses a segment, like `"month"`, or the first one.

### 事件
回调先收到新值，然后收到详情。绑定的 `value` 只在 `onValueChange` 未取消变更时更新。

| 属性            | 所属        | 收到                                                              |
| --------------- | ----------- | ----------------------------------------------------------------- |
| `bind:value`    | `TimeInput` | 时间；为空或只输入一部分时为 null                                 |
| `onValueChange` | `TimeInput` | `(value, details)`：时间（被擦除后为 null），以及[原因](https://ui-sdk.cn/docs/svelte/components/time-input#reasons) |

## 无障碍
根部件是一个分组，名称来自它的 Field 标签或你的 `aria-label`。每个分段都是一个 spinbutton，由它的单位命名，比如“hour”或“AM/PM”，并带有它的值、范围和文本。按键与 [Date Input](https://ui-sdk.cn/docs/svelte/components/date-input#keyboard) 相同。

### 经过测试的行为
下面每一行都是一个自动化测试。

- Each unit is a spinbutton in the locale's order.
- Another locale orders the segments its own way.
- Typing fills each segment, moves on and reports the date once.
- A press on the box or between segments focuses the nearest segment.
- A digit that can't start a longer number moves on at once.
- A separator moves to the next segment.
- Typing with separators over a filled date or time keeps every segment.
- ArrowUp and ArrowDown step a segment and wrap around.
- PageUp and PageDown take bigger steps.
- Home and End go to a segment's first and last values.
- ArrowLeft and ArrowRight move between segments.
- Tab moves through the segments, then out.
- Backspace empties a segment, then moves back.
- A day past the end of a new month is moved back to its last day.
- Pasting a date fills every segment.
- A time takes hours, minutes and AM or PM.
- A 24-hour clock has no AM or PM.
- A date with a time shows its hour and minute too.
- A controller sets and clears the date.
- A canceled change keeps the date.
- The form submits the ISO date and time under their names.
- Enter submits the form with what was typed.
- Enter submits a form with no submit button, in a segment or the form's one text field.
- Resetting the form brings back the default date.
- A partly filled date can't be submitted.
- A required empty date can't be submitted.
- A date after max is invalid.
- An unavailable date is invalid.
- The field's label focuses the first segment.
- A native label for the input focuses the first segment wherever it's pressed. Web components only.
- A read-only date takes focus but doesn't change.
- A disabled date can't be focused.
- Digits committed through an input method fill the segment.

Runs in React、Vue 和 web components.
