|
1 | | -import React, { Component } from 'react'; |
2 | | -import ReactDOM from 'react-dom'; |
3 | | - |
4 | | -const withDropdown = WrappedComponent => ( |
5 | | - class extends Component { |
6 | | - constructor(props) { |
7 | | - super(props); |
8 | | - this.state = { open: false }; |
9 | | - this.toggle = this.toggle.bind(this); |
10 | | - this.manageEvents = this.manageEvents.bind(this); |
11 | | - this.handleDocumentClick = this.handleDocumentClick.bind(this); |
12 | | - } |
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | +import { ROLE_MENU_ITEM } from '../constants'; |
13 | 3 |
|
14 | | - componentWillUnmount() { |
15 | | - this.manageEvents(true); |
16 | | - } |
| 4 | +const events = ['click', 'touchstart', 'keyup']; |
| 5 | +const getMenuItems = (element) => [].slice.call(element.querySelectorAll(`[role="${ROLE_MENU_ITEM}"]`)); |
17 | 6 |
|
18 | | - componentDidMount() { |
19 | | - this.manageEvents(); |
20 | | - } |
| 7 | +const withDropdown = WrappedComponent => (props) => { |
| 8 | + const ref = useRef(null); |
| 9 | + const [ open, toggle ] = useState(false); |
21 | 10 |
|
22 | | - toggle( e ) { |
23 | | - const { open } = this.state; |
24 | | - this.setState({ open: !open }); |
| 11 | + const handleDocumentClick = (e) => { |
| 12 | + if (e && (e.which === 3 || (e.type === 'keyup' && e.which !== 9))) { |
| 13 | + return; |
25 | 14 | } |
26 | 15 |
|
27 | | - manageEvents(remove = false) { |
28 | | - var eventUpdater = remove ? document.removeEventListener : document.addEventListener; |
| 16 | + const container = ref.current; |
| 17 | + if (container.contains(e.target)) { |
| 18 | + if (e.target.getAttribute('role') === ROLE_MENU_ITEM) { |
| 19 | + toggle(open => open ? !open : open); |
| 20 | + } else { |
| 21 | + const menuItems = getMenuItems(container); |
| 22 | + menuItems.map((menuItem) => { |
| 23 | + if (menuItem.contains(e.target)) { |
| 24 | + toggle(open => open ? !open : open); |
| 25 | + } |
| 26 | + }); |
| 27 | + } |
| 28 | + } |
29 | 29 |
|
30 | | - ['click', 'touchstart', 'keyup'].forEach( event => |
31 | | - eventUpdater(event, this.handleDocumentClick, true) |
32 | | - ); |
| 30 | + if (container.contains(e.target) && container !== e.target |
| 31 | + && (e.type !== 'keyup' || e.which === 9) |
| 32 | + ) { |
| 33 | + return; |
33 | 34 | } |
34 | 35 |
|
35 | | - handleDocumentClick(e) { |
36 | | - if (e && (e.which === 3 || (e.type === 'keyup' && e.which !== 9))) { |
37 | | - return; |
38 | | - } |
| 36 | + toggle(open => open ? !open : open); |
| 37 | + }; |
39 | 38 |
|
40 | | - const container = ReactDOM.findDOMNode(this); |
41 | | - if (container.contains(e.target) && container !== e.target |
42 | | - && (e.type !== 'keyup' || e.which === 9) |
43 | | - ) { |
44 | | - return; |
45 | | - } |
| 39 | + const manageEvents = (remove = false) => { |
| 40 | + var eventUpdater = remove ? document.removeEventListener : document.addEventListener; |
| 41 | + events.forEach((event) => eventUpdater(event, handleDocumentClick, true)); |
| 42 | + }; |
46 | 43 |
|
47 | | - if (this.state.open) { |
48 | | - this.toggle(e); |
49 | | - } |
50 | | - } |
| 44 | + useEffect(() => { |
| 45 | + manageEvents(); |
| 46 | + return () => manageEvents(true); |
| 47 | + }, []); |
51 | 48 |
|
52 | | - render() { |
53 | | - return ( |
54 | | - <WrappedComponent ref={ this.ref } toggle={ this.toggle } open={ this.state.open } { ...this.props } /> |
55 | | - ); |
56 | | - } |
57 | | - } |
58 | | -); |
| 49 | + return ( |
| 50 | + <WrappedComponent ref={ ref } toggle={ toggle } open={ open } { ...props } /> |
| 51 | + ); |
| 52 | +}; |
59 | 53 |
|
60 | 54 | export default withDropdown; |
0 commit comments