wix-style-react
Version:
wix-style-react
168 lines (136 loc) • 7.23 kB
JavaScript
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 _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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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; }
import React from 'react';
import PropTypes from 'prop-types';
import WixComponent from '../BaseComponents/WixComponent';
import DropdownLayout from '../DropdownLayout/DropdownLayout';
import styles from './IconWithOptions.scss';
import classNames from 'classnames';
import deprecationLog from '../utils/deprecationLog';
var IconWithOptions = function (_WixComponent) {
_inherits(IconWithOptions, _WixComponent);
function IconWithOptions(props) {
_classCallCheck(this, IconWithOptions);
var _this = _possibleConstructorReturn(this, (IconWithOptions.__proto__ || Object.getPrototypeOf(IconWithOptions)).call(this, props));
_this.state = { showOptions: false };
_this.onSelect = _this.onSelect.bind(_this);
_this.sortChildren(props);
deprecationLog('Using "<IconWithOptions/>" is deprecated. Instead, we advise you to use the newer "<DropdownPopover/>" component. Please refer to it\'s documentation.');
return _this;
}
_createClass(IconWithOptions, [{
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.iconElement = _React$Children$toArr2[0];
this.optionsElement = _React$Children$toArr2.slice(1);
}
}, {
key: 'renderDropdownLayout',
value: function renderDropdownLayout() {
var _classNames,
_this2 = this;
/* eslint-disable no-unused-vars */
var _props = this.props,
dataHook = _props.dataHook,
dropdownProps = _objectWithoutProperties(_props, ['dataHook']);
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);
});
var classes = classNames((_classNames = {}, _defineProperty(_classNames, styles.dropdownLayout, true), _defineProperty(_classNames, styles.dropDirectionUp, dropdownProps.dropDirectionUp), _classNames));
var style = { width: dropdownProps.dropdownWidth };
return React.createElement(
'div',
{
className: classes,
style: style,
'data-hook': 'iconWithOptions-dropdownLayout-wrapper'
},
React.createElement(DropdownLayout, _extends({}, dropdownProps, {
dataHook: 'iconWithOptions-dropdownLayout',
options: dropdownLayoutOptions,
visible: this.state.showOptions,
onSelect: function onSelect(option, isSelectedOption) {
return _this2.onSelect(option, isSelectedOption);
}
}))
);
}
}, {
key: 'render',
value: function render() {
var _classNames2,
_this3 = this;
var _props2 = this.props,
dropDirectionUp = _props2.dropDirectionUp,
dropdownWidth = _props2.dropdownWidth;
var style = { width: dropdownWidth };
var classes = classNames((_classNames2 = {}, _defineProperty(_classNames2, styles.wrapper, true), _defineProperty(_classNames2, styles.hover, this.state.showOptions), _classNames2));
return React.createElement(
'div',
{
className: classes,
style: style,
onMouseLeave: function onMouseLeave() {
return _this3.setState({ showOptions: false });
}
},
dropDirectionUp ? this.renderDropdownLayout() : null,
React.createElement(
'div',
{
'data-hook': 'icon-wrapper',
className: styles.iconWrapper,
onMouseEnter: function onMouseEnter() {
return _this3.setState({ showOptions: true });
}
},
this.iconElement
),
!dropDirectionUp ? this.renderDropdownLayout() : null
);
}
}, {
key: 'onSelect',
value: function onSelect(option, isSelectedOption) {
if (!isSelectedOption) {
this.props.onSelect(option);
this.setState({ showOptions: false });
}
}
}]);
return IconWithOptions;
}(WixComponent);
IconWithOptions.defaultProps = _extends({}, DropdownLayout.defaultProps, {
onSelect: function onSelect() {},
withArrow: true,
dropdownWidth: '130px'
});
IconWithOptions.propTypes = _extends({}, DropdownLayout.propTypes, {
dropdownWidth: PropTypes.string,
children: PropTypes.array.isRequired
});
IconWithOptions.Option = function () {
return null;
};
IconWithOptions.Option.displayName = 'IconWithOptions.Option';
IconWithOptions.Icon = function (props) {
return React.createElement('div', props);
};
IconWithOptions.Icon.displayName = 'IconWithOptions.Icon';
export default IconWithOptions;