UNPKG

1.94 kBTypeScriptView Raw
1import React from "react";
2
3import { UI } from "../UI.js";
4import type { ClassNames, CustomComponents } from "../types/index.js";
5
6/** An option to use in the dropdown. Maps to the `<option>` HTML element. */
7export type DropdownOption = {
8 /** The value of the option. */
9 value: number;
10 /** The label of the option. */
11 label: string;
12 /**
13 * The dropdown option is disabled when it cannot be selected because out of
14 * the calendar range.
15 */
16 disabled: boolean;
17};
18
19/**
20 * Render a dropdown component to use in the navigation bar.
21 *
22 * @group Components
23 * @see https://daypicker.dev/guides/custom-components
24 */
25export function Dropdown(
26 props: {
27 components: Pick<CustomComponents, "Select" | "Option" | "Chevron">;
28 classNames: Pick<
29 ClassNames,
30 UI.DropdownRoot | UI.Dropdown | UI.CaptionLabel | UI.Chevron
31 >;
32 options?: DropdownOption[] | undefined;
33 } & Omit<JSX.IntrinsicElements["select"], "children">
34) {
35 const { options, className, components, classNames, ...selectProps } = props;
36
37 const cssClassSelect = [classNames[UI.Dropdown], className].join(" ");
38
39 const selectedOption = options?.find(
40 ({ value }) => value === selectProps.value
41 );
42 return (
43 <span
44 data-disabled={selectProps.disabled}
45 className={classNames[UI.DropdownRoot]}
46 >
47 <components.Select className={cssClassSelect} {...selectProps}>
48 {options?.map(({ value, label, disabled }) => (
49 <components.Option key={value} value={value} disabled={disabled}>
50 {label}
51 </components.Option>
52 ))}
53 </components.Select>
54 <span className={classNames[UI.CaptionLabel]} aria-hidden>
55 {selectedOption?.label}
56 <components.Chevron
57 orientation="down"
58 size={18}
59 className={classNames[UI.Chevron]}
60 />
61 </span>
62 </span>
63 );
64}
65
66export type DropdownProps = Parameters<typeof Dropdown>[0];