UNPKG

wix-style-react

Version:
158 lines (129 loc) • 5.56 kB
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 _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 { st, classes } from './DatePickerDropdown.st.css'; import ChevronDown from 'wix-ui-icons-common/ChevronDown'; import DropdownBase from '../../DropdownBase'; import TextButton from '../../TextButton'; var DropdownPicker = /*#__PURE__*/function (_React$Component) { _inherits(DropdownPicker, _React$Component); var _super = _createSuper(DropdownPicker); function DropdownPicker() { var _this; _classCallCheck(this, DropdownPicker); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "state", { open: false }); _defineProperty(_assertThisInitialized(_this), "_onSelect", function (data) { var onChange = _this.props.onChange; _this.setState({ open: false }, function () { if (typeof onChange === 'function') onChange(data); }); }); _defineProperty(_assertThisInitialized(_this), "_toggle", function () { _this.setState({ open: !_this.state.open }); }); _defineProperty(_assertThisInitialized(_this), "_onKeyDown", function (e, delegateKeyDown) { var eventWasHandled = delegateKeyDown(e); var open = _this.state.open; // We'll open the list when pressing the ArrowDown key if (!eventWasHandled && e.key === 'ArrowDown') { _this._open(); e.preventDefault(); return; } // close on Escape if (e.key === 'Escape') { _this._close(); e.preventDefault(); } // prevent TextButton onClick event if (open && (e.key === 'Enter' || e.key === 'Spacebar' || e.key === ' ')) { e.preventDefault(); } }); _defineProperty(_assertThisInitialized(_this), "_close", function () { _this.setState({ open: false }); }); _defineProperty(_assertThisInitialized(_this), "_open", function () { _this.setState({ open: true }); }); return _this; } _createClass(DropdownPicker, [{ key: "render", value: function render() { var _this2 = this; var _this$props = this.props, className = _this$props.className, caption = _this$props.caption, options = _this$props.options, dataHook = _this$props.dataHook, selectedId = _this$props.selectedId, ariaLabel = _this$props.ariaLabel, ariaLabelledBy = _this$props.ariaLabelledBy; var open = this.state.open; var finalAriaLabel = ariaLabel ? "".concat(ariaLabel, " ").concat(caption) : undefined; return /*#__PURE__*/React.createElement("div", { className: st(classes.root, className) }, /*#__PURE__*/React.createElement(DropdownBase, { "data-hook": dataHook, className: classes.dropdown, options: options, onClickOutside: this._close, dynamicWidth: true, minWidth: 120, selectedId: selectedId, onSelect: this._onSelect, focusOnSelectedOption: true, open: open }, function (_ref) { var delegateKeyDown = _ref.delegateKeyDown; return /*#__PURE__*/React.createElement(TextButton, { className: classes.caption, skin: "dark", size: "small", suffixIcon: /*#__PURE__*/React.createElement(ChevronDown, null), onClick: _this2._toggle, dataHook: "".concat(dataHook, "-button"), onKeyDown: function onKeyDown(e) { return _this2._onKeyDown(e, delegateKeyDown); }, ariaLabel: finalAriaLabel, ariaLabelledBy: ariaLabelledBy, ariaHaspopup: "listbox", ariaExpanded: open }, caption); })); } }]); return DropdownPicker; }(React.Component); _defineProperty(DropdownPicker, "propTypes", { dataHook: PropTypes.string, className: PropTypes.string, caption: PropTypes.node, options: PropTypes.array, onChange: PropTypes.func, selectedId: PropTypes.number, ariaLabel: PropTypes.string, ariaLabelledBy: PropTypes.string }); export { DropdownPicker as default };