wix-style-react
Version:
wix-style-react
195 lines (148 loc) • 8.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
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 _IconWithOptions = require('./IconWithOptions.scss');
var _IconWithOptions2 = _interopRequireDefault(_IconWithOptions);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _deprecationLog = require('../utils/deprecationLog');
var _deprecationLog2 = _interopRequireDefault(_deprecationLog);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
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);
(0, _deprecationLog2.default)('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 = _react2.default.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 = _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);
});
var classes = (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, _IconWithOptions2.default.dropdownLayout, true), _defineProperty(_classNames, _IconWithOptions2.default.dropDirectionUp, dropdownProps.dropDirectionUp), _classNames));
var style = { width: dropdownProps.dropdownWidth };
return _react2.default.createElement(
'div',
{
className: classes,
style: style,
'data-hook': 'iconWithOptions-dropdownLayout-wrapper'
},
_react2.default.createElement(_DropdownLayout2.default, _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 = (0, _classnames2.default)((_classNames2 = {}, _defineProperty(_classNames2, _IconWithOptions2.default.wrapper, true), _defineProperty(_classNames2, _IconWithOptions2.default.hover, this.state.showOptions), _classNames2));
return _react2.default.createElement(
'div',
{
className: classes,
style: style,
onMouseLeave: function onMouseLeave() {
return _this3.setState({ showOptions: false });
}
},
dropDirectionUp ? this.renderDropdownLayout() : null,
_react2.default.createElement(
'div',
{
'data-hook': 'icon-wrapper',
className: _IconWithOptions2.default.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;
}(_WixComponent3.default);
IconWithOptions.defaultProps = _extends({}, _DropdownLayout2.default.defaultProps, {
onSelect: function onSelect() {},
withArrow: true,
dropdownWidth: '130px'
});
IconWithOptions.propTypes = _extends({}, _DropdownLayout2.default.propTypes, {
dropdownWidth: _propTypes2.default.string,
children: _propTypes2.default.array.isRequired
});
IconWithOptions.Option = function () {
return null;
};
IconWithOptions.Option.displayName = 'IconWithOptions.Option';
IconWithOptions.Icon = function (props) {
return _react2.default.createElement('div', props);
};
IconWithOptions.Icon.displayName = 'IconWithOptions.Icon';
exports.default = IconWithOptions;