|
| 1 | +// Adapted from https://github.com/mlaursen/react-dd-menu/blob/master/src/js/DropdownMenu.js |
| 2 | + |
| 3 | +/* eslint react/no-find-dom-node: 0 */ |
| 4 | + |
| 5 | +import React, { PureComponent, PropTypes } from "react" |
| 6 | +import ReactDOM from "react-dom" |
| 7 | +import CSSTransitionGroup from "react-transition-group/CSSTransitionGroup" |
| 8 | +import classnames from "classnames" |
| 9 | + |
| 10 | +const TAB = 9 |
| 11 | +const SPACEBAR = 32 |
| 12 | +const ALIGNMENTS = ["center", "right", "left"] |
| 13 | +const MENU_SIZES = ["sm", "md", "lg", "xl"] |
| 14 | + |
| 15 | + |
| 16 | +export default class DropdownMenu extends PureComponent { |
| 17 | + static propTypes = { |
| 18 | + isOpen: PropTypes.bool.isRequired, |
| 19 | + close: PropTypes.func.isRequired, |
| 20 | + toggle: PropTypes.node.isRequired, |
| 21 | + children: PropTypes.node, |
| 22 | + inverse: PropTypes.bool, |
| 23 | + align: PropTypes.oneOf(ALIGNMENTS), |
| 24 | + animAlign: PropTypes.oneOf(ALIGNMENTS), |
| 25 | + textAlign: PropTypes.oneOf(ALIGNMENTS), |
| 26 | + menuAlign: PropTypes.oneOf(ALIGNMENTS), |
| 27 | + className: PropTypes.string, |
| 28 | + size: PropTypes.oneOf(MENU_SIZES), |
| 29 | + upwards: PropTypes.bool, |
| 30 | + animate: PropTypes.bool, |
| 31 | + enterTimeout: PropTypes.number, |
| 32 | + leaveTimeout: PropTypes.number, |
| 33 | + closeOnInsideClick: PropTypes.bool, |
| 34 | + closeOnOutsideClick: PropTypes.bool, |
| 35 | + }; |
| 36 | + |
| 37 | + static defaultProps = { |
| 38 | + inverse: false, |
| 39 | + align: "center", |
| 40 | + animAlign: null, |
| 41 | + textAlign: null, |
| 42 | + menuAlign: null, |
| 43 | + className: null, |
| 44 | + size: null, |
| 45 | + upwards: false, |
| 46 | + animate: true, |
| 47 | + enterTimeout: 150, |
| 48 | + leaveTimeout: 150, |
| 49 | + closeOnInsideClick: true, |
| 50 | + closeOnOutsideClick: true, |
| 51 | + }; |
| 52 | + |
| 53 | + static MENU_SIZES = MENU_SIZES; |
| 54 | + static ALIGNMENTS = ALIGNMENTS; |
| 55 | + |
| 56 | + componentDidUpdate(prevProps) { |
| 57 | + if(this.props.isOpen === prevProps.isOpen) { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + const menuItems = ReactDOM.findDOMNode(this).querySelector(".dd-menu > .dd-menu-items") |
| 62 | + if(this.props.isOpen && !prevProps.isOpen) { |
| 63 | + this.lastWindowClickEvent = this.handleClickOutside |
| 64 | + document.addEventListener("click", this.lastWindowClickEvent) |
| 65 | + if(this.props.closeOnInsideClick) { |
| 66 | + menuItems.addEventListener("click", this.props.close) |
| 67 | + } |
| 68 | + menuItems.addEventListener("onkeydown", this.close) |
| 69 | + } else if(!this.props.isOpen && prevProps.isOpen) { |
| 70 | + document.removeEventListener("click", this.lastWindowClickEvent) |
| 71 | + if(prevProps.closeOnInsideClick) { |
| 72 | + menuItems.removeEventListener("click", this.props.close) |
| 73 | + } |
| 74 | + menuItems.removeEventListener("onkeydown", this.close) |
| 75 | + |
| 76 | + this.lastWindowClickEvent = null |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + componentWillUnmount() { |
| 81 | + if(this.lastWindowClickEvent) { |
| 82 | + document.removeEventListener("click", this.lastWindowClickEvent) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + close = (e) => { |
| 87 | + const key = e.which || e.keyCode |
| 88 | + if(key === SPACEBAR) { |
| 89 | + this.props.close() |
| 90 | + e.preventDefault() |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + handleClickOutside = (e) => { |
| 95 | + if(!this.props.closeOnOutsideClick) { |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + const node = ReactDOM.findDOMNode(this) |
| 100 | + let target = e.target |
| 101 | + |
| 102 | + while(target.parentNode) { |
| 103 | + if(target === node) { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + target = target.parentNode |
| 108 | + } |
| 109 | + |
| 110 | + this.props.close(e) |
| 111 | + }; |
| 112 | + |
| 113 | + handleKeyDown = (e) => { |
| 114 | + const key = e.which || e.keyCode |
| 115 | + if(key !== TAB) { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + const items = ReactDOM.findDOMNode(this).querySelectorAll("button,a") |
| 120 | + const id = e.shiftKey ? 1 : items.length - 1 |
| 121 | + |
| 122 | + if(e.target === items[id]) { |
| 123 | + this.props.close(e) |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + |
| 128 | + render() { |
| 129 | + const { menuAlign, align, inverse, size, className } = this.props |
| 130 | + |
| 131 | + const menuClassName = classnames( |
| 132 | + "dd-menu", |
| 133 | + `dd-menu-${menuAlign || align}`, |
| 134 | + { "dd-menu-inverse": inverse }, |
| 135 | + className, |
| 136 | + size ? ("dd-menu-" + size) : null |
| 137 | + ) |
| 138 | + |
| 139 | + const { textAlign, upwards, animAlign, animate, enterTimeout, leaveTimeout } = this.props |
| 140 | + |
| 141 | + const listClassName = "dd-items-" + (textAlign || align) |
| 142 | + const transitionProps = { |
| 143 | + transitionName: "grow-from-" + (upwards ? "up-" : "") + (animAlign || align), |
| 144 | + component: "div", |
| 145 | + className: classnames("dd-menu-items", { "dd-items-upwards": upwards }), |
| 146 | + onKeyDown: this.handleKeyDown, |
| 147 | + transitionEnter: animate, |
| 148 | + transitionLeave: animate, |
| 149 | + transitionEnterTimeout: enterTimeout, |
| 150 | + transitionLeaveTimeout: leaveTimeout, |
| 151 | + } |
| 152 | + |
| 153 | + return ( |
| 154 | + <div className={menuClassName}> |
| 155 | + {this.props.toggle} |
| 156 | + <CSSTransitionGroup {...transitionProps}> |
| 157 | + {this.props.isOpen && |
| 158 | + <ul key="items" className={listClassName}>{this.props.children}</ul> |
| 159 | + } |
| 160 | + </CSSTransitionGroup> |
| 161 | + </div> |
| 162 | + ) |
| 163 | + } |
| 164 | +} |
0 commit comments