@acusti/dropdown
Version:
React component that renders a dropdown with a trigger and supports searching, keyboard access, and more
88 lines (87 loc) • 2.37 kB
TypeScript
import * as React from 'react';
export type Item = {
element: HTMLElement | null;
event: Event | React.SyntheticEvent<HTMLElement>;
label: string;
value: string;
};
export type Props = {
/**
* Boolean indicating if the user can submit a value not already in the
* dropdown.
*/
allowCreate?: boolean;
/**
* Boolean indicating if the user can submit an empty value (i.e. clear
* the value). Defaults to true.
*/
allowEmpty?: boolean;
/**
* Can take a single React element or exactly two renderable children.
*/
children: ChildrenTuple | React.JSX.Element;
className?: string;
disabled?: boolean;
/**
* Group identifier string links dropdowns together into a menu
* (like macOS top menubar).
*/
group?: string;
hasItems?: boolean;
isOpenOnMount?: boolean;
isSearchable?: boolean;
keepOpenOnSubmit?: boolean;
label?: string;
/**
* Only usable in conjunction with {isSearchable: true}.
* Used as search input’s name.
*/
name?: string;
onClick?: (event: React.MouseEvent<HTMLElement>) => unknown;
onClose?: () => unknown;
onMouseDown?: (event: React.MouseEvent<HTMLElement>) => unknown;
onMouseUp?: (event: React.MouseEvent<HTMLElement>) => unknown;
onOpen?: () => unknown;
onSubmitItem?: (payload: Item) => void;
/**
* Only usable in conjunction with {isSearchable: true}.
* Used as search input’s placeholder.
*/
placeholder?: string;
style?: React.CSSProperties;
/**
* Only usable in conjunction with {isSearchable: true}.
* Used as search input’s tabIndex.
*/
tabIndex?: number;
/**
* Used as search input’s value if props.isSearchable === true
* Used to determine if value has changed to avoid triggering onSubmitItem if not
*/
value?: string;
};
type ChildrenTuple = [React.ReactNode, React.ReactNode];
export default function Dropdown({
allowCreate,
allowEmpty,
children,
className,
disabled,
hasItems,
isOpenOnMount,
isSearchable,
keepOpenOnSubmit,
label,
name,
onClick,
onClose,
onMouseDown,
onMouseUp,
onOpen,
onSubmitItem,
placeholder,
style: styleFromProps,
tabIndex,
value,
}: Props): React.JSX.Element;
export {};