From 77fd8fd1dd9a420ebce6dfb2f0d89f6da4b4dea7 Mon Sep 17 00:00:00 2001 From: Ilya Mukhortov Date: Fri, 30 Oct 2026 13:23:36 +1000 Subject: [PATCH] Let the calendar year be typed when the year list is long. Co-authored-by: Cursor --- src/kit/date-input.tsx | 6 +++- src/ui/calendar.tsx | 70 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/kit/date-input.tsx b/src/kit/date-input.tsx index aa65a73..fa7fed4 100644 --- a/src/kit/date-input.tsx +++ b/src/kit/date-input.tsx @@ -140,7 +140,7 @@ export function calendarBounds({ min, max }: DateLimits, captionLayout: "label" /** * Дата: ввод вручную по маске «дд.мм.гггг» или выбор в календаре (кнопка, Alt+↓). - * captionLayout="dropdown" — месяц и год списками, удобно для даты рождения. + * captionLayout="dropdown" — месяц списком. yearCaption="input" — год полем из четырёх цифр. */ export function DateInput({ value, @@ -149,6 +149,7 @@ export function DateInput({ max, invalid, captionLayout = "label", + yearCaption = "select", onErrorChange, id, disabled, @@ -161,6 +162,8 @@ export function DateInput({ /** Подсветить извне (ошибка формы), вдобавок к собственной проверке. */ invalid?: boolean captionLayout?: "label" | "dropdown" + /** Год в шапке календаря. «input» — набрать цифрами, когда список годов длинный. */ + yearCaption?: "select" | "input" /** Сообщение о неправильном вводе — чтобы показать его в FormField. */ onErrorChange?: (error: string | undefined) => void id?: string @@ -222,6 +225,7 @@ export function DateInput({ required autoFocus captionLayout={captionLayout} + yearCaption={yearCaption} selected={value ?? undefined} month={popover.month} onMonthChange={popover.setMonth} diff --git a/src/ui/calendar.tsx b/src/ui/calendar.tsx index 2c4bad7..1ba7fea 100644 --- a/src/ui/calendar.tsx +++ b/src/ui/calendar.tsx @@ -22,6 +22,7 @@ function Calendar({ classNames, showOutsideDays = true, captionLayout = "label", + yearCaption = "select", buttonVariant = "ghost", formatters, components, @@ -29,6 +30,8 @@ function Calendar({ ...props }: React.ComponentProps & { buttonVariant?: React.ComponentProps["variant"] + /** Год в шапке: список или поле из четырёх цифр. Вместе с captionLayout="dropdown". */ + yearCaption?: "select" | "input" }) { const defaultClassNames = getDefaultClassNames() @@ -167,7 +170,7 @@ function Calendar({ ) }, - Dropdown: CalendarDropdown, + Dropdown: (dropdownProps) => , DayButton: CalendarDayButton, WeekNumber: ({ children, ...props }) => { return ( @@ -185,13 +188,40 @@ function Calendar({ ) } -/** Месяц и год в шапке: список кита вместо нативного select. */ -function CalendarDropdown({ options, value, onChange, disabled, "aria-label": ariaLabel }: DropdownProps) { +function applyDropdownChange(onChange: DropdownProps["onChange"], value: string) { + onChange?.({ target: { value } } as React.ChangeEvent) +} + +/** Год отличается от месяца тем, что значения опций — сами годы, а не 0–11. */ +function isYearDropdown(options: DropdownProps["options"]) { + return options?.some((option) => option.value > 99) ?? false +} + +/** Месяц и год в шапке: список кита вместо нативного select. Год можно заменить полем. */ +function CalendarDropdown({ + options, + value, + onChange, + disabled, + "aria-label": ariaLabel, + yearCaption, +}: DropdownProps & { yearCaption?: "select" | "input" }) { + if (yearCaption === "input" && isYearDropdown(options)) { + return ( + + ) + } return ( { + const next = e.target.value.replace(/\D/g, "").slice(0, 4) + setDraft(next) + if (next.length === 4 && allowed.has(next)) { + applyDropdownChange(onChange, next) + setDraft(null) + } + }} + onFocus={(e) => e.currentTarget.select()} + onBlur={() => setDraft(null)} + onKeyDown={(e) => { + if (e.key.startsWith("Arrow")) e.stopPropagation() + }} + className="h-8 w-16 rounded-md border border-input bg-transparent text-center text-sm tabular shadow-none outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50" + /> + ) +} + function CalendarDayButton({ className, day,