# ChronoPicker

Crate: `libero`
Import: `use libero::{chrono::{NaiveDate, NaiveTime}, components::{DateLevel, ChronoPicker, DateRange}};`
Feature: `date-picker` (off by default, turns on `tabs`, `action-icon`, `use-drag`, `use-media-query`, `use-swipe`, `use-timers`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/form/date/chrono_picker.rs>
Index: [index.md](index.md) lists every other page
Description: One picker for every date and time value, from days, months and years to times, date-times and ranges of them.

One picker for every date and time value. The value's type picks what it draws.
`NaiveDate` a month of days, `NaiveTime` a clock, `NaiveDateTime` the day and
then the time, a `DateRange` of either a start and an end, and a `TimeDelta` a
duration, one column per part. Names come from the localization's `DateLocale`,
the first weekday from the provider's `Formats`. `DatePicker`, `MonthPicker`,
`YearPicker`, `TimePicker` and `DateRangePicker` take one value type and need no
turbofish.

## Usage

```rust
use dioxus::prelude::*;
use libero::{chrono::NaiveDate, components::ChronoPicker};

#[component]
fn Demo() -> Element {
    let mut date = use_signal(|| None::<NaiveDate>);

    rsx! {
        ChronoPicker {
            value: date() /* Option<NaiveDate> */,
            onchange: move |next| date.set(next),
        }
    }
}
```

## Props

### `ChronoPicker<V: DateValue>`

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `Option<V>` | - | The picked value. Its type picks what the picker draws, and the picker opens on its month, else today's. Pair it with `onchange`: a typed `value` alone does not name `V`, so store into a typed signal or use a turbofish. |
| `onchange` | `EventHandler<Option<V>>` | - | Called with the value to hold next. |
| `oncomplete` | `Callback<()>` | - | Fires once the last unit of a time, a date-time or a duration is picked, after its `onchange`: the last hand on the clock face, or Enter or a filled column in the last column. A date or a range of dates alone never fires it. |
| `level` | `DateLevel` | `Day` | Picks a `NaiveDate` as a day, a month (its first day) or a year (its January 1). Ignored for other values. |
| `min` | `V::Bound` | - | The earliest value that can be picked. For a range, the earliest end. |
| `max` | `V::Bound` | - | The latest value that can be picked. For a range, the latest end. |
| `exclude_date` | `Callback<NaiveDate, bool>` | - | Days that cannot be picked, on top of `min` and `max`. |
| `allow_deselect` | `bool` | `false` | Clicking the picked day again clears it. Only for a day. |
| `columns` | `usize` | `1, or 2 for a range above sm` | Months side by side, for a day or a range of days; below `sm` only as many as fit, at least one. Unset, a range below `sm` shows its months in one scrolling column under its ends. |
| `calendar` | `CalendarVariant` | `full` | A month of days, or `mini`, one row of days with buttons that page it. For a day or a date-time. |
| `days` | `usize` | `7` | Days in the mini calendar's row. |
| `variant` | `TimePickerVariant` | `analog` | A digital clock, `HH:MM` with a column to turn per part, or a clock face, for values with a time. |
| `with_seconds` | `bool` | `false` | Seconds: a column on the digital clock, a button and hand on the clock face. |
| `step` | `u8` | `5` | The minute step: minutes between the offered minutes. |
| `hour_step` | `u8` | `1` | Hours between the offered hours, counted from midnight. The clock face disables the hours between. |
| `second_step` | `u8` | `1` | Seconds between the offered seconds, with `with_seconds`. |
| `open_to` | `DateLevel` | `level` | The view a `NaiveDate` picker first shows, as `DateLevel::Year` to start from the years. A level below `level` shows `level`. |
| `presets` | `Vec<DatePreset<V>>` | - | Values offered as buttons beside the calendar, above it on a phone. Pressing one picks it and shows its month. For a day or a range of days. |
| `week_numbers` | `bool` | `false` | An ISO week number before each week of days, a row header. Not in the mini calendar. |
| `weekend_days` | `Vec<Weekday>` | `Saturday, Sunday` | The days marked `data-weekend` and tinted in the error colour. |
| `hide_weekdays` | `bool` | `false` | Hides the weekday names over the days. Screen readers still get them as column headers. |
| `hide_outside_days` | `bool` | `false` | Blanks instead of the neighbouring months' days. |
| `twelve_hour` | `bool` | `formats` | A 12-hour clock with AM and PM. Defaults to whether `Formats::time` is one. |
| `today` | `NaiveDate` | - | The day marked as today. Unset, the platform clock answers after mount on the web. Elsewhere no day is marked. |
| `size` | `Responsive<Size>` | `md` | Cell, option and font size. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. |
| `name` | `String` | - | Posts the value as ISO 8601 in a hidden input of that name. |
| `focusable` | `bool` | `true` | `false` keeps the picker out of the tab order, for a picker inside a dropdown whose input keeps focus. |
| `disabled` | `bool` | `false` | Shows the value but takes no pick: every cell is `aria-disabled` and out of the tab order, presses and keys do nothing, and a focused cell keeps its focus. The hidden `name` input posts nothing. A disabled `Fieldset` sets it too. |

Props that only some value types use are ignored by the rest, with a warning in
debug builds.

Like every component, it also takes the shared props `sx`, `class`, `style`,
`states`, `hidden_from`, `visible_from`, and any extra HTML attributes. The attributes land on the root.

## Style API

Style a part with the `parts` prop, or address it as `[data-slot='…']` in your
own CSS. The names are stable. [Style API in Styling](styling.md#style-api)
explains how parts work. Every picker takes `ChronoPickerPart` and draws only
the parts its value uses. A date field's dropdown is portaled, so style the
picker in it with the field's `dropdown_parts`.

| Part | `data-slot` | Description |
|---|---|---|
| `ChronoPickerPart::Header` | `header` | Calendar: the row over a month, a year or a decade, with paging buttons and title. |
| `ChronoPickerPart::Nav` | `nav` | Calendar: a paging button, in the header or beside the mini calendar's row. |
| `ChronoPickerPart::Title` | `title` | Calendar: the month, year or decade heading, a button that climbs a level. |
| `ChronoPickerPart::Months` | `months` | Days: the months side by side, or the mini calendar's row. |
| `ChronoPickerPart::Weekday` | `weekday` | Days: a weekday name over a month's columns. |
| `ChronoPickerPart::Day` | `day` | Days: a day button. |
| `ChronoPickerPart::Month` | `month` | Mini calendar: the month over a day's number. |
| `ChronoPickerPart::Blank` | `blank` | Days, `columns` over 1: an empty cell instead of a neighbour's day. |
| `ChronoPickerPart::Cells` | `cells` | Months and years: their grid. |
| `ChronoPickerPart::Cell` | `cell` | Months and years: a month or a year button. |
| `ChronoPickerPart::Strip` | `strip` | Mini calendar: the row of days between its paging buttons. |
| `ChronoPickerPart::Columns` | `columns` | Digital clock, duration: the columns. |
| `ChronoPickerPart::Spin` | `spin` | Digital clock, duration: one column, a spinbutton. |
| `ChronoPickerPart::Value` | `value` | Digital clock, duration: a column's value. |
| `ChronoPickerPart::Neighbour` | `neighbour` | Digital clock, duration: the faded values above and below a column's value. |
| `ChronoPickerPart::Separator` | `separator` | Digital clock: the `:` between columns. |
| `ChronoPickerPart::Unit` | `unit` | Duration: the unit after each column. |
| `ChronoPickerPart::Readout` | `readout` | Analog clock: the digits over the face, buttons that pick the hand. Scrolling range calendar: the picked range's two ends above the months. |
| `ChronoPickerPart::Face` | `face` | Analog clock: the face, a slider. |
| `ChronoPickerPart::Mark` | `mark` | Analog clock: a number on the face. |
| `ChronoPickerPart::Ticks` | `ticks` | Analog clock: the ring of ticks for steps finer than the marks. |
| `ChronoPickerPart::Hand` | `hand` | Analog clock: the hand. |
| `ChronoPickerPart::Pivot` | `pivot` | Analog clock: the dot the hand turns on. |
| `ChronoPickerPart::Presets` | `presets` | Calendar with `presets`: the column of preset buttons, a row on a phone. |
| `ChronoPickerPart::Preset` | `preset` | Calendar with `presets`: a preset button. |
| `ChronoPickerPart::WeekNumber` | `week-number` | Days with `week_numbers`: a week's number, and the empty cell over them. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Left` or `Right` | Days: the day before or after. Months and years: the cell before or after. Mini calendar: a day, sliding the row one day past its ends. |
| `Up` or `Down` | Days: a week earlier or later. Months and years: a row up or down. |
| `Home` or `End` | Days: the first or last day of the week. Months, years and the mini calendar: the row's ends. |
| `PageUp` or `PageDown` | Days: a month. Months: a year. Years: a decade. Mini calendar: `days` days. |
| `Shift+PageUp` or `Shift+PageDown` | Days: a year. |
| `Enter` or `Space` | Picks the cell. |
| `Enter` | On the month heading: climbs to the months, focus on the year heading. On the year heading: climbs to the years, focus into them. |
| `Up` or `Right` | Analog clock: the hand forward by an hour or `step` minutes. Digital column (`Up`): a step forward. |
| `Down` or `Left` | Analog clock: the hand back. Digital column (`Down`): a step back. |
| `PageUp` or `PageDown` | Analog clock: the hand a quarter face, 3 hours or 15 minutes. Digital column: a bigger step. |
| `Home` or `End` | Analog clock: the hand's first or last open value. Digital column: the first or last value. |
| `Digit` | Digital column: picks. A filled column moves on to the next. |
| `Enter` | Analog clock: from the hour to the minute. Digital column: to the next column. |
| `Tab` | Analog clock: to the next control. Digital column: to the next column. |

### Libero handles

- The days, the months and the years are one tab stop each. Tab lands on the
  picked cell, else today, else the first.
- Today's day, month and year carry `aria-current="date"`, which a screen
  reader reads as the current date.
- The decade heading is plain text, not a button: there is no level above it.
- After climbing to the months or years, a pick goes back down to the picker's
  level, with focus on the picked cell.
- The mini calendar's row of days is one tab stop. Its two buttons page the
  row by `days`.
- The clock face is one tab stop. Each digital column is a spinbutton and a
  tab stop of its own.
- The clock keys change the value at once and skip what `min` and `max` rule
  out. The wheel, a drag, or a press on the value above or below turns a
  digital column too.
- A date-time shows the day and the time as tabs above the picker. Picking the
  day moves focus into the clock.
- A disabled picker keeps its cells as they are, marked `aria-disabled` and
  out of the tab order, so a focused cell keeps its focus. It takes no key,
  press, wheel or drag, and its hidden input posts nothing. A disabled
  `Fieldset` disables it the same way.
- A date-time range has three tabs: the days, the start time and the end time.
  Each shows its value once picked, such as `12–14 Oct` or `09:00`, and moves
  on to the next when complete. The start and the end may be on different
  days.

### You must

- Off the web, pass `today`. Without it no day is marked as today.
- Set `focusable: false` only for a picker inside a dropdown whose input keeps
  focus.

### Example

A due-date picker, `ChronoPicker::<NaiveDate>`: Tab lands once in the month,
on the picked day. The arrows move a day or a week, PageDown a month, Enter
picks, and a screen reader reads today's cell as the current date.

## Theme defaults

`theme.chrono_picker` and `theme.time_picker` hold the size steps, the default
calendar and its `days`, and the default clock variant and minute `step`. The
month and weekday names and the button labels come from `Localization::date`, a
`DateLocale` (see [localization](localization.md)). The provider's `formats`,
`Formats::AMERICAN` by default or `Formats::GERMAN`, hold the first weekday and
the date and time patterns.
