UNPKG

wix-style-react

Version:
232 lines (194 loc) • 9.4 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import PropTypes from 'prop-types'; import ChevronDownSmall from 'wix-ui-icons-common/ChevronDownSmall'; import { classes } from './BadgeSelect.st.css'; import DropdownLayout from '../DropdownLayout'; import Popover from '../Popover'; import Badge from '../Badge'; import { badgeSelectItemBuilder } from '../BadgeSelectItem'; import * as DATA_ATTR from './DataAttr'; import { PopoverCommonProps } from '../common/PropTypes/PopoverCommon'; var BadgeSelect = /*#__PURE__*/function (_React$Component) { _inherits(BadgeSelect, _React$Component); var _super = _createSuper(BadgeSelect); /** * Determine if a certain key should open the DropdownLayout * * @param {KeyboardEvent.key} key - The key name * @return {boolean} - Whether the key should cause the DropdownLayout to open */ function BadgeSelect(_props) { var _this; _classCallCheck(this, BadgeSelect); _this = _super.call(this, _props); _defineProperty(_assertThisInitialized(_this), "_isControlled", function () { return typeof _this.props.selectedId !== 'undefined'; }); _defineProperty(_assertThisInitialized(_this), "_isOpenKey", function (key) { return ['Enter', 'Spacebar', ' ', 'ArrowDown'].includes(key); }); _defineProperty(_assertThisInitialized(_this), "_getBadgeOptionById", function (options, wantedId) { return options.find(function (_ref) { var id = _ref.id; return id === wantedId; }); }); _defineProperty(_assertThisInitialized(_this), "_handleSelect", function (_ref2) { var selectedId = _ref2.id; var _this$props = _this.props, onSelect = _this$props.onSelect, options = _this$props.options; var selectedBadge = _this._getBadgeOptionById(options, selectedId); var newState = { visible: false }; if (!_this._isControlled()) { newState.selectedBadge = selectedBadge; } _this.setState(newState); onSelect && onSelect(selectedBadge); }); _defineProperty(_assertThisInitialized(_this), "_onKeyDown", function (event) { // Delegate the event to the DropdownLayout. It'll handle the navigation, // option selection and closing of the dropdown. var isHandledByDropdownLayout = _this.dropdownLayout ? _this.dropdownLayout._onKeyDown(event) : false; // If the event wasn't handled by the DropdownLayout correctly, check if // the pressed key should open the dropdown and act accordingly. if (!isHandledByDropdownLayout) { if (_this._isOpenKey(event.key)) { _this.showDropdown(); event.preventDefault(); } } }); _defineProperty(_assertThisInitialized(_this), "getSelectedOption", function (props) { return _this._getBadgeOptionById(props.options, props.selectedId) || props.options[0]; }); _defineProperty(_assertThisInitialized(_this), "hideDropdown", function () { _this.setState({ visible: false }); }); _defineProperty(_assertThisInitialized(_this), "showDropdown", function () { _this.setState({ visible: true }); }); _defineProperty(_assertThisInitialized(_this), "toggleDropdown", function () { _this.setState({ visible: !_this.state.visible }); }); _this.state = { visible: false, selectedBadge: _this.getSelectedOption(_props) }; return _this; } _createClass(BadgeSelect, [{ key: "options", get: function get() { var options = this.props.options; return Array.isArray(options) ? options.map(function (option) { return badgeSelectItemBuilder(_objectSpread({}, option)); }) : []; } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { if (this.props.selectedId !== nextProps.selectedId) { this.setState({ selectedBadge: this.getSelectedOption(nextProps) }); } } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, type = _this$props2.type, size = _this$props2.size, uppercase = _this$props2.uppercase, dataHook = _this$props2.dataHook, popoverProps = _this$props2.popoverProps; var _this$state = this.state, visible = _this$state.visible, selectedBadge = _this$state.selectedBadge; return /*#__PURE__*/React.createElement(Popover, _extends({ shown: visible, dataHook: dataHook, placement: "bottom", onKeyDown: this._onKeyDown, onClickOutside: this.hideDropdown }, popoverProps, { className: classes.root }), /*#__PURE__*/React.createElement(Popover.Element, null, /*#__PURE__*/React.createElement(Badge, { ref: function ref(badge) { return _this2.badge = badge; }, type: type, size: size, uppercase: uppercase, suffixIcon: /*#__PURE__*/React.createElement(ChevronDownSmall, null), skin: this.state.selectedBadge.skin, onClick: this.toggleDropdown, dataHook: DATA_ATTR.DATA_BADGE }, this.state.selectedBadge.text)), /*#__PURE__*/React.createElement(Popover.Content, null, /*#__PURE__*/React.createElement(DropdownLayout, { ref: function ref(r) { return _this2.dropdownLayout = r; }, dataHook: DATA_ATTR.DATA_DROPDOWN, visible: true, selectedId: selectedBadge.id, options: this.options, onSelect: this._handleSelect, onClose: this.hideDropdown, inContainer: true }))); } }]); return BadgeSelect; }(React.Component); BadgeSelect.displayName = 'BadgeSelect'; BadgeSelect.propTypes = { /** An array of options. Each option must have a unique `id`, a `text` and a `skin` whose value should match one of `<Badge/>`'s skin values */ options: PropTypes.arrayOf(PropTypes.shape({ id: PropTypes.string.isRequired, skin: PropTypes.oneOf(['general', 'standard', 'danger', 'success', 'neutral', 'neutralLight', 'warning', 'warningLight', 'urgent', 'neutralStandard', 'neutralSuccess', 'neutralDanger', 'premium']).isRequired, text: PropTypes.string.isRequired, subtitle: PropTypes.string, ellipsis: PropTypes.bool })).isRequired, /** The id of the selected option in the list */ selectedId: PropTypes.string, /** Callback function called whenever the user selects a different option in the list */ onSelect: PropTypes.func, /** The size of the `<Badge/>` */ size: PropTypes.oneOf(['small', 'medium']), /** The type of the `<Badge/>` */ type: PropTypes.oneOf(['solid', 'outlined', 'transparent']), /** Whether the text of the `<Badge/>` should be uppercase */ uppercase: PropTypes.bool, /** Applied as data-hook HTML attribute that can be used to create driver in testing */ dataHook: PropTypes.string, /** common popover props */ popoverProps: PropTypes.shape(PopoverCommonProps) }; BadgeSelect.defaultProps = { size: 'medium', type: 'solid', uppercase: true }; export default BadgeSelect;