UNPKG

@atlaskit/select

Version:

Select allows users to make a single selection or multiple selections from a list of options.

82 lines (81 loc) 3.64 kB
import _extends from "@babel/runtime/helpers/extends"; import React, { forwardRef, useImperativeHandle, useMemo, useRef } from 'react'; const InlineMenuPortal = ({ children }) => children; const InlineMenu = ({ children, innerProps }) => /*#__PURE__*/React.createElement("div", _extends({}, innerProps, { // MenuList owns this ID; forwarding it here would create duplicate listbox IDs. id: undefined }), children); export default function createSelect(WrappedComponent) { const AtlaskitSelect = /*#__PURE__*/forwardRef(function AtlaskitSelect(props, forwardedRef) { const { ariaLiveMessages, components, isInvalid, // TODO: set to true when cleaning up validationState prop so it has a default value menuRenderMode = 'popup', onClickPreventDefault = true, tabSelectsValue = false, validationState = 'default', ...restProps } = props; const internalSelectRef = useRef(null); const inlineMenuComponents = useMemo(() => ({ ...components, MenuPortal: InlineMenuPortal, Menu: InlineMenu }), [components]); /** * The following `useImperativeHandle` hook exists for the sake of backwards compatibility. * This component used to be a class component which set the value of the `ref` prop to object with the properties and value as below. * This has lead to slightly odd usage of refs with this component, e.g. `myRef.current.select.select.controlRef` instead of just `myRef.current.select.controlRef` * In the next major release, this should removed and the ref should be passed directly to the wrapped component (given users have updated usage) * More info https://stash.atlassian.com/projects/ATLASSIAN/repos/atlassian-frontend-monorepo/pull-requests/88021/overview */ useImperativeHandle(forwardedRef, () => ({ select: internalSelectRef.current, focus: () => { var _internalSelectRef$cu; return (_internalSelectRef$cu = internalSelectRef.current) === null || _internalSelectRef$cu === void 0 ? void 0 : _internalSelectRef$cu.focus(); }, blur: () => { var _internalSelectRef$cu2; return (_internalSelectRef$cu2 = internalSelectRef.current) === null || _internalSelectRef$cu2 === void 0 ? void 0 : _internalSelectRef$cu2.blur(); } }), []); if (menuRenderMode === 'inline') { return /*#__PURE__*/React.createElement(WrappedComponent, _extends({ ref: internalSelectRef, ariaLiveMessages: ariaLiveMessages, onClickPreventDefault: onClickPreventDefault, isInvalid: isInvalid || validationState === 'error' }, restProps, { components: inlineMenuComponents, menuIsOpen: true, tabSelectsValue: tabSelectsValue // indicates react-select to be async by default using the base Select component // so that makers can pass all async props on the base select to async load options. , isAsync: true })); } return /*#__PURE__*/React.createElement(WrappedComponent, _extends({ ref: internalSelectRef, ariaLiveMessages: ariaLiveMessages, tabSelectsValue: tabSelectsValue, onClickPreventDefault: onClickPreventDefault, isInvalid: isInvalid || validationState === 'error', components: components }, restProps, { // indicates react-select to be async by default using the base Select component // so that makers can pass all async props on the base select to async load options. isAsync: true })); }); AtlaskitSelect.displayName = 'AtlaskitSelect'; return AtlaskitSelect; }