wix-style-react
Version:
wix-style-react
282 lines (216 loc) • 11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _WixComponent2 = require('../BaseComponents/WixComponent');
var _WixComponent3 = _interopRequireDefault(_WixComponent2);
var _DropdownLayout = require('../DropdownLayout/DropdownLayout');
var _DropdownLayout2 = _interopRequireDefault(_DropdownLayout);
var _Button = require('../Button');
var _Button2 = _interopRequireDefault(_Button);
var _ButtonLayout = require('../ButtonLayout');
var _ButtonLayout2 = _interopRequireDefault(_ButtonLayout);
var _ChevronDown = require('../new-icons/ChevronDown');
var _ChevronDown2 = _interopRequireDefault(_ChevronDown);
var _ButtonWithOptions = require('./ButtonWithOptions.scss');
var _ButtonWithOptions2 = _interopRequireDefault(_ButtonWithOptions);
var _deprecationLog = require('../utils/deprecationLog');
var _deprecationLog2 = _interopRequireDefault(_deprecationLog);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
/**
* 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);
}
(0, _deprecationLog2.default)('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 = _react2.default.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 = _react2.default.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), _react2.default.createElement(
'span',
{ key: 1, className: _ButtonWithOptions2.default.icon },
_react2.default.createElement(_ChevronDown2.default, null)
)];
}
}, {
key: 'renderButton',
value: function renderButton() {
return _react2.default.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 = _react2.default.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 _react2.default.createElement(_DropdownLayout2.default, _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 _react2.default.createElement(
'div',
{ style: sizeRestrictionStyles },
dropDirectionUp ? this.renderDropdownLayout() : null,
this.renderButton(),
!dropDirectionUp ? this.renderDropdownLayout() : null
);
}
}]);
return ButtonWithOptions;
}(_WixComponent3.default);
ButtonWithOptions.defaultProps = _extends({}, _DropdownLayout2.default.defaultProps, {
onSelect: function onSelect() {},
restrainDropdownSize: true,
theme: _ButtonLayout2.default.defaultProps.theme
});
ButtonWithOptions.propTypes = _extends({}, _DropdownLayout2.default.propTypes, {
restrainDropdownSize: _propTypes2.default.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: _propTypes2.default.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) {
_react2.default.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 _react2.default.createElement(
'div',
{ 'data-hook': 'buttonWithOptions-button-wrapper' },
_react2.default.createElement(_Button2.default, 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;
}(_react2.default.Component), _class2.displayName = 'ButtonWithOptions.Option', _class2.propTypes = {
children: function children(props, propName, componentName) {
var prop = props[propName];
if (_react2.default.Children.count(prop) !== 1) {
return new Error(componentName + ': Should have a single child');
}
}
}, _temp);
exports.default = ButtonWithOptions;