A date typed part by part in the reader's order, with arrow keys, paste, min and max, and an error that says what's wrong.
Nov 1 to today. October is closed.
Net 30 by default.
INV-2041 · $4,280.00 · due December 28, 2026
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { addDays, formatDate } from "./ui/uicontext/components/dates/DateMath.ts"import { DateField } from "./ui/uicontext/components/DateField.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"// An invoice's dates. The issue date can't be in a closed period (before Nov 1) or in the future;// the due date follows it and can't come before it. Try 10/31/2026, paste "2026-02-30", or clear// the due date and leave the field.const today = "2026-12-02"const styles = stylex.create({ form: { display: "grid", gap: space.lg, inlineSize: "min(100%, 26rem)" }, row: { display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(11rem, 1fr))", gap: space.md, alignItems: "start" }, total: { margin: 0, fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" },})const Message = UiSchema.TaggedEnum({ CommittedIssued: { value: Schema.NullOr(Schema.String) }, CommittedDue: { value: Schema.NullOr(Schema.String) },})export const InvoiceDates = Html.component({ name: "InvoiceDates", model: Schema.Struct({ issued: Schema.NullOr(Schema.String), due: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ issued: "2026-11-28", due: "2026-12-28" }), update: { // net 30 follows the issue date until someone sets the due date by hand CommittedIssued: ($, { value }) => $.evolve({ issued: value, due: (due) => (value !== null && (due === null || due === addDays($.model.issued ?? value, 30)) ? addDays(value, 30) : due) }), CommittedDue: ($, { value }) => $.evolve({ due: value }), }, view: (h, { issued, due }) => h.div.attrs(sx(styles.form)).children( h.div.attrs(sx(styles.row)).children( DateField(h, { key: "issued", props: { label: "Issue date", id: "invoice-issued", value: issued, min: "2026-11-01", max: today, today, locale: "en-US", required: true, name: "issued", description: "Nov 1 to today. October is closed." }, onEmit: { Committed: ({ value }) => Message.CommittedIssued({ value }), RequestedOpen: () => undefined }, }), DateField(h, { key: "due", props: { label: "Due date", id: "invoice-due", value: due, ...(issued === null ? {} : { min: issued }), today, locale: "en-US", required: true, name: "due", description: "Net 30 by default." }, onEmit: { Committed: ({ value }) => Message.CommittedDue({ value }), RequestedOpen: () => undefined }, }), ), h.p.attrs(sx(styles.total)).children( issued === null || due === null ? "Both dates are needed to send the invoice." : `INV-2041 · $4,280.00 · due ${formatDate(due, "en-US", { dateStyle: "long" })}`, ), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/date-fieldUsage
import { DateField } from "./ui/uicontext/components/DateField.ts"const Message = UiSchema.TaggedEnum({ CommittedBirthday: { value: Schema.NullOr(Schema.String) } })export const Birthday = (h: Html.H<typeof Message.Type>, birthday: string | null) => DateField(h, { props: { label: "Birthday", value: birthday, max: "2026-12-02" }, onEmit: { Committed: ({ value }) => Message.CommittedBirthday({ value }), RequestedOpen: () => undefined }, })Examples
Locales and right to left
Month first in the US, day first in the UK and Germany, with a dot in German. Type in one and the others follow.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { DateField } from "./ui/uicontext/components/DateField.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"// One day in four readers' fields: the parts follow each locale's order and mark, and a// right-to-left page keeps the numbers left to right.const styles = stylex.create({ grid: { display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(12rem, 1fr))", gap: space.lg, inlineSize: "min(100%, 34rem)" },})const Message = UiSchema.TaggedEnum({ CommittedDay: { value: Schema.NullOr(Schema.String) } })const readers = [ { locale: "en-US", label: "Ship date (US)", dir: "ltr" }, { locale: "en-GB", label: "Ship date (UK)", dir: "ltr" }, { locale: "de-DE", label: "Versanddatum", dir: "ltr" }, { locale: "he-IL", label: "תאריך משלוח", dir: "rtl" },] as constexport const LocaleDates = Html.component({ name: "LocaleDates", model: Schema.Struct({ day: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ day: "2027-01-08" }), update: { CommittedDay: ($, { value }) => $.evolve({ day: value }) }, view: (h, { day }) => h.div.attrs(sx(styles.grid)).children( readers.map((reader) => h.div.key(reader.locale).attrs({ dir: reader.dir, lang: reader.locale }).children( DateField(h, { key: reader.locale, props: { label: reader.label, id: `ship-${reader.locale}`, value: day, locale: reader.locale, today: "2026-12-02" }, onEmit: { Committed: ({ value }) => Message.CommittedDay({ value }), RequestedOpen: () => undefined }, }), ), ), ),})