1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Toggle = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const React = tslib_1.__importStar(require("react"));
|
6 | const dropdown_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Dropdown/dropdown"));
|
7 | const dropdownConstants_1 = require("./dropdownConstants");
|
8 | const react_styles_1 = require("@patternfly/react-styles");
|
9 | const constants_1 = require("../../helpers/constants");
|
10 | const buttonVariantStyles = {
|
11 | default: '',
|
12 | primary: dropdown_1.default.modifiers.primary,
|
13 | secondary: dropdown_1.default.modifiers.secondary
|
14 | };
|
15 | class Toggle extends React.Component {
|
16 | constructor() {
|
17 | super(...arguments);
|
18 | this.buttonRef = React.createRef();
|
19 | this.componentDidMount = () => {
|
20 | document.addEventListener('click', this.onDocClick);
|
21 | document.addEventListener('touchstart', this.onDocClick);
|
22 | document.addEventListener('keydown', this.onEscPress);
|
23 | };
|
24 | this.componentWillUnmount = () => {
|
25 | document.removeEventListener('click', this.onDocClick);
|
26 | document.removeEventListener('touchstart', this.onDocClick);
|
27 | document.removeEventListener('keydown', this.onEscPress);
|
28 | };
|
29 | this.onDocClick = (event) => {
|
30 | const { isOpen, parentRef, onToggle, getMenuRef } = this.props;
|
31 | const menuRef = getMenuRef && getMenuRef();
|
32 | const clickedOnToggle = parentRef && parentRef.current && parentRef.current.contains(event.target);
|
33 | const clickedWithinMenu = menuRef && menuRef.contains && menuRef.contains(event.target);
|
34 | if (isOpen && !(clickedOnToggle || clickedWithinMenu)) {
|
35 | onToggle(false, event);
|
36 | }
|
37 | };
|
38 | this.onEscPress = (event) => {
|
39 | const { parentRef, getMenuRef } = this.props;
|
40 | const keyCode = event.keyCode || event.which;
|
41 | const menuRef = getMenuRef && getMenuRef();
|
42 | const escFromToggle = parentRef && parentRef.current && parentRef.current.contains(event.target);
|
43 | const escFromWithinMenu = menuRef && menuRef.contains && menuRef.contains(event.target);
|
44 | if (this.props.isOpen &&
|
45 | (keyCode === constants_1.KEY_CODES.ESCAPE_KEY || event.key === 'Tab') &&
|
46 | (escFromToggle || escFromWithinMenu)) {
|
47 | this.props.onToggle(false, event);
|
48 | this.buttonRef.current.focus();
|
49 | }
|
50 | };
|
51 | this.onKeyDown = (event) => {
|
52 | if (event.key === 'Tab' && !this.props.isOpen) {
|
53 | return;
|
54 | }
|
55 | if ((event.key === 'Tab' || event.key === 'Enter' || event.key === ' ') && this.props.isOpen) {
|
56 | if (!this.props.bubbleEvent) {
|
57 | event.stopPropagation();
|
58 | }
|
59 | event.preventDefault();
|
60 | this.props.onToggle(!this.props.isOpen, event);
|
61 | }
|
62 | else if ((event.key === 'Enter' || event.key === ' ') && !this.props.isOpen) {
|
63 | if (!this.props.bubbleEvent) {
|
64 | event.stopPropagation();
|
65 | }
|
66 | event.preventDefault();
|
67 | this.props.onToggle(!this.props.isOpen, event);
|
68 | this.props.onEnter();
|
69 | }
|
70 | };
|
71 | }
|
72 | render() {
|
73 | const _a = this.props, { className, children, isOpen, isDisabled, isPlain, isText, isPrimary, isSplitButton, toggleVariant, onToggle, 'aria-haspopup': ariaHasPopup,
|
74 |
|
75 | isActive, bubbleEvent, onEnter, parentRef, getMenuRef,
|
76 |
|
77 | id, type } = _a, props = tslib_1.__rest(_a, ["className", "children", "isOpen", "isDisabled", "isPlain", "isText", "isPrimary", "isSplitButton", "toggleVariant", "onToggle", 'aria-haspopup', "isActive", "bubbleEvent", "onEnter", "parentRef", "getMenuRef", "id", "type"]);
|
78 | return (React.createElement(dropdownConstants_1.DropdownContext.Consumer, null, ({ toggleClass }) => (React.createElement("button", Object.assign({}, props, { id: id, ref: this.buttonRef, className: react_styles_1.css(isSplitButton ? dropdown_1.default.dropdownToggleButton : toggleClass || dropdown_1.default.dropdownToggle, isActive && dropdown_1.default.modifiers.active, isPlain && dropdown_1.default.modifiers.plain, isText && dropdown_1.default.modifiers.text, isPrimary && dropdown_1.default.modifiers.primary, buttonVariantStyles[toggleVariant], className), type: type || 'button', onClick: event => onToggle(!isOpen, event), "aria-expanded": isOpen, "aria-haspopup": ariaHasPopup, onKeyDown: event => this.onKeyDown(event), disabled: isDisabled }), children))));
|
79 | }
|
80 | }
|
81 | exports.Toggle = Toggle;
|
82 | Toggle.displayName = 'Toggle';
|
83 | Toggle.defaultProps = {
|
84 | className: '',
|
85 | isOpen: false,
|
86 | isActive: false,
|
87 | isDisabled: false,
|
88 | isPlain: false,
|
89 | isText: false,
|
90 | isPrimary: false,
|
91 | isSplitButton: false,
|
92 | onToggle: () => { },
|
93 | onEnter: () => { },
|
94 | bubbleEvent: false
|
95 | };
|
96 |
|
\ | No newline at end of file |