UNPKG

wix-style-react

Version:
252 lines (202 loc) • 9.98 kB
var _class2, _temp; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React from 'react'; import PropTypes from 'prop-types'; import WixComponent from '../BaseComponents/WixComponent'; import DropdownLayout from '../DropdownLayout/DropdownLayout'; import Button from '../Button'; import ButtonLayout from '../ButtonLayout'; import ChevronDown from '../new-icons/ChevronDown'; import styles from './ButtonWithOptions.scss'; import deprecationLog from '../utils/deprecationLog'; /** * A simple dropdown with button trigger * * Composed of special `children`: * * `<ButtonWithOptions.Button>` - the Button component to be used * * `<ButtonWithOptions.Option>` - an option to be used for the dropdown - must contain an id */ var ButtonWithOptions = function (_WixComponent) { _inherits(ButtonWithOptions, _WixComponent); function ButtonWithOptions(props) { _classCallCheck(this, ButtonWithOptions); var _this = _possibleConstructorReturn(this, (ButtonWithOptions.__proto__ || Object.getPrototypeOf(ButtonWithOptions)).call(this, props)); _this.hideOptions = function () { return _this.setState({ showOptions: false }); }; _this.showOptions = function () { return _this.setState({ showOptions: true }); }; _this.onSelect = function (option, sameOptionSelected) { _this.hideOptions(); _this.props.onSelect(option, sameOptionSelected); }; _this.state = { showOptions: false, selectedId: props.selectedId }; if (props.children) { _this.sortChildren(props); } deprecationLog('Using "<ButtonWithOptions/>" is deprecated. Instead, we advise you to use the newer "<DropdownPopover/>" component. Please refer to it\'s documentation.'); return _this; } _createClass(ButtonWithOptions, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { this.sortChildren(nextProps); } }, { key: 'sortChildren', value: function sortChildren(props) { var _React$Children$toArr = React.Children.toArray(props.children); var _React$Children$toArr2 = _toArray(_React$Children$toArr); this.buttonElement = _React$Children$toArr2[0]; this.optionsElement = _React$Children$toArr2.slice(1); } }, { key: 'cleanOptionToSimpleTextForm', value: function cleanOptionToSimpleTextForm(children) { var supportedElements = ['string', 'span']; if (typeof children === 'string') { return children; } children = Array.isArray(children) ? children : [children]; var filteredChildren = children.filter(function (child) { return supportedElements.includes(child.type || (typeof child === 'undefined' ? 'undefined' : _typeof(child))); }); return filteredChildren; } }, { key: 'getSelectedOptionValue', value: function getSelectedOptionValue() { var children = this.buttonElement.props.children; var selectedId = this.state.selectedId; var theme = this.props.theme; if (theme.indexOf('no-border') === -1 || selectedId < 0) { return children; } var childrenArr = React.Children.toArray(this.props.children); var selectedOption = childrenArr.find(function (_ref) { var id = _ref.props.id; return id === selectedId; }); return [this.cleanOptionToSimpleTextForm(selectedOption.props.children), React.createElement( 'span', { key: 1, className: styles.icon }, React.createElement(ChevronDown, null) )]; } }, { key: 'renderButton', value: function renderButton() { return React.cloneElement(this.buttonElement, { onClick: this.showOptions, children: this.getSelectedOptionValue(), theme: this.props.theme }); } }, { key: 'renderDropdownLayout', value: function renderDropdownLayout() { var _this2 = this; /* eslint-disable no-unused-vars */ var _props = this.props, dataHook = _props.dataHook, restrainDropdownSize = _props.restrainDropdownSize, dropdownProps = _objectWithoutProperties(_props, ['dataHook', 'restrainDropdownSize']); var dropdownLayoutOptions = React.Children.map(this.optionsElement, function (option) { var _option$props = option.props, value = _option$props.children, rest = _objectWithoutProperties(_option$props, ['children']); return _extends({ value: value }, rest); }); return React.createElement(DropdownLayout, _extends({}, dropdownProps, { dataHook: 'buttonWithOptions-dropdownLayout', options: dropdownLayoutOptions, theme: this.props.theme, visible: this.state.showOptions, onSelect: function onSelect(option, sameOptionSelected) { _this2.setState({ selectedId: option.id }); _this2.onSelect(option, sameOptionSelected); }, onClickOutside: this.hideOptions, selectedId: this.state.selectedId })); } }, { key: 'render', value: function render() { var dropDirectionUp = this.props.dropDirectionUp; var sizeRestrictionStyles = this.props.restrainDropdownSize ? { display: 'inline-block' } : {}; return React.createElement( 'div', { style: sizeRestrictionStyles }, dropDirectionUp ? this.renderDropdownLayout() : null, this.renderButton(), !dropDirectionUp ? this.renderDropdownLayout() : null ); } }]); return ButtonWithOptions; }(WixComponent); ButtonWithOptions.defaultProps = _extends({}, DropdownLayout.defaultProps, { onSelect: function onSelect() {}, restrainDropdownSize: true, theme: ButtonLayout.defaultProps.theme }); ButtonWithOptions.propTypes = _extends({}, DropdownLayout.propTypes, { restrainDropdownSize: PropTypes.bool, /** * First children must be `<ButtonWithOptions.Button>` - its children are used as trigger component for dropdown * * all following children must be `<ButtonWithOptions.Option>` with required `id` prop. These will be displayed in * dropdown */ children: PropTypes.arrayOf(function (propValue, key) { if (key === 0 && propValue[key].type !== ButtonWithOptions.Button) { return new Error('ButtonWithOptions: Invalid Prop children, first child must be ButtonWithOptions.Button'); } if (key !== 0) { React.Children.forEach(propValue[key], function (item) { if (item.type !== ButtonWithOptions.Option) { return new Error('ButtonWithOptions: Invalid Prop children was given. Validation failed on child number ' + key); } }); } }) }); ButtonWithOptions.Button = function (props) { return React.createElement( 'div', { 'data-hook': 'buttonWithOptions-button-wrapper' }, React.createElement(Button, props) ); }; ButtonWithOptions.Button.displayName = 'ButtonWithOptions.Button'; ButtonWithOptions.Option = (_temp = _class2 = function (_React$Component) { _inherits(Option, _React$Component); function Option() { _classCallCheck(this, Option); return _possibleConstructorReturn(this, (Option.__proto__ || Object.getPrototypeOf(Option)).apply(this, arguments)); } _createClass(Option, [{ key: 'render', value: function render() { return null; } }]); return Option; }(React.Component), _class2.displayName = 'ButtonWithOptions.Option', _class2.propTypes = { children: function children(props, propName, componentName) { var prop = props[propName]; if (React.Children.count(prop) !== 1) { return new Error(componentName + ': Should have a single child'); } } }, _temp); export default ButtonWithOptions;