Let the calendar year be typed when the year list is long.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
5441245827
commit
77fd8fd1dd
|
|
@ -140,7 +140,7 @@ export function calendarBounds({ min, max }: DateLimits, captionLayout: "label"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Дата: ввод вручную по маске «дд.мм.гггг» или выбор в календаре (кнопка, Alt+↓).
|
* Дата: ввод вручную по маске «дд.мм.гггг» или выбор в календаре (кнопка, Alt+↓).
|
||||||
* captionLayout="dropdown" — месяц и год списками, удобно для даты рождения.
|
* captionLayout="dropdown" — месяц списком. yearCaption="input" — год полем из четырёх цифр.
|
||||||
*/
|
*/
|
||||||
export function DateInput({
|
export function DateInput({
|
||||||
value,
|
value,
|
||||||
|
|
@ -149,6 +149,7 @@ export function DateInput({
|
||||||
max,
|
max,
|
||||||
invalid,
|
invalid,
|
||||||
captionLayout = "label",
|
captionLayout = "label",
|
||||||
|
yearCaption = "select",
|
||||||
onErrorChange,
|
onErrorChange,
|
||||||
id,
|
id,
|
||||||
disabled,
|
disabled,
|
||||||
|
|
@ -161,6 +162,8 @@ export function DateInput({
|
||||||
/** Подсветить извне (ошибка формы), вдобавок к собственной проверке. */
|
/** Подсветить извне (ошибка формы), вдобавок к собственной проверке. */
|
||||||
invalid?: boolean
|
invalid?: boolean
|
||||||
captionLayout?: "label" | "dropdown"
|
captionLayout?: "label" | "dropdown"
|
||||||
|
/** Год в шапке календаря. «input» — набрать цифрами, когда список годов длинный. */
|
||||||
|
yearCaption?: "select" | "input"
|
||||||
/** Сообщение о неправильном вводе — чтобы показать его в FormField. */
|
/** Сообщение о неправильном вводе — чтобы показать его в FormField. */
|
||||||
onErrorChange?: (error: string | undefined) => void
|
onErrorChange?: (error: string | undefined) => void
|
||||||
id?: string
|
id?: string
|
||||||
|
|
@ -222,6 +225,7 @@ export function DateInput({
|
||||||
required
|
required
|
||||||
autoFocus
|
autoFocus
|
||||||
captionLayout={captionLayout}
|
captionLayout={captionLayout}
|
||||||
|
yearCaption={yearCaption}
|
||||||
selected={value ?? undefined}
|
selected={value ?? undefined}
|
||||||
month={popover.month}
|
month={popover.month}
|
||||||
onMonthChange={popover.setMonth}
|
onMonthChange={popover.setMonth}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ function Calendar({
|
||||||
classNames,
|
classNames,
|
||||||
showOutsideDays = true,
|
showOutsideDays = true,
|
||||||
captionLayout = "label",
|
captionLayout = "label",
|
||||||
|
yearCaption = "select",
|
||||||
buttonVariant = "ghost",
|
buttonVariant = "ghost",
|
||||||
formatters,
|
formatters,
|
||||||
components,
|
components,
|
||||||
|
|
@ -29,6 +30,8 @@ function Calendar({
|
||||||
...props
|
...props
|
||||||
}: React.ComponentProps<typeof DayPicker> & {
|
}: React.ComponentProps<typeof DayPicker> & {
|
||||||
buttonVariant?: React.ComponentProps<typeof Button>["variant"]
|
buttonVariant?: React.ComponentProps<typeof Button>["variant"]
|
||||||
|
/** Год в шапке: список или поле из четырёх цифр. Вместе с captionLayout="dropdown". */
|
||||||
|
yearCaption?: "select" | "input"
|
||||||
}) {
|
}) {
|
||||||
const defaultClassNames = getDefaultClassNames()
|
const defaultClassNames = getDefaultClassNames()
|
||||||
|
|
||||||
|
|
@ -167,7 +170,7 @@ function Calendar({
|
||||||
<ChevronDownIcon className={cn("size-4", className)} {...props} />
|
<ChevronDownIcon className={cn("size-4", className)} {...props} />
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
Dropdown: CalendarDropdown,
|
Dropdown: (dropdownProps) => <CalendarDropdown {...dropdownProps} yearCaption={yearCaption} />,
|
||||||
DayButton: CalendarDayButton,
|
DayButton: CalendarDayButton,
|
||||||
WeekNumber: ({ children, ...props }) => {
|
WeekNumber: ({ children, ...props }) => {
|
||||||
return (
|
return (
|
||||||
|
|
@ -185,13 +188,40 @@ function Calendar({
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Месяц и год в шапке: список кита вместо нативного select. */
|
function applyDropdownChange(onChange: DropdownProps["onChange"], value: string) {
|
||||||
function CalendarDropdown({ options, value, onChange, disabled, "aria-label": ariaLabel }: DropdownProps) {
|
onChange?.({ target: { value } } as React.ChangeEvent<HTMLSelectElement>)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Год отличается от месяца тем, что значения опций — сами годы, а не 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 (
|
||||||
|
<CalendarYearInput
|
||||||
|
options={options}
|
||||||
|
value={value}
|
||||||
|
onChange={onChange}
|
||||||
|
disabled={disabled}
|
||||||
|
aria-label={ariaLabel}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<Select
|
<Select
|
||||||
value={value == null ? undefined : String(value)}
|
value={value == null ? undefined : String(value)}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onValueChange={(next) => onChange?.({ target: { value: next } } as React.ChangeEvent<HTMLSelectElement>)}
|
onValueChange={(next) => applyDropdownChange(onChange, next)}
|
||||||
>
|
>
|
||||||
<SelectTrigger size="sm" aria-label={ariaLabel} className="h-8 gap-1 px-2 font-normal shadow-none">
|
<SelectTrigger size="sm" aria-label={ariaLabel} className="h-8 gap-1 px-2 font-normal shadow-none">
|
||||||
<SelectValue />
|
<SelectValue />
|
||||||
|
|
@ -207,6 +237,38 @@ function CalendarDropdown({ options, value, onChange, disabled, "aria-label": ar
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Четыре цифры года: сетка переключается, когда год есть в доступных границах. */
|
||||||
|
function CalendarYearInput({ options, value, onChange, disabled, "aria-label": ariaLabel }: DropdownProps) {
|
||||||
|
const year = value == null ? "" : String(value)
|
||||||
|
const [draft, setDraft] = React.useState<string | null>(null)
|
||||||
|
const allowed = new Set(options?.filter((option) => !option.disabled).map((option) => String(option.value)))
|
||||||
|
|
||||||
|
return (
|
||||||
|
<input
|
||||||
|
inputMode="numeric"
|
||||||
|
autoComplete="off"
|
||||||
|
maxLength={4}
|
||||||
|
aria-label={ariaLabel}
|
||||||
|
disabled={disabled}
|
||||||
|
value={draft ?? year}
|
||||||
|
onChange={(e) => {
|
||||||
|
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({
|
function CalendarDayButton({
|
||||||
className,
|
className,
|
||||||
day,
|
day,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue