UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

80 lines (75 loc) 2.33 kB
/** * Copyright IBM Corp. 2020, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js'; import React__default from 'react'; import PropTypes from '../../_virtual/index.js'; import { unstable_FeatureFlags, OverflowMenu, MenuItem } from '@carbon/react'; import { ArrowsVertical } from '@carbon/react/icons'; import { pkg } from '../../settings.js'; const blockClass = `${pkg.prefix}--add-select-sort`; const componentName = 'AddSelectSort'; const AddSelectSort = _ref => { let { setSortAttribute, setSortDirection, sortBy, sortByLabel } = _ref; const sortByOpts = sortBy ? sortBy?.reduce((acc, cur) => { const opts = [{ id: `${cur}-asc`, label: cur, direction: 'asc', attribute: cur }, { id: `${cur}-desc`, label: cur, direction: 'desc', attribute: cur }]; return [...acc, ...opts]; }, []) : []; // handlers const sortHandler = _ref2 => { let { direction, attribute } = _ref2; setSortAttribute?.(attribute); setSortDirection?.(direction); }; return /*#__PURE__*/React__default.createElement("div", { className: blockClass }, sortByOpts.length > 0 && /*#__PURE__*/React__default.createElement(unstable_FeatureFlags, { enableV12Overflowmenu: true }, /*#__PURE__*/React__default.createElement(OverflowMenu // @ts-ignore // TODO: remove after carbon fixes this ts error , { autoAlign: true, menuAlignment: "bottom-end", renderIcon: props => /*#__PURE__*/React__default.createElement(ArrowsVertical, _extends({ size: 32 }, props)), className: `${blockClass}_overflow`, label: sortByLabel }, sortByOpts.map(opt => { return /*#__PURE__*/React__default.createElement(MenuItem, { className: `${blockClass}_overflow-item`, key: opt?.id, label: `${opt?.label} ${opt?.direction}`, onClick: () => sortHandler(opt) }); })))); }; AddSelectSort.propTypes = { setSortAttribute: PropTypes.func, setSortDirection: PropTypes.func, sortBy: PropTypes.array, sortByLabel: PropTypes.string }; AddSelectSort.displayName = componentName; export { AddSelectSort };