mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-25 09:55:15 +00:00
285 lines
10 KiB
JavaScript
285 lines
10 KiB
JavaScript
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
||
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
||
|
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
|
||
|
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
|
||
|
|
||
|
/* eslint react/no-find-dom-node: 0 */
|
||
|
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-find-dom-node.md
|
||
|
import React from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import { Manager } from 'react-popper';
|
||
|
import classNames from 'classnames';
|
||
|
import { DropdownContext } from './DropdownContext';
|
||
|
import { mapToCssModules, omit, keyCodes, tagPropType } from './utils';
|
||
|
var propTypes = {
|
||
|
a11y: PropTypes.bool,
|
||
|
disabled: PropTypes.bool,
|
||
|
direction: PropTypes.oneOf(['up', 'down', 'left', 'right']),
|
||
|
group: PropTypes.bool,
|
||
|
isOpen: PropTypes.bool,
|
||
|
nav: PropTypes.bool,
|
||
|
active: PropTypes.bool,
|
||
|
addonType: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['prepend', 'append'])]),
|
||
|
size: PropTypes.string,
|
||
|
tag: tagPropType,
|
||
|
toggle: PropTypes.func,
|
||
|
children: PropTypes.node,
|
||
|
className: PropTypes.string,
|
||
|
cssModule: PropTypes.object,
|
||
|
inNavbar: PropTypes.bool,
|
||
|
setActiveFromChild: PropTypes.bool
|
||
|
};
|
||
|
var defaultProps = {
|
||
|
a11y: true,
|
||
|
isOpen: false,
|
||
|
direction: 'down',
|
||
|
nav: false,
|
||
|
active: false,
|
||
|
addonType: false,
|
||
|
inNavbar: false,
|
||
|
setActiveFromChild: false
|
||
|
};
|
||
|
var preventDefaultKeys = [keyCodes.space, keyCodes.enter, keyCodes.up, keyCodes.down, keyCodes.end, keyCodes.home];
|
||
|
|
||
|
var Dropdown = /*#__PURE__*/function (_React$Component) {
|
||
|
_inheritsLoose(Dropdown, _React$Component);
|
||
|
|
||
|
function Dropdown(props) {
|
||
|
var _this;
|
||
|
|
||
|
_this = _React$Component.call(this, props) || this;
|
||
|
_this.addEvents = _this.addEvents.bind(_assertThisInitialized(_this));
|
||
|
_this.handleDocumentClick = _this.handleDocumentClick.bind(_assertThisInitialized(_this));
|
||
|
_this.handleKeyDown = _this.handleKeyDown.bind(_assertThisInitialized(_this));
|
||
|
_this.removeEvents = _this.removeEvents.bind(_assertThisInitialized(_this));
|
||
|
_this.toggle = _this.toggle.bind(_assertThisInitialized(_this));
|
||
|
_this.handleMenuRef = _this.handleMenuRef.bind(_assertThisInitialized(_this));
|
||
|
_this.containerRef = /*#__PURE__*/React.createRef();
|
||
|
_this.menuRef = /*#__PURE__*/React.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 !== 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 === 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 = 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 ([keyCodes.space, keyCodes.enter, keyCodes.up, 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 === keyCodes.esc) {
|
||
|
this.toggle(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (this.props.isOpen && e.target.getAttribute('role') === 'menuitem') {
|
||
|
if ([keyCodes.tab, keyCodes.esc].indexOf(e.which) > -1) {
|
||
|
this.toggle(e);
|
||
|
this.getMenuCtrl().focus();
|
||
|
} else if ([keyCodes.space, keyCodes.enter].indexOf(e.which) > -1) {
|
||
|
e.target.click();
|
||
|
this.getMenuCtrl().focus();
|
||
|
} else if ([keyCodes.down, keyCodes.up].indexOf(e.which) > -1 || [keyCodes.n, keyCodes.p].indexOf(e.which) > -1 && e.ctrlKey) {
|
||
|
var $menuitems = this.getMenuItems();
|
||
|
var index = $menuitems.indexOf(e.target);
|
||
|
|
||
|
if (keyCodes.up === e.which || keyCodes.p === e.which && e.ctrlKey) {
|
||
|
index = index !== 0 ? index - 1 : $menuitems.length - 1;
|
||
|
} else if (keyCodes.down === e.which || keyCodes.n === e.which && e.ctrlKey) {
|
||
|
index = index === $menuitems.length - 1 ? 0 : index + 1;
|
||
|
}
|
||
|
|
||
|
$menuitems[index].focus();
|
||
|
} else if (keyCodes.end === e.which) {
|
||
|
var _$menuitems = this.getMenuItems();
|
||
|
|
||
|
_$menuitems[_$menuitems.length - 1].focus();
|
||
|
} else if (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 = 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 = _objectWithoutPropertiesLoose(_omit, ["className", "cssModule", "direction", "isOpen", "group", "size", "nav", "setActiveFromChild", "active", "addonType", "tag"]);
|
||
|
|
||
|
var Tag = tag || (nav ? 'li' : 'div');
|
||
|
var subItemIsActive = false;
|
||
|
|
||
|
if (setActiveFromChild) {
|
||
|
React.Children.map(this.props.children[1].props.children, function (dropdownItem) {
|
||
|
if (dropdownItem && dropdownItem.props.active) subItemIsActive = true;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
var classes = mapToCssModules(classNames(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.createElement(DropdownContext.Provider, {
|
||
|
value: this.getContextValue()
|
||
|
}, /*#__PURE__*/React.createElement(Manager, null, /*#__PURE__*/React.createElement(Tag, _extends({}, attrs, (_ref = {}, _ref[typeof Tag === 'string' ? 'ref' : 'innerRef'] = this.containerRef, _ref), {
|
||
|
onKeyDown: this.handleKeyDown,
|
||
|
className: classes
|
||
|
}))));
|
||
|
};
|
||
|
|
||
|
return Dropdown;
|
||
|
}(React.Component);
|
||
|
|
||
|
Dropdown.propTypes = propTypes;
|
||
|
Dropdown.defaultProps = defaultProps;
|
||
|
export default Dropdown;
|