Some refactoring

This commit is contained in:
jos 2017-07-13 12:20:48 +02:00
parent 94b7be90d3
commit f786940d8d
2 changed files with 16 additions and 10 deletions

View File

@ -39,7 +39,8 @@ export default class Menu extends Component {
return h('div', { return h('div', {
className: className, className: className,
'data-menu': 'true' 'data-menu': 'true',
ref: '',
}, },
this.props.items.map(this.renderMenuItem) 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 // Attach event listener on next tick, else the current click to open
// the menu will immediately result in requestClose event as well // the menu will immediately result in requestClose event as well
setTimeout(() => { setTimeout(() => {
if (this.props.open && !this.handleRequestClose) { if (!this.handleRequestClose) {
this.handleRequestClose = (event) => this.props.onRequestClose() this.handleRequestClose = (event) => {
if (!findParentNode(event.target, 'data-menu', 'true')) {
this.props.onRequestClose()
}
}
window.addEventListener('click', this.handleRequestClose) window.addEventListener('click', this.handleRequestClose)
} }
}) })

View File

@ -46,7 +46,8 @@ export default class ModeMenu extends Component {
} }
componentWillUnmount () { componentWillUnmount () {
this.removeRequestCloseListener() // remove on next tick, since a listener can be created on next tick too
setTimeout(() => this.removeRequestCloseListener())
} }
updateRequestCloseListener () { updateRequestCloseListener () {
@ -59,18 +60,18 @@ export default class ModeMenu extends Component {
} }
addRequestCloseListener () { addRequestCloseListener () {
if (!this.handleRequestClose) {
// Attach event listener on next tick, else the current click to open // Attach event listener on next tick, else the current click to open
// the menu will immediately result in requestClose event as well // the menu will immediately result in requestClose event as well
setTimeout(() => { setTimeout(() => {
if (!this.handleRequestClose) {
this.handleRequestClose = (event) => { this.handleRequestClose = (event) => {
if (!findParentNode(event.target, 'data-menu', 'true')) { if (!findParentNode(event.target, 'data-menu', 'true')) {
this.props.onRequestClose() this.props.onRequestClose()
} }
} }
window.addEventListener('click', this.handleRequestClose) window.addEventListener('click', this.handleRequestClose)
}, 0)
} }
})
} }
removeRequestCloseListener () { removeRequestCloseListener () {