GoScrobble/web/node_modules/reactstrap/lib/Dropdown.js

302 lines
11 KiB
JavaScript
Raw Normal View History

2022-04-25 02:47:15 +00:00
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactPopper = require("react-popper");
var _classnames = _interopRequireDefault(require("classnames"));
var _DropdownContext = require("./DropdownContext");
var _utils = require("./utils");
/* eslint react/no-find-dom-node: 0 */
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-find-dom-node.md
var propTypes = {
a11y: _propTypes.default.bool,
disabled: _propTypes.default.bool,
direction: _propTypes.default.oneOf(['up', 'down', 'left', 'right']),
group: _propTypes.default.bool,
isOpen: _propTypes.default.bool,
nav: _propTypes.default.bool,
active: _propTypes.default.bool,
addonType: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.oneOf(['prepend', 'append'])]),
size: _propTypes.default.string,
tag: _utils.tagPropType,
toggle: _propTypes.default.func,
children: _propTypes.default.node,
className: _propTypes.default.string,
cssModule: _propTypes.default.object,
inNavbar: _propTypes.default.bool,
setActiveFromChild: _propTypes.default.bool
};
var defaultProps = {
a11y: true,
isOpen: false,
direction: 'down',
nav: false,
active: false,
addonType: false,
inNavbar: false,
setActiveFromChild: false
};
var preventDefaultKeys = [_utils.keyCodes.space, _utils.keyCodes.enter, _utils.keyCodes.up, _utils.keyCodes.down, _utils.keyCodes.end, _utils.keyCodes.home];
var Dropdown = /*#__PURE__*/function (_React$Component) {
(0, _inheritsLoose2.default)(Dropdown, _React$Component);
function Dropdown(props) {
var _this;
_this = _React$Component.call(this, props) || this;
_this.addEvents = _this.addEvents.bind((0, _assertThisInitialized2.default)(_this));
_this.handleDocumentClick = _this.handleDocumentClick.bind((0, _assertThisInitialized2.default)(_this));
_this.handleKeyDown = _this.handleKeyDown.bind((0, _assertThisInitialized2.default)(_this));
_this.removeEvents = _this.removeEvents.bind((0, _assertThisInitialized2.default)(_this));
_this.toggle = _this.toggle.bind((0, _assertThisInitialized2.default)(_this));
_this.handleMenuRef = _this.handleMenuRef.bind((0, _assertThisInitialized2.default)(_this));
_this.containerRef = /*#__PURE__*/_react.default.createRef();
_this.menuRef = /*#__PURE__*/_react.default.createRef();
return _this;
}
var _proto = Dropdown.prototype;
_proto.handleMenuRef = function handleMenuRef(menuRef) {
this.menuRef.current = menuRef;
};
_proto.getContextValue = function getContextValue() {
return {
toggle: this.toggle,
isOpen: this.props.isOpen,
direction: this.props.direction === 'down' && this.props.dropup ? 'up' : this.props.direction,
inNavbar: this.props.inNavbar,
disabled: this.props.disabled,
// Callback that should be called by DropdownMenu to provide a ref to
// a HTML tag that's used for the DropdownMenu
onMenuRef: this.handleMenuRef
};
};
_proto.componentDidMount = function componentDidMount() {
this.handleProps();
};
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
if (this.props.isOpen !== prevProps.isOpen) {
this.handleProps();
}
};
_proto.componentWillUnmount = function componentWillUnmount() {
this.removeEvents();
};
_proto.getContainer = function getContainer() {
return this.containerRef.current;
};
_proto.getMenu = function getMenu() {
return this.menuRef.current;
};
_proto.getMenuCtrl = function getMenuCtrl() {
if (this._$menuCtrl) return this._$menuCtrl;
this._$menuCtrl = this.getContainer().querySelector('[aria-expanded]');
return this._$menuCtrl;
};
_proto.getMenuItems = function getMenuItems() {
// In a real menu with a child DropdownMenu, `this.getMenu()` should never
// be null, but it is sometimes null in tests. To mitigate that, we just
// use `this.getContainer()` as the fallback `menuContainer`.
var menuContainer = this.getMenu() || this.getContainer();
return [].slice.call(menuContainer.querySelectorAll('[role="menuitem"]'));
};
_proto.addEvents = function addEvents() {
var _this2 = this;
['click', 'touchstart', 'keyup'].forEach(function (event) {
return document.addEventListener(event, _this2.handleDocumentClick, true);
});
};
_proto.removeEvents = function removeEvents() {
var _this3 = this;
['click', 'touchstart', 'keyup'].forEach(function (event) {
return document.removeEventListener(event, _this3.handleDocumentClick, true);
});
};
_proto.handleDocumentClick = function handleDocumentClick(e) {
if (e && (e.which === 3 || e.type === 'keyup' && e.which !== _utils.keyCodes.tab)) return;
var container = this.getContainer();
var menu = this.getMenu();
var clickIsInContainer = container.contains(e.target) && container !== e.target;
var clickIsInMenu = menu && menu.contains(e.target) && menu !== e.target;
if ((clickIsInContainer || clickIsInMenu) && (e.type !== 'keyup' || e.which === _utils.keyCodes.tab)) {
return;
}
this.toggle(e);
};
_proto.handleKeyDown = function handleKeyDown(e) {
var _this4 = this;
var isTargetMenuItem = e.target.getAttribute('role') === 'menuitem';
var isTargetMenuCtrl = this.getMenuCtrl() === e.target;
var isTab = _utils.keyCodes.tab === e.which;
if (/input|textarea/i.test(e.target.tagName) || isTab && !this.props.a11y || isTab && !(isTargetMenuItem || isTargetMenuCtrl)) {
return;
}
if (preventDefaultKeys.indexOf(e.which) !== -1 || e.which >= 48 && e.which <= 90) {
e.preventDefault();
}
if (this.props.disabled) return;
if (isTargetMenuCtrl) {
if ([_utils.keyCodes.space, _utils.keyCodes.enter, _utils.keyCodes.up, _utils.keyCodes.down].indexOf(e.which) > -1) {
// Open the menu (if not open) and focus the first menu item
if (!this.props.isOpen) {
this.toggle(e);
}
setTimeout(function () {
return _this4.getMenuItems()[0].focus();
});
} else if (this.props.isOpen && isTab) {
// Focus the first menu item if tabbing from an open menu. We need this
// for cases where the DropdownMenu sets a custom container, which may
// not be the natural next item to tab to from the DropdownToggle.
e.preventDefault();
this.getMenuItems()[0].focus();
} else if (this.props.isOpen && e.which === _utils.keyCodes.esc) {
this.toggle(e);
}
}
if (this.props.isOpen && e.target.getAttribute('role') === 'menuitem') {
if ([_utils.keyCodes.tab, _utils.keyCodes.esc].indexOf(e.which) > -1) {
this.toggle(e);
this.getMenuCtrl().focus();
} else if ([_utils.keyCodes.space, _utils.keyCodes.enter].indexOf(e.which) > -1) {
e.target.click();
this.getMenuCtrl().focus();
} else if ([_utils.keyCodes.down, _utils.keyCodes.up].indexOf(e.which) > -1 || [_utils.keyCodes.n, _utils.keyCodes.p].indexOf(e.which) > -1 && e.ctrlKey) {
var $menuitems = this.getMenuItems();
var index = $menuitems.indexOf(e.target);
if (_utils.keyCodes.up === e.which || _utils.keyCodes.p === e.which && e.ctrlKey) {
index = index !== 0 ? index - 1 : $menuitems.length - 1;
} else if (_utils.keyCodes.down === e.which || _utils.keyCodes.n === e.which && e.ctrlKey) {
index = index === $menuitems.length - 1 ? 0 : index + 1;
}
$menuitems[index].focus();
} else if (_utils.keyCodes.end === e.which) {
var _$menuitems = this.getMenuItems();
_$menuitems[_$menuitems.length - 1].focus();
} else if (_utils.keyCodes.home === e.which) {
var _$menuitems2 = this.getMenuItems();
_$menuitems2[0].focus();
} else if (e.which >= 48 && e.which <= 90) {
var _$menuitems3 = this.getMenuItems();
var charPressed = String.fromCharCode(e.which).toLowerCase();
for (var i = 0; i < _$menuitems3.length; i += 1) {
var firstLetter = _$menuitems3[i].textContent && _$menuitems3[i].textContent[0].toLowerCase();
if (firstLetter === charPressed) {
_$menuitems3[i].focus();
break;
}
}
}
}
};
_proto.handleProps = function handleProps() {
if (this.props.isOpen) {
this.addEvents();
} else {
this.removeEvents();
}
};
_proto.toggle = function toggle(e) {
if (this.props.disabled) {
return e && e.preventDefault();
}
return this.props.toggle(e);
};
_proto.render = function render() {
var _classNames, _ref;
var _omit = (0, _utils.omit)(this.props, ['toggle', 'disabled', 'inNavbar', 'a11y']),
className = _omit.className,
cssModule = _omit.cssModule,
direction = _omit.direction,
isOpen = _omit.isOpen,
group = _omit.group,
size = _omit.size,
nav = _omit.nav,
setActiveFromChild = _omit.setActiveFromChild,
active = _omit.active,
addonType = _omit.addonType,
tag = _omit.tag,
attrs = (0, _objectWithoutPropertiesLoose2.default)(_omit, ["className", "cssModule", "direction", "isOpen", "group", "size", "nav", "setActiveFromChild", "active", "addonType", "tag"]);
var Tag = tag || (nav ? 'li' : 'div');
var subItemIsActive = false;
if (setActiveFromChild) {
_react.default.Children.map(this.props.children[1].props.children, function (dropdownItem) {
if (dropdownItem && dropdownItem.props.active) subItemIsActive = true;
});
}
var classes = (0, _utils.mapToCssModules)((0, _classnames.default)(className, direction !== 'down' && "drop" + direction, nav && active ? 'active' : false, setActiveFromChild && subItemIsActive ? 'active' : false, (_classNames = {}, _classNames["input-group-" + addonType] = addonType, _classNames['btn-group'] = group, _classNames["btn-group-" + size] = !!size, _classNames.dropdown = !group && !addonType, _classNames.show = isOpen, _classNames['nav-item'] = nav, _classNames)), cssModule);
return /*#__PURE__*/_react.default.createElement(_DropdownContext.DropdownContext.Provider, {
value: this.getContextValue()
}, /*#__PURE__*/_react.default.createElement(_reactPopper.Manager, null, /*#__PURE__*/_react.default.createElement(Tag, (0, _extends2.default)({}, attrs, (_ref = {}, _ref[typeof Tag === 'string' ? 'ref' : 'innerRef'] = this.containerRef, _ref), {
onKeyDown: this.handleKeyDown,
className: classes
}))));
};
return Dropdown;
}(_react.default.Component);
Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;
var _default = Dropdown;
exports.default = _default;