Some refactoring
This commit is contained in:
parent
94b7be90d3
commit
f786940d8d
|
@ -39,7 +39,8 @@ export default class Menu extends Component {
|
|||
|
||||
return h('div', {
|
||||
className: className,
|
||||
'data-menu': 'true'
|
||||
'data-menu': 'true',
|
||||
ref: '',
|
||||
},
|
||||
this.props.items.map(this.renderMenuItem)
|
||||
)
|
||||
|
@ -175,8 +176,12 @@ export default class Menu extends Component {
|
|||
// Attach event listener on next tick, else the current click to open
|
||||
// the menu will immediately result in requestClose event as well
|
||||
setTimeout(() => {
|
||||
if (this.props.open && !this.handleRequestClose) {
|
||||
this.handleRequestClose = (event) => this.props.onRequestClose()
|
||||
if (!this.handleRequestClose) {
|
||||
this.handleRequestClose = (event) => {
|
||||
if (!findParentNode(event.target, 'data-menu', 'true')) {
|
||||
this.props.onRequestClose()
|
||||
}
|
||||
}
|
||||
window.addEventListener('click', this.handleRequestClose)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -46,7 +46,8 @@ export default class ModeMenu extends Component {
|
|||
}
|
||||
|
||||
componentWillUnmount () {
|
||||
this.removeRequestCloseListener()
|
||||
// remove on next tick, since a listener can be created on next tick too
|
||||
setTimeout(() => this.removeRequestCloseListener())
|
||||
}
|
||||
|
||||
updateRequestCloseListener () {
|
||||
|
@ -59,18 +60,18 @@ export default class ModeMenu extends Component {
|
|||
}
|
||||
|
||||
addRequestCloseListener () {
|
||||
if (!this.handleRequestClose) {
|
||||
// Attach event listener on next tick, else the current click to open
|
||||
// the menu will immediately result in requestClose event as well
|
||||
setTimeout(() => {
|
||||
if (!this.handleRequestClose) {
|
||||
this.handleRequestClose = (event) => {
|
||||
if (!findParentNode(event.target, 'data-menu', 'true')) {
|
||||
this.props.onRequestClose()
|
||||
}
|
||||
}
|
||||
window.addEventListener('click', this.handleRequestClose)
|
||||
}, 0)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
removeRequestCloseListener () {
|
||||
|
|
Loading…
Reference in New Issue