From 544124582753aceb3f8d680c7a1b5880d077d4ff Mon Sep 17 00:00:00 2001 From: Ilya Mukhortov Date: Fri, 30 Oct 2026 13:00:07 +1000 Subject: [PATCH] Use the kit select for calendar month and year so the list stays with the popover. Co-authored-by: Cursor --- src/kit/date-input.tsx | 11 ++++++++++- src/ui/calendar.tsx | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/kit/date-input.tsx b/src/kit/date-input.tsx index a8ad552..aa65a73 100644 --- a/src/kit/date-input.tsx +++ b/src/kit/date-input.tsx @@ -73,6 +73,11 @@ export function useDateText(value: Date | null, onChange: (date: Date | null) => return { text, change, set, error, shownError, onBlur: () => setTouched(true) } } +/** Список месяца и года рисуется порталом вне попапа календаря. */ +function inSelectLayer(target: EventTarget | null) { + return target instanceof Element && Boolean(target.closest("[data-slot=select-content]")) +} + /** * Состояние попапа с календарём у поля ввода: при каждом открытии календарь показывает * месяц текущего значения, а после закрытия (выбор, Esc, кнопка) фокус возвращается в поле — @@ -96,9 +101,13 @@ export function useCalendarPopover(initialMonth: () => Date) { const contentProps = { onInteractOutside: (e: Event) => { // Кнопка календаря сама закроет попап своим кликом, иначе он тут же откроется снова. - if (toggleRef.current?.contains(e.target as Node)) e.preventDefault() + // Список месяца и года — в портале, вне попапа: клик по нему не закрывает календарь. + if (toggleRef.current?.contains(e.target as Node) || inSelectLayer(e.target)) e.preventDefault() else clickedOutside.current = true }, + onFocusOutside: (e: Event) => { + if (inSelectLayer(e.target)) e.preventDefault() + }, onCloseAutoFocus: (e: Event) => { e.preventDefault() if (!clickedOutside.current) returnFocus.current?.focus() diff --git a/src/ui/calendar.tsx b/src/ui/calendar.tsx index 38baf28..2c4bad7 100644 --- a/src/ui/calendar.tsx +++ b/src/ui/calendar.tsx @@ -10,10 +10,12 @@ import { DayPicker, getDefaultClassNames, type DayButton, + type DropdownProps, } from "react-day-picker" import { ru } from "react-day-picker/locale" import { Button, buttonVariants } from "./button" +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select" function Calendar({ className, @@ -54,17 +56,18 @@ function Calendar({ ), month: cn("flex w-full flex-col gap-4", defaultClassNames.month), nav: cn( - "absolute inset-x-0 top-0 flex w-full items-center justify-between gap-1", + // Полоса на всю ширину шапки: клики в середине должны попадать в списки месяца и года. + "pointer-events-none absolute inset-x-0 top-0 flex w-full items-center justify-between gap-1", defaultClassNames.nav ), button_previous: cn( buttonVariants({ variant: buttonVariant }), - "size-(--cell-size) p-0 select-none aria-disabled:opacity-50", + "pointer-events-auto size-(--cell-size) p-0 select-none aria-disabled:opacity-50", defaultClassNames.button_previous ), button_next: cn( buttonVariants({ variant: buttonVariant }), - "size-(--cell-size) p-0 select-none aria-disabled:opacity-50", + "pointer-events-auto size-(--cell-size) p-0 select-none aria-disabled:opacity-50", defaultClassNames.button_next ), month_caption: cn( @@ -72,7 +75,7 @@ function Calendar({ defaultClassNames.month_caption ), dropdowns: cn( - "flex h-(--cell-size) w-full items-center justify-center gap-1.5 text-sm font-medium", + "relative z-10 flex h-(--cell-size) w-full items-center justify-center gap-1.5 text-sm font-medium", defaultClassNames.dropdowns ), dropdown_root: cn( @@ -164,6 +167,7 @@ function Calendar({ ) }, + Dropdown: CalendarDropdown, DayButton: CalendarDayButton, WeekNumber: ({ children, ...props }) => { return ( @@ -181,6 +185,28 @@ function Calendar({ ) } +/** Месяц и год в шапке: список кита вместо нативного select. */ +function CalendarDropdown({ options, value, onChange, disabled, "aria-label": ariaLabel }: DropdownProps) { + return ( + + ) +} + function CalendarDayButton({ className, day,