Handle error when switching mode
This commit is contained in:
parent
873d5f8ae2
commit
1bcc6382aa
|
@ -33,11 +33,10 @@ export default class TextMode extends Component {
|
|||
h('div', {class: 'jsoneditor-vertical-menu-separator'}),
|
||||
|
||||
this.props.options.modes && h(ModeButton, {
|
||||
open: this.state.modeMenuOpen,
|
||||
modes: this.props.options.modes,
|
||||
mode: this.props.mode,
|
||||
onMode: this.props.onMode,
|
||||
onClick: this.handleShowModeMenu
|
||||
onError: this.handleError
|
||||
})
|
||||
]),
|
||||
|
||||
|
@ -92,7 +91,7 @@ export default class TextMode extends Component {
|
|||
}
|
||||
|
||||
/** @private */
|
||||
handleError (err) {
|
||||
handleError = (err) => {
|
||||
if (this.props.options && this.props.options.onError) {
|
||||
this.props.options.onError(err)
|
||||
}
|
||||
|
|
|
@ -101,7 +101,8 @@ export default class TreeMode extends Component {
|
|||
this.props.options.modes && h(ModeButton, {
|
||||
modes: this.props.options.modes,
|
||||
mode: this.props.mode,
|
||||
onMode: this.props.onMode
|
||||
onMode: this.props.onMode,
|
||||
onError: this.handleError
|
||||
})
|
||||
])
|
||||
}
|
||||
|
@ -199,6 +200,16 @@ export default class TreeMode extends Component {
|
|||
this.emitOnChange (actions, revert)
|
||||
}
|
||||
|
||||
/** @private */
|
||||
handleError = (err) => {
|
||||
if (this.props.options && this.props.options.onError) {
|
||||
this.props.options.onError(err)
|
||||
}
|
||||
else {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit an onChange event when there is a listener for it.
|
||||
* @param {JSONPatch} patch
|
||||
|
|
|
@ -12,7 +12,7 @@ export default class ModeButton extends Component {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param {{modes: string[], mode: string, onMode: function}} props
|
||||
* @param {{modes: string[], mode: string, onMode: function, onError: function}} props
|
||||
* @param state
|
||||
* @return {*}
|
||||
*/
|
||||
|
|
|
@ -3,7 +3,7 @@ import { toCapital } from '../utils/stringUtils'
|
|||
|
||||
export default class ModeMenu extends Component {
|
||||
/**
|
||||
* @param {{open, modes, mode, onMode}} props
|
||||
* @param {{open, modes, mode, onMode, onError}} props
|
||||
* @param {Obect} state
|
||||
* @return {JSX.Element}
|
||||
*/
|
||||
|
@ -15,9 +15,14 @@ export default class ModeMenu extends Component {
|
|||
class: 'jsoneditor-menu-button jsoneditor-type-modes' +
|
||||
((mode === props.mode) ? ' jsoneditor-selected' : ''),
|
||||
onClick: () => {
|
||||
try {
|
||||
props.onMode(mode)
|
||||
this.setState({ open: false })
|
||||
}
|
||||
catch (err) {
|
||||
props.onError(err)
|
||||
}
|
||||
}
|
||||
}, toCapital(mode))
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in New Issue