import React from 'react'; import PropTypes from 'prop-types'; import { ComponentProps } from '../utils/types'; /** @public */ type DropdownChildrenRenderer = (data: { anchorHeight: number | null; anchorWidth: number | null; maxHeight: number | null; maxWidth: number | null; placement: DropdownPlacementStatus | null; toggleId?: string; }) => React.ReactNode; /** @public */ type DropdownPlacement = 'above' | 'below' | 'left' | 'right' | 'vertical' | 'horizontal'; /** @public */ type DropdownPlacementStatus = 'above' | 'below' | 'left' | 'right' | 'misaligned'; /** @public */ type DropdownPossibleOpenReason = 'toggleClick' | 'toggleKeydown'; /** @public */ type DropdownPossibleCloseReason = 'clickAway' | 'contentClick' | 'escapeKey' | 'offScreen' | 'tabKey' | 'toggleClick'; /** @public */ type DropdownRequestCloseHandler = (data: { event?: React.MouseEvent | MouseEvent | KeyboardEvent | TouchEvent; reason: DropdownPossibleCloseReason; }) => void; /** @public */ type DropdownRequestOpenHandler = (event: React.MouseEvent | React.KeyboardEvent, data: { reason: DropdownPossibleOpenReason; }) => void; /** @public */ type DropdownToggleElement = React.ReactElement & { 'data-test-popover-id'?: string; 'data-test'?: string; ref?: React.Ref; }>; interface DropdownPropsBase { /** * Resurface private `Popover` prop for aligning the edge. * * `end` is ONLY supported in with the `below` PopoverPlacement * TODO: SUI-5101 Allow users to supply align prop in Popover and Dropdowns * @private */ align?: 'center' | 'edge' | 'end'; /** * Enables the `Dropdown` to be rendered over the toggle * if there isn't enough room to render it in a direction. */ canCoverAnchor?: boolean; /** * The content of the `Dropdown`. If a function is provided, it is invoked with an * object containing `anchorHeight`, `anchorWidth`, `maxHeight`, `maxWidth`, and * `placement`, and is expected to return renderable content. */ children?: React.ReactNode | DropdownChildrenRenderer; /** * An array of reasons for which this `Dropdown` closes. */ closeReasons?: DropdownPossibleCloseReason[]; /** * The default placement of the `Dropdown`. It might be rendered in a different direction * depending on the space available and the `repositionMode`. */ defaultPlacement?: DropdownPlacement; /** * A React ref which is set to the DOM element when the component mounts and null when it unmounts. */ elementRef?: React.Ref; /** * An array of reasons for which to set focus on the toggle. Only the subset of `closeReasons` * is honored. When `Menu.Items` open a Modal or other dialog, it might be necessary to * remove the 'contentClick' reason to allow focus to be passed to the dialog. */ focusToggleReasons?: DropdownPossibleCloseReason[]; /** * An id for the input, which may be necessary for accessibility, such as for aria * attributes. */ inputId?: string; /** * A callback function invoked with a data object containing the event, if applicable, and * a reason for the close request. */ onRequestClose?: DropdownRequestCloseHandler; /** * A callback function invoked with a data object containing the event. */ onRequestOpen?: DropdownRequestOpenHandler; /** * If an open prop is provided, this component behaves as a * [controlled component](https://reactjs.org/docs/forms.html#controlled-components). * The consumer is responsible for handling the open/close state. If no * open prop is provided, the component handles the open/close state internally. */ open?: boolean; /** * A private prop to support up and down arrow key navigation for `Select` and `Multiselect`. * @private */ openWithArrowKeys?: boolean; /** * See `repositionMode` on `Popover` for details. */ repositionMode?: 'none' | 'flip' | 'any'; /** * Keeps focus within the Popover while open. Only use this for inputs used in a form control. * Do not use this when the Dropdown contains a Menu because Menu handles its own focus. */ retainFocus?: boolean; /** * When true, the Popover automatically takes focus when 'open' changes to `true`. * Disable this for a Popover that has shows on hover, such as a tooltip. */ takeFocus?: boolean; /** * A toggle, such as a button or equivalent component that accepts `ref`, must be passed. * `aria-haspopup`, `aria-expanded`, and `aria-controls` attributes are applied to the toggle to support accessibility. * The result of the `ref` placed on the toggle must be an instance of `HTMLElement`. Results that are instances * of class components are not supported. * Also see ["Forwarding Refs"](https://reactjs.org/docs/forwarding-refs.html). */ toggle: DropdownToggleElement; } interface DropdownPropsBaseControlled extends DropdownPropsBase { onRequestClose: DropdownRequestCloseHandler; onRequestOpen: DropdownRequestOpenHandler; open: boolean; } interface DropdownPropsBaseUncontrolled extends DropdownPropsBase { open?: never; } type DropdownProps = ComponentProps; declare function Dropdown({ 'aria-labelledby': ariaLabelledBy, align, canCoverAnchor, children, closeReasons, defaultPlacement, elementRef, focusToggleReasons, inputId, open: openProp, openWithArrowKeys, onRequestClose, onRequestOpen, repositionMode, retainFocus, takeFocus, toggle, }: DropdownProps): React.JSX.Element; declare namespace Dropdown { var propTypes: { align: PropTypes.Requireable; canCoverAnchor: PropTypes.Requireable; children: PropTypes.Requireable>; closeReasons: PropTypes.Requireable<(DropdownPossibleCloseReason | null | undefined)[]>; defaultPlacement: PropTypes.Requireable; elementRef: PropTypes.Requireable; focusToggleReasons: PropTypes.Requireable<(DropdownPossibleCloseReason | null | undefined)[]>; inputId: PropTypes.Requireable; onRequestClose: PropTypes.Requireable<(...args: any[]) => any>; onRequestOpen: PropTypes.Requireable<(...args: any[]) => any>; open: PropTypes.Requireable; openWithArrowKeys: PropTypes.Requireable; repositionMode: PropTypes.Requireable; retainFocus: PropTypes.Requireable; takeFocus: PropTypes.Requireable; toggle: PropTypes.Validator; }; var possibleOpenReasons: DropdownPossibleOpenReason[]; var possibleCloseReasons: DropdownPossibleCloseReason[]; } export default Dropdown; export type { DropdownChildrenRenderer, DropdownPlacement, DropdownPlacementStatus, DropdownPossibleOpenReason, DropdownPossibleCloseReason, DropdownRequestCloseHandler, DropdownRequestOpenHandler, DropdownToggleElement, };