UNPKG

@wordpress/components

Version:
182 lines (174 loc) 6.87 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var Ariakit = _interopRequireWildcard(require("@ariakit/react")); var _clsx = _interopRequireDefault(require("clsx")); var _compose = require("@wordpress/compose"); var _i18n = require("@wordpress/i18n"); var _customSelect = _interopRequireDefault(require("../custom-select-control-v2/custom-select")); var _item = _interopRequireDefault(require("../custom-select-control-v2/item")); var Styled = _interopRequireWildcard(require("../custom-select-control-v2/styles")); var _visuallyHidden = require("../visually-hidden"); var _deprecated36pxSize = require("../utils/deprecated-36px-size"); var _jsxRuntime = require("react/jsx-runtime"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ function useDeprecatedProps({ __experimentalShowSelectedHint, ...otherProps }) { return { showSelectedHint: __experimentalShowSelectedHint, ...otherProps }; } // The removal of `__experimentalHint` in favour of `hint` doesn't happen in // the `useDeprecatedProps` hook in order not to break consumers that rely // on object identity (see https://github.com/WordPress/gutenberg/pull/63248#discussion_r1672213131) function applyOptionDeprecations({ __experimentalHint, ...rest }) { return { hint: __experimentalHint, ...rest }; } function getDescribedBy(currentValue, describedBy) { if (describedBy) { return describedBy; } // translators: %s: The selected option. return (0, _i18n.sprintf)((0, _i18n.__)('Currently selected: %s'), currentValue); } function CustomSelectControl(props) { const { __next40pxDefaultSize = false, __shouldNotWarnDeprecated36pxSize, describedBy, options, onChange, size = 'default', value, className: classNameProp, showSelectedHint = false, ...restProps } = useDeprecatedProps(props); (0, _deprecated36pxSize.maybeWarnDeprecated36pxSize)({ componentName: 'CustomSelectControl', __next40pxDefaultSize, size, __shouldNotWarnDeprecated36pxSize }); const descriptionId = (0, _compose.useInstanceId)(CustomSelectControl, 'custom-select-control__description'); // Forward props + store from v2 implementation const store = Ariakit.useSelectStore({ async setValue(nextValue) { const nextOption = options.find(item => item.name === nextValue); if (!onChange || !nextOption) { return; } // Executes the logic in a microtask after the popup is closed. // This is simply to ensure the isOpen state matches the one from the // previous legacy implementation. await Promise.resolve(); const state = store.getState(); const changeObject = { highlightedIndex: state.renderedItems.findIndex(item => item.value === nextValue), inputValue: '', isOpen: state.open, selectedItem: nextOption, type: '' }; onChange(changeObject); }, value: value?.name, // Setting the first option as a default value when no value is provided // is already done natively by the underlying Ariakit component, // but doing this explicitly avoids the `onChange` callback from firing // on initial render, thus making this implementation closer to the v1. defaultValue: options[0]?.name }); const children = options.map(applyOptionDeprecations).map(({ name, key, hint, style, className }) => { const withHint = /*#__PURE__*/(0, _jsxRuntime.jsxs)(Styled.WithHintItemWrapper, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", { children: name }), /*#__PURE__*/(0, _jsxRuntime.jsx)(Styled.WithHintItemHint, { // Keeping the classname for legacy reasons className: "components-custom-select-control__item-hint", children: hint })] }); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_item.default, { value: name, children: hint ? withHint : name, style: style, className: (0, _clsx.default)(className, // Keeping the classnames for legacy reasons 'components-custom-select-control__item', { 'has-hint': hint }) }, key); }); const currentValue = Ariakit.useStoreState(store, 'value'); const renderSelectedValueHint = () => { const selectedOptionHint = options?.map(applyOptionDeprecations)?.find(({ name }) => currentValue === name)?.hint; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Styled.SelectedExperimentalHintWrapper, { children: [currentValue, selectedOptionHint && /*#__PURE__*/(0, _jsxRuntime.jsx)(Styled.SelectedExperimentalHintItem, { // Keeping the classname for legacy reasons className: "components-custom-select-control__hint", children: selectedOptionHint })] }); }; const translatedSize = (() => { if (__next40pxDefaultSize && size === 'default' || size === '__unstable-large') { return 'default'; } if (!__next40pxDefaultSize && size === 'default') { return 'compact'; } return size; })(); return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_customSelect.default, { "aria-describedby": descriptionId, renderSelectedValue: showSelectedHint ? renderSelectedValueHint : undefined, size: translatedSize, store: store, className: (0, _clsx.default)( // Keeping the classname for legacy reasons 'components-custom-select-control', classNameProp), isLegacy: true, ...restProps, children: children }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_visuallyHidden.VisuallyHidden, { children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", { id: descriptionId, children: getDescribedBy(currentValue, describedBy) }) })] }); } var _default = exports.default = CustomSelectControl; //# sourceMappingURL=index.js.map