import * as React from "react"; import classNames from "classnames"; import { AriaSelectProps, HiddenSelect, useSelect } from "react-aria"; import { useSelectState } from "react-stately"; import { Clickable } from "../Clickable/Clickable"; import { InputWithAttachments, useInputStyleClasses } from "../Input/Input"; import { Stack } from "../Stack/Stack"; import { DropdownChevron } from "./dropdown/DropdownChevron"; import { Picker, PickerPublicProps } from "./Picker"; import styles from "./Picker.module.css"; export interface SelectProps extends PickerPublicProps, Omit, "onSelectionChange"> { inputClassName?: string; onSelect?: (itemKey: string) => void; } export function Select(props: SelectProps) { const { selectedKey, placeholder = "Select an option", name, inputClassName, onSelect } = props; const inputStyles = useInputStyleClasses(props); const controlState = useSelectState({ ...props, defaultSelectedKey: selectedKey ?? undefined, onSelectionChange(key) { // React.Key can be a number, but we're restricting that to only strings // for simplicity. onSelect?.(key as string); }, }); const ref = React.useRef(null); const { triggerProps, valueProps, menuProps, labelProps, descriptionProps, errorMessageProps } = useSelect( { ...props, defaultSelectedKey: selectedKey ?? undefined, }, controlState, ref, ); const selectedElement = controlState.selectedItem != null ? controlState.selectedItem.rendered : placeholder; return (
{selectedElement}
); }