UNPKG

@carbon/react

Version:

React components for the Carbon Design System

90 lines (81 loc) 3.31 kB
/** * 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. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var iconsReact = require('@carbon/icons-react'); var cx = require('classnames'); var PropTypes = require('prop-types'); var React = require('react'); var useId = require('../../internal/useId.js'); 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 PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes); var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var _ChevronDown; const SideNavSwitcher = /*#__PURE__*/React.forwardRef(function SideNavSwitcher(props, ref) { const id = useId.useId('side-nav-switcher'); const prefix = usePrefix.usePrefix(); const { className: customClassName, labelText, onChange, options } = props; const className = cx__default["default"](`${prefix}--side-nav__switcher`, customClassName); // Note for usage around `onBlur`: https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-onchange.md return /*#__PURE__*/React__default["default"].createElement("div", { className: className }, /*#__PURE__*/React__default["default"].createElement("label", { htmlFor: id, className: `${prefix}--assistive-text` }, labelText), /*#__PURE__*/React__default["default"].createElement("select", { id: id, className: `${prefix}--side-nav__select`, defaultValue: "", onBlur: onChange, onChange: onChange, ref: ref }, /*#__PURE__*/React__default["default"].createElement("option", { className: `${prefix}--side-nav__option`, disabled: true, hidden: true, value: "" }, labelText), options.map(option => /*#__PURE__*/React__default["default"].createElement("option", { key: option, className: `${prefix}--side-nav__option`, value: option }, option))), /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--side-nav__switcher-chevron` }, _ChevronDown || (_ChevronDown = /*#__PURE__*/React__default["default"].createElement(iconsReact.ChevronDown, { size: 20 })))); }); SideNavSwitcher.displayName = 'SideNavSwitcher'; SideNavSwitcher.propTypes = { /** * Provide an optional class to be applied to the containing node */ className: PropTypes__default["default"].string, /** * Provide the label for the switcher. This will be the first visible option * when someone views this control */ labelText: PropTypes__default["default"].string.isRequired, /** * Provide a callback function that is called whenever the switcher value is * updated */ onChange: PropTypes__default["default"].func, /** * Provide an array of options to be rendered in the switcher as an * `<option>`. The text value will be what is displayed to the user and is set * as the `value` prop for each `<option>`. */ options: PropTypes__default["default"].arrayOf(PropTypes__default["default"].string).isRequired }; exports["default"] = SideNavSwitcher;