# Time Picker

A time you can type, like 930 or 9:30pm, or pick from a list every 15 minutes, in the same box and states as Input. Built on ui-sdk's Combobox: typing filters the list, Enter takes a time that isn't in it, and the value is "HH:mm".

- Beta: ready to use; its API can still change in a minor release.
- Category: Form fields

```tsx
import { Field, TimePicker } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it for a time of day, like a meeting's start or a court appearance. Pair it with `DatePicker` when the date is picked too.
- Do: Set `step` to the interval appointments book on, like 30 for the half hour. The default is 15 minutes.
- Do: Let people type the time. "930" and "9:30pm" both work, and Enter takes a time that isn't in the list, so a 9:40 start doesn't have to fit the step.
- Do: Put it in a `Field` with a label. Outside a field, name it with `aria-label`.
- Don't: Use it for a duration, like time spent on a matter. It holds a time of day as "HH:mm"; use `Input` with a `suffix` for the unit.

## Keyboard

| Keys | What they do |
| --- | --- |
| Typing | Filters the list of times. |
| Arrow keys | Move the highlight while focus stays in the input. |
| Enter | Chooses the highlighted time, or takes a typed time between the steps. |
| Escape | Closes the list and keeps focus in the input. |

## Related

- [Date Picker](./date-picker.md)
- [Combobox](./combobox.md)
- [Select](./select.md)

## Examples

What the examples import:

```tsx
import { Fragment, useState } from "react";
import { Field, TimePicker } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const sizes = ["xs", "sm", "md", "lg"] as const;

function StartDemo() {
  const [start, setStart] = useState("09:30");
  return (
    <div className="flex w-64 flex-col gap-3">
      <Field label="Start time">
        <TimePicker value={start} onChange={setStart} required />
      </Field>
      <span className="text-small text-neutral-text-muted">Value: {start}</span>
    </div>
  );
}
```

### Typing or picking

Press the chevron or Down for the list, which opens on the chosen time. Type 937 and press Enter for a time between the steps.

```tsx
<StartDemo />
```

### Sizes

```tsx
<div className="grid w-64 grid-cols-[auto_1fr] items-center gap-x-4 gap-y-3">
  {sizes.map(size => (
    <Fragment key={size}>
      <span className="text-small text-neutral-text-muted">{size}</span>
      <TimePicker
        size={size}
        aria-label={`Start time, ${size}`}
        defaultValue="09:30"
      />
    </Fragment>
  ))}
</div>
```

### States

Read-only drops the chevron. A 24 hour clock shows 09:30.

```tsx
<div className="grid w-full max-w-2xl grid-cols-2 gap-x-6 gap-y-6">
  <Field label="Error" error="Please enter a valid time">
    <TimePicker defaultValue="09:30" />
  </Field>
  <Field label="Read-only">
    <TimePicker defaultValue="09:30" readOnly />
  </Field>
  <Field label="Disabled" disabled>
    <TimePicker defaultValue="09:30" />
  </Field>
  <Field label="24 hour">
    <TimePicker defaultValue="21:30" hourCycle={24} />
  </Field>
</div>
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` |  | The time as "HH:mm", like "09:30". |
| `defaultValue` | `string` |  | The time it starts with, when uncontrolled. |
| `onChange` | `(value: string) => void` |  | Runs with "HH:mm" when a time is picked, or typed and committed with Enter. |
| `step` | `number` | `15` | Minutes between the times in the list. |
| `hourCycle` | `12 \| 24` | `12` | 12 shows "9:30 am", 24 shows "09:30". |
| `size` | `TextSize` | `"md"` | xs 28, sm 32, md 36 (forms, the default), lg 40, as Input. |
| `placeholder` | `string` |  | Grey hint text while it's empty. Never a replacement for a label. |
| `aria-label` | `string` |  | Names the field outside a Field. |
| `id` | `string` |  | The control's id, for a label's htmlFor or aria-describedby. |
| `name` | `string` |  | The field's name when a form submits. |
| `disabled` | `boolean` |  | Shows it as unavailable and stops it responding. |
| `readOnly` | `boolean` |  | Shows the value without letting it change. Still focusable. |
| `required` | `boolean` |  | Marks it required: the label gets its asterisk and the form checks it. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
