@carbon/react
Version:
React components for the Carbon Design System
76 lines (67 loc) • 2.65 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var cx = require('classnames');
var React = require('react');
var PropTypes = require('prop-types');
var iconsReact = require('@carbon/icons-react');
var usePrefix = require('../../../internal/usePrefix.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx);
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
var _ChevronDown;
const translationIds = {
'close.menu': 'close.menu',
'open.menu': 'open.menu'
};
const defaultTranslations = {
[translationIds['close.menu']]: 'Close',
[translationIds['open.menu']]: 'Open'
};
const defaultTranslateWithId = id => defaultTranslations[id];
/**
* `ListBoxTrigger` is used to orient the icon up or down depending on the
* state of the menu for a given `ListBox`
*/
const ListBoxTrigger = /*#__PURE__*/React__default["default"].forwardRef(function ListBoxTrigger({
isOpen,
translateWithId: t = defaultTranslateWithId,
...rest
}, ref) {
const prefix = usePrefix.usePrefix();
const className = cx__default["default"]({
[`${prefix}--list-box__menu-icon`]: true,
[`${prefix}--list-box__menu-icon--open`]: isOpen
});
const description = isOpen ? t('close.menu') : t('open.menu');
return /*#__PURE__*/React__default["default"].createElement("button", _rollupPluginBabelHelpers["extends"]({}, rest, {
"aria-label": description,
title: description,
className: className,
type: "button",
tabIndex: -1,
ref: ref
}), _ChevronDown || (_ChevronDown = /*#__PURE__*/React__default["default"].createElement(iconsReact.ChevronDown, null)));
});
ListBoxTrigger.propTypes = {
/**
* Specify whether the menu is currently open, which will influence the
* direction of the menu icon
*/
isOpen: PropTypes__default["default"].bool.isRequired,
/**
* i18n hook used to provide the appropriate description for the given menu
* icon. This function takes in an id defined in `translationIds` and should
* return a string message for that given message id.
*/
translateWithId: PropTypes__default["default"].func
};
exports["default"] = ListBoxTrigger;
exports.translationIds = translationIds;