Use the kit select for calendar month and year so the list stays with the popover.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
93208fb911
commit
5441245827
|
|
@ -73,6 +73,11 @@ export function useDateText(value: Date | null, onChange: (date: Date | null) =>
|
||||||
return { text, change, set, error, shownError, onBlur: () => setTouched(true) }
|
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, кнопка) фокус возвращается в поле —
|
* месяц текущего значения, а после закрытия (выбор, Esc, кнопка) фокус возвращается в поле —
|
||||||
|
|
@ -96,9 +101,13 @@ export function useCalendarPopover(initialMonth: () => Date) {
|
||||||
const contentProps = {
|
const contentProps = {
|
||||||
onInteractOutside: (e: Event) => {
|
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
|
else clickedOutside.current = true
|
||||||
},
|
},
|
||||||
|
onFocusOutside: (e: Event) => {
|
||||||
|
if (inSelectLayer(e.target)) e.preventDefault()
|
||||||
|
},
|
||||||
onCloseAutoFocus: (e: Event) => {
|
onCloseAutoFocus: (e: Event) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
if (!clickedOutside.current) returnFocus.current?.focus()
|
if (!clickedOutside.current) returnFocus.current?.focus()
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,12 @@ import {
|
||||||
DayPicker,
|
DayPicker,
|
||||||
getDefaultClassNames,
|
getDefaultClassNames,
|
||||||
type DayButton,
|
type DayButton,
|
||||||
|
type DropdownProps,
|
||||||
} from "react-day-picker"
|
} from "react-day-picker"
|
||||||
import { ru } from "react-day-picker/locale"
|
import { ru } from "react-day-picker/locale"
|
||||||
|
|
||||||
import { Button, buttonVariants } from "./button"
|
import { Button, buttonVariants } from "./button"
|
||||||
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select"
|
||||||
|
|
||||||
function Calendar({
|
function Calendar({
|
||||||
className,
|
className,
|
||||||
|
|
@ -54,17 +56,18 @@ function Calendar({
|
||||||
),
|
),
|
||||||
month: cn("flex w-full flex-col gap-4", defaultClassNames.month),
|
month: cn("flex w-full flex-col gap-4", defaultClassNames.month),
|
||||||
nav: cn(
|
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
|
defaultClassNames.nav
|
||||||
),
|
),
|
||||||
button_previous: cn(
|
button_previous: cn(
|
||||||
buttonVariants({ variant: buttonVariant }),
|
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
|
defaultClassNames.button_previous
|
||||||
),
|
),
|
||||||
button_next: cn(
|
button_next: cn(
|
||||||
buttonVariants({ variant: buttonVariant }),
|
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
|
defaultClassNames.button_next
|
||||||
),
|
),
|
||||||
month_caption: cn(
|
month_caption: cn(
|
||||||
|
|
@ -72,7 +75,7 @@ function Calendar({
|
||||||
defaultClassNames.month_caption
|
defaultClassNames.month_caption
|
||||||
),
|
),
|
||||||
dropdowns: cn(
|
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
|
defaultClassNames.dropdowns
|
||||||
),
|
),
|
||||||
dropdown_root: cn(
|
dropdown_root: cn(
|
||||||
|
|
@ -164,6 +167,7 @@ function Calendar({
|
||||||
<ChevronDownIcon className={cn("size-4", className)} {...props} />
|
<ChevronDownIcon className={cn("size-4", className)} {...props} />
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
Dropdown: CalendarDropdown,
|
||||||
DayButton: CalendarDayButton,
|
DayButton: CalendarDayButton,
|
||||||
WeekNumber: ({ children, ...props }) => {
|
WeekNumber: ({ children, ...props }) => {
|
||||||
return (
|
return (
|
||||||
|
|
@ -181,6 +185,28 @@ function Calendar({
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Месяц и год в шапке: список кита вместо нативного select. */
|
||||||
|
function CalendarDropdown({ options, value, onChange, disabled, "aria-label": ariaLabel }: DropdownProps) {
|
||||||
|
return (
|
||||||
|
<Select
|
||||||
|
value={value == null ? undefined : String(value)}
|
||||||
|
disabled={disabled}
|
||||||
|
onValueChange={(next) => onChange?.({ target: { value: next } } as React.ChangeEvent<HTMLSelectElement>)}
|
||||||
|
>
|
||||||
|
<SelectTrigger size="sm" aria-label={ariaLabel} className="h-8 gap-1 px-2 font-normal shadow-none">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent className="z-[60]">
|
||||||
|
{options?.map((option) => (
|
||||||
|
<SelectItem key={option.value} value={String(option.value)} disabled={option.disabled}>
|
||||||
|
{option.label}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function CalendarDayButton({
|
function CalendarDayButton({
|
||||||
className,
|
className,
|
||||||
day,
|
day,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue