wix-style-react
Version:
wix-style-react
357 lines (287 loc) • 13.2 kB
JavaScript
'use strict';
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 _class, _temp2;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _DropdownBaseSt = require('./DropdownBase.st.css');
var _DropdownBaseSt2 = _interopRequireDefault(_DropdownBaseSt);
var _Popover = require('../Popover');
var _Popover2 = _interopRequireDefault(_Popover);
var _DropdownLayout = require('../DropdownLayout');
var _DropdownLayout2 = _interopRequireDefault(_DropdownLayout);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 DropdownBase = (_temp2 = _class = function (_React$PureComponent) {
_inherits(DropdownBase, _React$PureComponent);
function DropdownBase() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, DropdownBase);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = DropdownBase.__proto__ || Object.getPrototypeOf(DropdownBase)).call.apply(_ref, [this].concat(args))), _this), _this._dropdownLayoutRef = null, _this._shouldCloseOnMouseLeave = false, _this.state = {
open: _this.props.open,
selectedId: _this.props.selectedId || _this.props.initialSelectedId || -1
}, _this._isControllingOpen = function () {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props;
return typeof props.open !== 'undefined';
}, _this._isControllingSelection = function () {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props;
return typeof props.selectedId !== 'undefined' && typeof props.onSelect !== 'undefined';
}, _this._open = function () {
!_this._isControllingOpen() && _this.setState({ open: true });
}, _this._close = function (e) {
if (_this._isControllingOpen()) {
return;
}
// If called within a `mouseleave` event on the target element, we would like to close the
// popover only on the popover's `mouseleave` event
if (e && e.type === 'mouseleave') {
// We're not using `setState` since we don't want to wait for the next render
_this._shouldCloseOnMouseLeave = true;
} else {
_this.setState({ open: false });
}
}, _this._toggle = function () {
!_this._isControllingOpen() && _this.setState(function (_ref2) {
var open = _ref2.open;
return {
open: !open
};
});
}, _this._handleClickOutside = function () {
var onClickOutside = _this.props.onClickOutside;
_this._close();
onClickOutside && onClickOutside();
}, _this._handlePopoverMouseEnter = function () {
var onMouseEnter = _this.props.onMouseEnter;
onMouseEnter && onMouseEnter();
}, _this._handlePopoverMouseLeave = function () {
var onMouseLeave = _this.props.onMouseLeave;
if (_this._shouldCloseOnMouseLeave) {
_this._shouldCloseOnMouseLeave = false;
_this.setState({
open: false
});
}
onMouseLeave && onMouseLeave();
}, _this._handleSelect = function (selectedOption) {
var newState = {};
if (!_this._isControllingOpen()) {
newState.open = false;
}
if (!_this._isControllingSelection()) {
newState.selectedId = selectedOption.id;
}
_this.setState(newState, function () {
var onSelect = _this.props.onSelect;
onSelect && onSelect(selectedOption);
});
}, _this._handleClose = function () {
if (_this.state.open) {
_this._close();
}
}, _this._getSelectedOption = function (selectedId) {
return _this.props.options.find(function (_ref3) {
var id = _ref3.id;
return id === selectedId;
});
}, _this._isOpenKey = function (key) {
return ['Enter', 'Spacebar', ' ', 'ArrowDown'].includes(key);
}, _this._handleKeyDown = function (e) {
if (_this._isControllingOpen()) {
return;
}
var isHandledByDropdownLayout = _this._delegateKeyDown(e);
if (!isHandledByDropdownLayout) {
if (_this._isOpenKey(e.key)) {
_this._open();
e.preventDefault();
}
}
}, _this._delegateKeyDown = function (e) {
if (!_this._dropdownLayoutRef) {
return false;
}
return _this._dropdownLayoutRef._onKeyDown(e);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
/**
* Return `true` if the `open` prop is being controlled
*/
/**
* Return `true` if the selection behaviour is being controlled
*/
/**
* Determine if a certain key should open the DropdownLayout
*/
/**
* A common `keydown` event that can be used for the target elements. It will automatically
* delegate the event to the underlaying <DropdownLayout/>, and will determine when to open the
* dropdown depending on the pressed key.
*/
/*
* Delegate the event to the DropdownLayout. It'll handle the navigation, option selection and
* closing of the dropdown.
*/
_createClass(DropdownBase, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
// Keep internal state updated if needed
if (this._isControllingOpen(nextProps) && this.props.open !== nextProps.open) {
this.setState({ open: nextProps.open });
}
if (this._isControllingSelection(nextProps) && this.props.selectedId !== nextProps.selectedId) {
this.setState({ selectedId: nextProps.selectedId });
}
}
}, {
key: '_renderChildren',
value: function _renderChildren() {
var children = this.props.children;
var selectedId = this.state.selectedId;
if (!children) {
return null;
}
return _react2.default.isValidElement(children) ? children // Returning the children as is when using in controlled mode
: children({
open: this._open,
close: this._close,
toggle: this._toggle,
delegateKeyDown: this._delegateKeyDown,
selectedOption: this._getSelectedOption(selectedId)
});
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var _props = this.props,
dataHook = _props.dataHook,
placement = _props.placement,
showArrow = _props.showArrow,
options = _props.options,
minWidth = _props.minWidth,
maxWidth = _props.maxWidth;
var _state = this.state,
open = _state.open,
selectedId = _state.selectedId;
return _react2.default.createElement(
_Popover2.default,
_extends({
dataHook: dataHook,
shown: open,
placement: placement,
showArrow: showArrow,
onKeyDown: this._handleKeyDown,
onMouseEnter: this._handlePopoverMouseEnter,
onMouseLeave: this._handlePopoverMouseLeave,
onClickOutside: this._handleClickOutside
}, (0, _DropdownBaseSt2.default)('root', {
withWidth: Boolean(minWidth || maxWidth),
withArrow: showArrow
}, this.props)),
_react2.default.createElement(
_Popover2.default.Element,
null,
this._renderChildren()
),
_react2.default.createElement(
_Popover2.default.Content,
null,
_react2.default.createElement(
'div',
{
style: {
maxWidth: maxWidth,
minWidth: minWidth
}
},
_react2.default.createElement(_DropdownLayout2.default, {
dataHook: 'dropdown-base-dropdownlayout',
ref: function ref(r) {
return _this2._dropdownLayoutRef = r;
},
selectedId: selectedId,
options: options,
onSelect: this._handleSelect,
onClose: this._handleClose,
inContainer: true,
visible: true
})
)
)
);
}
}]);
return DropdownBase;
}(_react2.default.PureComponent), _class.displayName = 'DropdownBase', _class.propTypes = {
dataHook: _propTypes2.default.string,
/** A controlled prop to control whether the Popover should be opened*/
open: _propTypes2.default.bool,
/** The Popover's placement */
placement: _propTypes2.default.oneOf(_Popover.placements),
/** Whether to show the Popover's arrow */
showArrow: _propTypes2.default.bool,
/** Callback function to be called on outside click */
onClickOutside: _propTypes2.default.func,
/** Callback function to be called on mouseEnter on the entire component */
onMouseEnter: _propTypes2.default.func,
/** Callback function to be called on mouseEnter onMouseLeave the entire component */
onMouseLeave: _propTypes2.default.func,
/** Callback function to be called when selecting an option. It's signature is `onSelect(selectedOption)` */
onSelect: _propTypes2.default.func,
/** The minimum width applied to the list */
minWidth: _propTypes2.default.number,
/** The maximum width applied to the list */
maxWidth: _propTypes2.default.number,
/**
* The target component to be rendered. If a regular node is paseed, it'll be rendered as-is.
* If a function is passed, it's expected to return a React element. The function accepts an
* object containing the following properties:
*
* * `open` - will open the Popover
* * `close` - will close the Popover
* * `toggle` - will toggle the Popover
* * `delegateKeyDown` - the underlaying DropdownLayout's keydown handler. It can be called
* inside another keyDown event in order to delegate it.
* * `selectedOption` - the currently selected option
*
* Refer to the component documentation for more information.
*/
children: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.func]),
/**
* Array of objects. Objects must have an `id` and can can include value and node. If value is
* '-', a divider will be rendered instead (dividers do not require and id).
*/
options: _propTypes2.default.arrayOf(_propTypes2.default.oneOfType([_propTypes2.default.shape({
id: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]).isRequired,
value: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.string, _propTypes2.default.func]).isRequired,
disabled: _propTypes2.default.bool,
overrideStyle: _propTypes2.default.bool
}),
// A divider option without an id
_propTypes2.default.shape({
value: _propTypes2.default.oneOf(['-'])
})])),
/** The `id` of the selected option in the list */
selectedId: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
/**
* The `id` of the **initial** selected option in the list, will be used when the selection
* behaviour is being controlled
*/
initialSelectedId: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number])
}, _class.defaultProps = {
placement: 'bottom',
showArrow: false
}, _temp2);
exports.default = DropdownBase;