1 | function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
2 | var _excluded = ["className", "cssModule", "direction", "isOpen", "group", "size", "nav", "setActiveFromChild", "active", "tag", "menuRole"];
|
3 | function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); }
|
4 | 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; }
|
5 | function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
6 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
7 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
8 | 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); } }
|
9 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
10 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
11 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
12 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
13 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
14 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
15 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
16 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
17 |
|
18 |
|
19 |
|
20 | import React from 'react';
|
21 | import PropTypes from 'prop-types';
|
22 | import { Manager } from 'react-popper';
|
23 | import classNames from 'classnames';
|
24 | import { DropdownContext } from './DropdownContext';
|
25 | import { mapToCssModules, omit, keyCodes, tagPropType } from './utils';
|
26 | import { InputGroupContext } from './InputGroupContext';
|
27 | var propTypes = {
|
28 | a11y: PropTypes.bool,
|
29 | disabled: PropTypes.bool,
|
30 | direction: PropTypes.oneOf(['up', 'down', 'start', 'end', 'left', 'right']),
|
31 | group: PropTypes.bool,
|
32 | isOpen: PropTypes.bool,
|
33 | nav: PropTypes.bool,
|
34 | active: PropTypes.bool,
|
35 | size: PropTypes.string,
|
36 | tag: tagPropType,
|
37 | toggle: PropTypes.func,
|
38 | children: PropTypes.node,
|
39 | className: PropTypes.string,
|
40 | cssModule: PropTypes.object,
|
41 | dropup: PropTypes.bool,
|
42 | inNavbar: PropTypes.bool,
|
43 | setActiveFromChild: PropTypes.bool,
|
44 | menuRole: PropTypes.oneOf(['listbox', 'menu'])
|
45 | };
|
46 | var defaultProps = {
|
47 | a11y: true,
|
48 | isOpen: false,
|
49 | direction: 'down',
|
50 | nav: false,
|
51 | active: false,
|
52 | inNavbar: false,
|
53 | setActiveFromChild: false
|
54 | };
|
55 | var preventDefaultKeys = [keyCodes.space, keyCodes.enter, keyCodes.up, keyCodes.down, keyCodes.end, keyCodes.home];
|
56 | var Dropdown = function (_React$Component) {
|
57 | _inherits(Dropdown, _React$Component);
|
58 | var _super = _createSuper(Dropdown);
|
59 | function Dropdown(props) {
|
60 | var _this;
|
61 | _classCallCheck(this, Dropdown);
|
62 | _this = _super.call(this, props);
|
63 | _this.addEvents = _this.addEvents.bind(_assertThisInitialized(_this));
|
64 | _this.handleDocumentClick = _this.handleDocumentClick.bind(_assertThisInitialized(_this));
|
65 | _this.handleKeyDown = _this.handleKeyDown.bind(_assertThisInitialized(_this));
|
66 | _this.removeEvents = _this.removeEvents.bind(_assertThisInitialized(_this));
|
67 | _this.toggle = _this.toggle.bind(_assertThisInitialized(_this));
|
68 | _this.handleMenuRef = _this.handleMenuRef.bind(_assertThisInitialized(_this));
|
69 | _this.handleToggleRef = _this.handleToggleRef.bind(_assertThisInitialized(_this));
|
70 | _this.containerRef = React.createRef();
|
71 | _this.menuRef = React.createRef();
|
72 | _this.toggleRef = React.createRef();
|
73 |
|
74 | return _this;
|
75 | }
|
76 | _createClass(Dropdown, [{
|
77 | key: "componentDidMount",
|
78 | value: function componentDidMount() {
|
79 | this.handleProps();
|
80 | }
|
81 | }, {
|
82 | key: "componentDidUpdate",
|
83 | value: function componentDidUpdate(prevProps) {
|
84 | if (this.props.isOpen !== prevProps.isOpen) {
|
85 | this.handleProps();
|
86 | }
|
87 | }
|
88 | }, {
|
89 | key: "componentWillUnmount",
|
90 | value: function componentWillUnmount() {
|
91 | this.removeEvents();
|
92 | }
|
93 | }, {
|
94 | key: "handleMenuRef",
|
95 | value: function handleMenuRef(menuRef) {
|
96 | this.menuRef.current = menuRef;
|
97 | }
|
98 | }, {
|
99 | key: "handleToggleRef",
|
100 | value: function handleToggleRef(toggleRef) {
|
101 | this.toggleRef.current = toggleRef;
|
102 | }
|
103 | }, {
|
104 | key: "handleDocumentClick",
|
105 | value: function handleDocumentClick(e) {
|
106 | if (e && (e.which === 3 || e.type === 'keyup' && e.which !== keyCodes.tab)) return;
|
107 | var container = this.getContainer();
|
108 | var menu = this.getMenu();
|
109 | var toggle = this.getToggle();
|
110 |
|
111 |
|
112 |
|
113 | if (!toggle) {
|
114 | return;
|
115 | }
|
116 | var targetIsToggle = toggle.contains(e.target);
|
117 | var clickIsInMenu = menu && menu.contains(e.target) && menu !== e.target;
|
118 | var clickIsInInput = false;
|
119 | if (container) {
|
120 |
|
121 | clickIsInInput = container.classList.contains('input-group') && container.classList.contains('dropdown') && e.target.tagName === 'INPUT';
|
122 | }
|
123 | if ((targetIsToggle && !clickIsInInput || clickIsInMenu) && (e.type !== 'keyup' || e.which === keyCodes.tab)) {
|
124 | return;
|
125 | }
|
126 | this.toggle(e);
|
127 | }
|
128 | }, {
|
129 | key: "handleKeyDown",
|
130 | value: function handleKeyDown(e) {
|
131 | var _this2 = this;
|
132 | var isTargetMenuItem = e.target.getAttribute('role') === 'menuitem' || e.target.getAttribute('role') === 'option';
|
133 | var isTargetMenuCtrl = this.getMenuCtrl() === e.target;
|
134 | var isTab = keyCodes.tab === e.which;
|
135 | if (/input|textarea/i.test(e.target.tagName) || isTab && !this.props.a11y || isTab && !(isTargetMenuItem || isTargetMenuCtrl)) {
|
136 | return;
|
137 | }
|
138 | if (preventDefaultKeys.indexOf(e.which) !== -1 || e.which >= 48 && e.which <= 90) {
|
139 | e.preventDefault();
|
140 | }
|
141 | if (this.props.disabled) return;
|
142 | if (isTargetMenuCtrl) {
|
143 | if ([keyCodes.space, keyCodes.enter, keyCodes.up, keyCodes.down].indexOf(e.which) > -1) {
|
144 |
|
145 | if (!this.props.isOpen) {
|
146 | this.toggle(e);
|
147 | }
|
148 | setTimeout(function () {
|
149 | var _this2$getMenuItems$;
|
150 | return (_this2$getMenuItems$ = _this2.getMenuItems()[0]) === null || _this2$getMenuItems$ === void 0 ? void 0 : _this2$getMenuItems$.focus();
|
151 | });
|
152 | } else if (this.props.isOpen && isTab) {
|
153 | var _this$getMenuItems$;
|
154 |
|
155 |
|
156 |
|
157 | e.preventDefault();
|
158 | (_this$getMenuItems$ = this.getMenuItems()[0]) === null || _this$getMenuItems$ === void 0 ? void 0 : _this$getMenuItems$.focus();
|
159 | } else if (this.props.isOpen && e.which === keyCodes.esc) {
|
160 | this.toggle(e);
|
161 | }
|
162 | }
|
163 | if (this.props.isOpen && isTargetMenuItem) {
|
164 | if ([keyCodes.tab, keyCodes.esc].indexOf(e.which) > -1) {
|
165 | this.toggle(e);
|
166 | this.getMenuCtrl().focus();
|
167 | } else if ([keyCodes.space, keyCodes.enter].indexOf(e.which) > -1) {
|
168 | e.target.click();
|
169 | this.getMenuCtrl().focus();
|
170 | } else if ([keyCodes.down, keyCodes.up].indexOf(e.which) > -1 || [keyCodes.n, keyCodes.p].indexOf(e.which) > -1 && e.ctrlKey) {
|
171 | var $menuitems = this.getMenuItems();
|
172 | var index = $menuitems.indexOf(e.target);
|
173 | if (keyCodes.up === e.which || keyCodes.p === e.which && e.ctrlKey) {
|
174 | index = index !== 0 ? index - 1 : $menuitems.length - 1;
|
175 | } else if (keyCodes.down === e.which || keyCodes.n === e.which && e.ctrlKey) {
|
176 | index = index === $menuitems.length - 1 ? 0 : index + 1;
|
177 | }
|
178 | $menuitems[index].focus();
|
179 | } else if (keyCodes.end === e.which) {
|
180 | var _$menuitems = this.getMenuItems();
|
181 | _$menuitems[_$menuitems.length - 1].focus();
|
182 | } else if (keyCodes.home === e.which) {
|
183 | var _$menuitems2 = this.getMenuItems();
|
184 | _$menuitems2[0].focus();
|
185 | } else if (e.which >= 48 && e.which <= 90) {
|
186 | var _$menuitems3 = this.getMenuItems();
|
187 | var charPressed = String.fromCharCode(e.which).toLowerCase();
|
188 | for (var i = 0; i < _$menuitems3.length; i += 1) {
|
189 | var firstLetter = _$menuitems3[i].textContent && _$menuitems3[i].textContent[0].toLowerCase();
|
190 | if (firstLetter === charPressed) {
|
191 | _$menuitems3[i].focus();
|
192 | break;
|
193 | }
|
194 | }
|
195 | }
|
196 | }
|
197 | }
|
198 | }, {
|
199 | key: "handleProps",
|
200 | value: function handleProps() {
|
201 | if (this.props.isOpen) {
|
202 | this.addEvents();
|
203 | } else {
|
204 | this.removeEvents();
|
205 | }
|
206 | }
|
207 | }, {
|
208 | key: "getContextValue",
|
209 | value: function getContextValue() {
|
210 | return {
|
211 | toggle: this.toggle,
|
212 | isOpen: this.props.isOpen,
|
213 | direction: this.props.direction === 'down' && this.props.dropup ? 'up' : this.props.direction,
|
214 | inNavbar: this.props.inNavbar,
|
215 | disabled: this.props.disabled,
|
216 |
|
217 |
|
218 | onMenuRef: this.handleMenuRef,
|
219 | onToggleRef: this.handleToggleRef,
|
220 | menuRole: this.props.menuRole
|
221 | };
|
222 | }
|
223 | }, {
|
224 | key: "getContainer",
|
225 | value: function getContainer() {
|
226 | return this.containerRef.current;
|
227 | }
|
228 | }, {
|
229 | key: "getMenu",
|
230 | value: function getMenu() {
|
231 | return this.menuRef.current;
|
232 | }
|
233 | }, {
|
234 | key: "getToggle",
|
235 | value: function getToggle() {
|
236 | return this.toggleRef.current;
|
237 | }
|
238 | }, {
|
239 | key: "getMenuCtrl",
|
240 | value: function getMenuCtrl() {
|
241 | if (this._$menuCtrl) return this._$menuCtrl;
|
242 | this._$menuCtrl = this.getToggle();
|
243 | return this._$menuCtrl;
|
244 | }
|
245 | }, {
|
246 | key: "getItemType",
|
247 | value: function getItemType() {
|
248 | if (this.props.menuRole === 'listbox') {
|
249 | return 'option';
|
250 | }
|
251 | return 'menuitem';
|
252 | }
|
253 | }, {
|
254 | key: "getMenuItems",
|
255 | value: function getMenuItems() {
|
256 |
|
257 |
|
258 |
|
259 | var menuContainer = this.getMenu() || this.getContainer();
|
260 | return [].slice.call(menuContainer.querySelectorAll("[role=\"".concat(this.getItemType(), "\"]")));
|
261 | }
|
262 | }, {
|
263 | key: "addEvents",
|
264 | value: function addEvents() {
|
265 | var _this3 = this;
|
266 | ['click', 'touchstart', 'keyup'].forEach(function (event) {
|
267 | return document.addEventListener(event, _this3.handleDocumentClick, true);
|
268 | });
|
269 | }
|
270 | }, {
|
271 | key: "removeEvents",
|
272 | value: function removeEvents() {
|
273 | var _this4 = this;
|
274 | ['click', 'touchstart', 'keyup'].forEach(function (event) {
|
275 | return document.removeEventListener(event, _this4.handleDocumentClick, true);
|
276 | });
|
277 | }
|
278 | }, {
|
279 | key: "toggle",
|
280 | value: function toggle(e) {
|
281 | if (this.props.disabled) {
|
282 | return e && e.preventDefault();
|
283 | }
|
284 | return this.props.toggle(e);
|
285 | }
|
286 | }, {
|
287 | key: "render",
|
288 | value: function render() {
|
289 | var _classNames,
|
290 | _this5 = this;
|
291 | var _omit = omit(this.props, ['toggle', 'disabled', 'inNavbar', 'a11y']),
|
292 | className = _omit.className,
|
293 | cssModule = _omit.cssModule,
|
294 | direction = _omit.direction,
|
295 | isOpen = _omit.isOpen,
|
296 | group = _omit.group,
|
297 | size = _omit.size,
|
298 | nav = _omit.nav,
|
299 | setActiveFromChild = _omit.setActiveFromChild,
|
300 | active = _omit.active,
|
301 | tag = _omit.tag,
|
302 | menuRole = _omit.menuRole,
|
303 | attrs = _objectWithoutProperties(_omit, _excluded);
|
304 | var Tag = tag || (nav ? 'li' : 'div');
|
305 | var subItemIsActive = false;
|
306 | if (setActiveFromChild) {
|
307 | React.Children.map(this.props.children[1].props.children, function (dropdownItem) {
|
308 | if (dropdownItem && dropdownItem.props.active) subItemIsActive = true;
|
309 | });
|
310 | }
|
311 | var classes = mapToCssModules(classNames(className, nav && active ? 'active' : false, setActiveFromChild && subItemIsActive ? 'active' : false, (_classNames = {
|
312 | 'btn-group': group
|
313 | }, _defineProperty(_classNames, "btn-group-".concat(size), !!size), _defineProperty(_classNames, "dropdown", !group), _defineProperty(_classNames, "dropup", direction === 'up'), _defineProperty(_classNames, "dropstart", direction === 'start' || direction === 'left'), _defineProperty(_classNames, "dropend", direction === 'end' || direction === 'right'), _defineProperty(_classNames, "show", isOpen), _defineProperty(_classNames, 'nav-item', nav), _classNames)), cssModule);
|
314 | if (this.context.insideInputGroup) {
|
315 | return React.createElement(DropdownContext.Provider, {
|
316 | value: this.getContextValue()
|
317 | }, React.createElement(Manager, null, React.Children.map(this.props.children, function (child) {
|
318 | return React.cloneElement(child, {
|
319 | onKeyDown: _this5.handleKeyDown
|
320 | });
|
321 | })));
|
322 | }
|
323 | return React.createElement(DropdownContext.Provider, {
|
324 | value: this.getContextValue()
|
325 | }, React.createElement(Manager, null, React.createElement(Tag, _extends({}, attrs, _defineProperty({}, typeof Tag === 'string' ? 'ref' : 'innerRef', this.containerRef), {
|
326 | onKeyDown: this.handleKeyDown,
|
327 | className: classes
|
328 | }))));
|
329 | }
|
330 | }]);
|
331 | return Dropdown;
|
332 | }(React.Component);
|
333 | Dropdown.propTypes = propTypes;
|
334 | Dropdown.defaultProps = defaultProps;
|
335 | Dropdown.contextType = InputGroupContext;
|
336 | export default Dropdown; |
\ | No newline at end of file |