Functional Weave
Code in Python

react.form.date-field@1.0.0

README.md

2,297 bytes · view raw

# react.form.date-field

The browser's own date picker, `<input type="date">`, labelled and described
the GOV.UK way, with its value checked by `form.iso-date`.

```tsx
"use client";
import { DateField } from "#fune/react.form.date-field@^1";

<DateField id="start" label="Start date" required
  rules={{ today, timing: "future-or-today", notBefore: null, notAfter: "2027-12-31" }}
  value={start} onChange={setStart} showError={submitted} />
```

- **Timezone-safe.** The picker's value is always `YYYY-MM-DD` (or `""`),
  whatever the person's locale displays, and `validateIsoDate` compares it as
  a calendar date: no `Date` object, no UTC conversion, no off-by-one day for
  someone east or west of the server. `today`, for a past or future rule, is
  the caller's: work it out in the person's time zone (for example with
  `Intl.DateTimeFormat("en-CA", { timeZone })`), never from the server's clock.
- **min and max.** `rules.notBefore` and `rules.notAfter` become the picker's
  `min` and `max`, so the calendar greys out the dates it would refuse, and
  the same rules give the message if one is typed anyway.
- **Messages** are `form.date-parts`' wording ("Start date must be today or in
  the future", "Start date must be a real date"), named by `messageLabel`, or
  the label when it is plain text.
- **When to use it.** GOV.UK recommends three boxes (`react.form.date-input`)
  for a date people already know, such as a date of birth, and a picker only
  for one being chosen, such as an appointment.
- `showError` turns the check on (after touch or submit); `error` shows the
  server's message instead. `aria-describedby` names the hint then the
  error, and `aria-invalid="true"` is set only while an error shows.

It is a client component. Classes: those of `react.form.form-field`, plus
`fune-input`, `fune-input--error` and `fune-date-field`.

Sources: GOV.UK Design System, "Dates" pattern
https://design-system.service.gov.uk/patterns/dates/ ; HTML, the date state
of the input element https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date) .

## Notices

Portions derived from GOV.UK Frontend (https://github.com/alphagov/govuk-frontend), Copyright (c) 2017 Crown Copyright (Government Digital Service), under the MIT License; the full notice is in NOTICE.