From f63ca6d93e49c477e1e69a638115358a4f3e1e16 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Sun, 8 Jun 2025 15:14:55 +0300 Subject: [PATCH] add numberOfDaysBefore param --- .../src/components/ui/horizontal-calendar.tsx | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/ui/src/components/ui/horizontal-calendar.tsx b/packages/ui/src/components/ui/horizontal-calendar.tsx index 8bc2796..13f6f63 100644 --- a/packages/ui/src/components/ui/horizontal-calendar.tsx +++ b/packages/ui/src/components/ui/horizontal-calendar.tsx @@ -10,6 +10,7 @@ import { useEffect, useMemo, useRef, useState } from 'react'; type HorizontalCalendarProps = { readonly className?: string; readonly numberOfDays?: number; + readonly numberOfDaysBefore?: number; readonly onDateChange: (date: Date) => void; readonly selectedDate: Date; }; @@ -17,11 +18,12 @@ type HorizontalCalendarProps = { export function HorizontalCalendar({ className, numberOfDays = 14, + numberOfDaysBefore = 7, onDateChange, selectedDate, }: HorizontalCalendarProps) { const scrollRef = useRef(null); - const [baseDate, setBaseDate] = useState(new Date()); + const [baseDate, setBaseDate] = useState(addDays(new Date(), -numberOfDaysBefore)); const [currentMonthDate, setCurrentMonthDate] = useState(new Date()); const dates = useMemo(() => { @@ -90,6 +92,25 @@ export function HorizontalCalendar({ }; }, []); + useEffect(() => { + const container = scrollRef.current; + if (!container) return; + + const selectedChild = container.querySelector( + `[data-date="${selectedDate.toISOString()}"]`, + ) as HTMLElement; + if (selectedChild) { + // Прокрутка так, чтобы элемент оказался в центре + const offsetLeft = selectedChild.offsetLeft; + const centerOffset = offsetLeft - container.clientWidth / 2 + selectedChild.clientWidth / 2; + + container.scrollTo({ + behavior: 'auto', // можно 'smooth', если хочешь плавно + left: centerOffset, + }); + } + }, []); // <- только при монтировании + return (