wix-style-react
Version:
wix-style-react
112 lines (95 loc) • 4.37 kB
JavaScript
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 _class, _temp;
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 DropdownLayout from '../../DropdownLayout';
import Text from '../../Text';
import ChevronDown from '../../new-icons/ChevronDown';
import styles from './styles.scss';
var DropdownPicker = (_temp = _class = function (_React$Component) {
_inherits(DropdownPicker, _React$Component);
function DropdownPicker(props) {
_classCallCheck(this, DropdownPicker);
var _this = _possibleConstructorReturn(this, (DropdownPicker.__proto__ || Object.getPrototypeOf(DropdownPicker)).call(this, props));
_this.onClose = function () {
_this.setState({
isOpen: false
});
_this.stopAllEventsThatCanOpenModalInSameEventLoop = true;
setTimeout(function () {
// for next event loop we allow them
_this.stopAllEventsThatCanOpenModalInSameEventLoop = false;
});
};
_this.onSelect = function (data) {
_this.props.onChange(data);
_this.onClose();
};
_this.toggleDropdown = function () {
if (!_this.stopAllEventsThatCanOpenModalInSameEventLoop) {
_this.setState({
isOpen: !_this.state.isOpen
});
}
};
_this.stopAllEventsThatCanOpenModalInSameEventLoop = false;
_this.state = {
isOpen: false
};
return _this;
}
_createClass(DropdownPicker, [{
key: 'render',
value: function render() {
var _props = this.props,
caption = _props.caption,
options = _props.options,
dataHook = _props.dataHook,
selectedId = _props.selectedId;
var isOpen = this.state.isOpen;
return React.createElement(
'div',
{ 'data-hook': dataHook, className: styles.root },
React.createElement(
'div',
{ className: styles.button, onClick: this.toggleDropdown },
React.createElement(
Text,
{ dataHook: dataHook + '-button' },
caption
),
React.createElement(
'div',
{ className: styles.icon },
React.createElement(ChevronDown, null)
)
),
isOpen && React.createElement(
'div',
{ className: styles.dropdown },
React.createElement(DropdownLayout, {
dataHook: dataHook + '-menu',
focusOnSelectedOption: true,
visible: isOpen,
options: options,
onSelect: this.onSelect,
onClickOutside: this.onClose,
closeOnSelect: true,
selectedId: selectedId
})
)
);
}
}]);
return DropdownPicker;
}(React.Component), _class.propTypes = {
dataHook: PropTypes.string,
caption: PropTypes.node,
options: PropTypes.array,
onChange: PropTypes.func,
selectedId: PropTypes.number
}, _temp);
export { DropdownPicker as default };