Fixed ModeMenu broken after using it when bundled with `preact-compat`

This commit is contained in:
jos 2017-01-08 15:34:01 +01:00
parent 675432c52d
commit 6f1fbac9ac
1 changed files with 19 additions and 14 deletions

View File

@ -9,24 +9,14 @@ export default class ModeMenu extends Component {
* @return {JSX.Element} * @return {JSX.Element}
*/ */
render () { render () {
const { props, state} = this if (this.props.open) {
const items = this.props.modes.map(mode => {
if (props.open) {
const items = props.modes.map(mode => {
return h('button', { return h('button', {
key: mode, key: mode,
title: `Switch to ${mode} mode`, title: `Switch to ${mode} mode`,
className: 'jsoneditor-menu-button jsoneditor-type-modes' + className: 'jsoneditor-menu-button jsoneditor-type-modes' +
((mode === props.mode) ? ' jsoneditor-selected' : ''), ((mode === this.props.mode) ? ' jsoneditor-selected' : ''),
onClick: () => { onClick: this.handleClick
try {
props.onRequestClose()
props.onChangeMode(mode)
}
catch (err) {
props.onError(err)
}
}
}, toCapital(mode)) }, toCapital(mode))
}) })
@ -39,6 +29,21 @@ export default class ModeMenu extends Component {
} }
} }
handleClick = () => {
// we trigger the onChangeMode on the next tick, after the click event
// has been finished. This is a workaround for preact not neatly replacing
// a rendered app whilst the event is still being handled.
setTimeout(() => {
try {
this.props.onRequestClose()
this.props.onChangeMode(mode)
}
catch (err) {
this.props.onError(err)
}
})
}
componentDidMount () { componentDidMount () {
this.updateRequestCloseListener() this.updateRequestCloseListener()
} }