Functional Weave
Code in Python

react.form.date-field@1.0.0

impl/typescript.tsx

2,832 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

"use client";

import { useId, useState, type JSX, type ReactNode } from "react";
import type { DateRules } from "./form_date_parts.ts";  ← from form.date-parts ^1.0.0 · built alongside by fune
import { validateIsoDate } from "./form_iso_date.ts";  ← from form.iso-date ^1.0.0 · built alongside by fune
import { FormField, fieldIds } from "./react_form_form_field.ts";  ← from react.form.form-field ^1.0.0 · built alongside by fune
import type { DateFieldProps } from "./react_form_date_field_types.ts";

const NO_RULES: DateRules = { today: null, timing: null, notBefore: null, notAfter: null };

function present(node: ReactNode): boolean {
  return node !== null && node !== undefined && node !== false && node !== "";
}

/**
 * The browser's own date picker, labelled and described the GOV.UK way. Its
 * value is always YYYY-MM-DD (or ""), whatever the person's locale shows, and
 * validateIsoDate checks it as a calendar date: no Date object, no time zone,
 * so 2026-03-29 is the same day in London, Sydney and on the server. The
 * rules' earliest and latest dates become the picker's min and max, and
 * "today" for a past or future rule is the caller's, worked out in the
 * person's own time zone.
 *
 * GOV.UK asks for a date someone already knows (a date of birth) as three
 * boxes (react.form.date-input); a picker suits a date being chosen, such as
 * an appointment.
 */
export function DateField(props: DateFieldProps): JSX.Element {
  const auto = useId();
  const { label, hint, required, showError, error, value, defaultValue, onChange, onBlur, autoComplete, disabled, className } = props;
  const id = props.id ?? auto;
  const rules = props.rules ?? NO_RULES;
  const [own, setOwn] = useState<string>(defaultValue ?? "");
  const current = value ?? own;
  const messageLabel = props.messageLabel ?? (typeof label === "string" ? label : "Date");
  const check = validateIsoDate(current, messageLabel, required ?? false, rules);
  const shown: ReactNode = present(error) ? error : showError && !check.valid ? check.message : null;
  const ids = fieldIds(id, present(hint), present(shown));
  return (
    <FormField id={id} label={label} hint={hint} error={shown} className={className}>
      <input
        className={ids.error ? "fune-input fune-input--error fune-date-field" : "fune-input fune-date-field"}
        id={ids.control}
        name={props.name ?? id}
        type="date"
        min={rules.notBefore ?? undefined}
        max={rules.notAfter ?? undefined}
        value={value}
        defaultValue={value === undefined ? defaultValue : undefined}
        autoComplete={autoComplete}
        required={required || undefined}
        disabled={disabled}
        aria-describedby={ids.describedBy ?? undefined}
        aria-invalid={ids.error ? true : undefined}
        onChange={(event) => {
          setOwn(event.target.value);
          onChange?.(event.target.value);
        }}
        onBlur={onBlur ? () => onBlur() : undefined}
      />
    </FormField>
  );
}