import React from 'react'; import noop from 'utils/noop'; import { ControllerStateAndHelpers } from 'downshift'; import SimpleDropdown from './components/SimpleDropdown'; import ScrollableDropdown from './components/ScrollableDropdown'; import { Item } from './types'; interface Props { /** Fit the width to its parent width when it is `true`. * For Dropdown with cascading menu, `block` is only applied to the top-level menu. * See **Block with cascading menu** example below. */ block?: boolean; /** Pop menu items */ items: Item[]; /** The currently selected item */ selectedItem?: Item; /** A boolean which control to show a checked icon next to the selected item */ checkSelectedItem?: boolean; /** Callback function, to be executed when user selected an item and it has changed */ onChange?: (_selectedItem: Item | null) => void; /** Callback function, to be executed when user selected an item */ onSelect?: (_selectedItem: Item | null) => void; /** Callback function, to be executed when user clicked outside of pop menu */ onOuterClick?: (_: ControllerStateAndHelpers) => void; /** Callback function, to be executed when user toggled the pop menu */ onToggle?: (_isOpen: boolean, _args: { selectedItem: Item }) => void; /** Label of the component, will be shown before user selected option */ defaultLabel?: string; /** Open direction of pop menu */ direction?: 'left' | 'right' | 'auto'; /** Variant of component, `default` is the component with standard select button; * `compact` is the component with icon-only button */ variant?: 'default' | 'compact'; /** Custom icon for the Dropdown button, if not supplied default icon will be used: * (`v`) icon on `default` variant and (**⋮**) icon on `compact` variant */ // eslint-disable-next-line no-undef icon?: JSX.Element; /** A boolean which, if true, disables the Dropdown */ disabled?: boolean; /** injected by with theme, used to handle key down for auto direction and rtl */ theme?: { rtl: boolean; }; /** A custom string to replace the label, if not supplied selectedItems will be used */ customLabel?: string; /** A boolean which, if true, the Dropdown will become scrollable */ scroll?: boolean; /** A list size of the menu */ listSize?: 'default' | 'intrinsic' | 'fixed' | 'parent'; /** Rest Props */ [rest: string]: any; } const defaultProps = { block: false, selectedItem: undefined, customLabel: undefined, checkSelectedItem: false, onChange: noop, onOuterClick: noop, onToggle: noop, onSelect: noop, defaultLabel: 'Options', direction: 'auto', variant: 'default', icon: undefined, disabled: false, theme: { rtl: false, }, listSize: 'default', }; const Dropdown = ({ scroll = false, ...props }: Props) => { const DropdownComponent = scroll ? ScrollableDropdown : SimpleDropdown; const dropdownProps = { ...defaultProps, ...props }; return ; }; export default Dropdown;