1 | import React from "react";
|
2 |
|
3 | import { UI } from "../UI.js";
|
4 | import type { ClassNames, CustomComponents } from "../types/index.js";
|
5 |
|
6 |
|
7 | export type DropdownOption = {
|
8 |
|
9 | value: number;
|
10 |
|
11 | label: string;
|
12 | |
13 |
|
14 |
|
15 |
|
16 | disabled: boolean;
|
17 | };
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | export 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 |
|
66 | export type DropdownProps = Parameters<typeof Dropdown>[0];
|