@atlaskit/select
Version:
Select allows users to make a single selection or multiple selections from a list of options.
93 lines (92 loc) • 5.34 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/extends";
var _excluded = ["ariaLiveMessages", "components", "isInvalid", "menuRenderMode", "onClickPreventDefault", "tabSelectsValue", "validationState"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { forwardRef, useImperativeHandle, useMemo, useRef } from 'react';
var InlineMenuPortal = function InlineMenuPortal(_ref) {
var children = _ref.children;
return children;
};
var InlineMenu = function InlineMenu(_ref2) {
var children = _ref2.children,
innerProps = _ref2.innerProps;
return /*#__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) {
var AtlaskitSelect = /*#__PURE__*/forwardRef(function AtlaskitSelect(props, forwardedRef) {
var ariaLiveMessages = props.ariaLiveMessages,
components = props.components,
isInvalid = props.isInvalid,
_props$menuRenderMode = props.menuRenderMode,
menuRenderMode = _props$menuRenderMode === void 0 ? 'popup' : _props$menuRenderMode,
_props$onClickPrevent = props.onClickPreventDefault,
onClickPreventDefault = _props$onClickPrevent === void 0 ? true : _props$onClickPrevent,
_props$tabSelectsValu = props.tabSelectsValue,
tabSelectsValue = _props$tabSelectsValu === void 0 ? false : _props$tabSelectsValu,
_props$validationStat = props.validationState,
validationState = _props$validationStat === void 0 ? 'default' : _props$validationStat,
restProps = _objectWithoutProperties(props, _excluded);
var internalSelectRef = useRef(null);
var inlineMenuComponents = useMemo(function () {
return _objectSpread(_objectSpread({}, 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, function () {
return {
select: internalSelectRef.current,
focus: function focus() {
var _internalSelectRef$cu;
return (_internalSelectRef$cu = internalSelectRef.current) === null || _internalSelectRef$cu === void 0 ? void 0 : _internalSelectRef$cu.focus();
},
blur: function 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;
}