-
-
Notifications
You must be signed in to change notification settings - Fork 697
/
CaptionDropdowns.tsx
43 lines (39 loc) · 1.45 KB
/
CaptionDropdowns.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { addMonths } from "date-fns";
import { useDayPicker } from "../contexts/DayPicker";
import { useNavigation } from "../contexts/Navigation";
import { MonthChangeEventHandler } from "../types/EventHandlers";
import { CaptionProps } from "./Caption";
import { CaptionLabel } from "./CaptionLabel";
import { MonthsDropdown } from "./MonthsDropdown";
import { YearsDropdown } from "./YearsDropdown";
/** Render a caption with the dropdowns to navigate between months and years. */
export function CaptionDropdowns(props: CaptionProps): JSX.Element {
const { classNames, styles, components } = useDayPicker();
const { goToMonth } = useNavigation();
const handleMonthChange: MonthChangeEventHandler = (newMonth) => {
goToMonth(
addMonths(newMonth, props.displayIndex ? -props.displayIndex : 0)
);
};
const CaptionLabelComponent = components?.CaptionLabel ?? CaptionLabel;
const captionLabel = (
<CaptionLabelComponent id={props.id} displayMonth={props.displayMonth} />
);
return (
<div
className={classNames.caption_dropdowns}
style={styles.caption_dropdowns}
>
{/* Caption label is visually hidden but for a11y. */}
<div className={classNames.vhidden}>{captionLabel}</div>
<MonthsDropdown
onChange={handleMonthChange}
displayMonth={props.displayMonth}
/>
<YearsDropdown
onChange={handleMonthChange}
displayMonth={props.displayMonth}
/>
</div>
);
}