Skip to content
Snippets Groups Projects
Commit 15a855aa authored by Loïc Correnson's avatar Loïc Correnson
Browse files

[dome] TS port for errors

parent a75b4445
No related branches found
No related tags found
No related merge requests found
...@@ -7,47 +7,53 @@ ...@@ -7,47 +7,53 @@
@module dome/errors @module dome/errors
*/ */
import React from 'react' ; import React from 'react';
import { Label } from 'dome/controls/labels' ; import { Label } from 'dome/controls/labels';
import { Button } from 'dome/controls/buttons' ; import { Button } from 'dome/controls/buttons';
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// --- Error Boundaries // --- Error Boundaries
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
@summary React Error Boundaries. Alternative renderer in case of error.
@property {string} [label] - Default error box label @param reload - callback for re-rendering the faulty component
@property {function} [onError] - Alternative renderer */
@description export interface renderError {
Install an error boundary. In case of error, the default (error: any, info: any, reload: () => void): JSX.Element;
rendering is a warning button that output on console the }
catched error.
export interface CatchProps {
/** Name of the error boundary. */
label?: string;
/** Alternative renderer callback in case of errors. */
onError?: JSX.Element | renderError;
}
An alternative rendering can be supplied interface CatchState {
with `onError:(error,info) => React.Element`. error?: any;
info?: any;
}
/**
React Error Boundaries.
*/ */
export class Catch extends React.Component export class Catch extends React.Component<CatchProps, CatchState, {}> {
{
constructor(props) { constructor(props: CatchProps) {
super(props); super(props);
this.state = { }; this.state = {};
this.logerr = this.logerr.bind(this); this.logerr = this.logerr.bind(this);
this.reload = this.reload.bind(this); this.reload = this.reload.bind(this);
} }
dumpError(error,info) { componentDidCatch(error: any, info: any) {
}
componentDidCatch(error, info) {
this.setState({ error, info }); this.setState({ error, info });
} }
logerr() { logerr() {
const { error, info } = this.state ; const { error, info } = this.state;
console.error('[dome] Catched error:',error,info); console.error('[dome] Catched error:', error, info);
} }
reload() { reload() {
...@@ -55,23 +61,23 @@ export class Catch extends React.Component ...@@ -55,23 +61,23 @@ export class Catch extends React.Component
} }
render() { render() {
const { error, info } = this.state ; const { error, info } = this.state;
if (error) { if (error) {
const { onError, label='Error' } = this.props ; const { onError, label = 'Error' } = this.props;
if (typeof(onError)==='function') if (typeof (onError) === 'function')
return onError(error,info,this.reload); return onError(error, info, this.reload);
else else
return ( return (
<div> <div>
<Button icon='WARNING' kind='warning' <Button icon='WARNING' kind='warning'
title={error} title={error}
onClick={this.logerr} /> onClick={this.logerr} />
<Button icon='RELOAD' onClick={this.reload} /> <Button icon='RELOAD' onClick={this.reload} />
<Label>{label}</Label> <Label>{label}</Label>
</div> </div>
); );
} }
return this.props.children || null ; return this.props.children || null;
} }
} }
......
...@@ -329,7 +329,7 @@ const makeGridItem = (customize: any, onClose: any) => (comp: any) => { ...@@ -329,7 +329,7 @@ const makeGridItem = (customize: any, onClose: any) => (comp: any) => {
<Vfill className="labview-content"> <Vfill className="labview-content">
<Hbox className="labview-titlebar"> <Hbox className="labview-titlebar">
<Hfill> <Hfill>
<Catch title={id}> <Catch label={id}>
<RenderItem id={`labview.title.${id}`}> <RenderItem id={`labview.title.${id}`}>
<Label className="labview-handle" label={label} title={title} /> <Label className="labview-handle" label={label} title={title} />
</RenderItem> </RenderItem>
...@@ -338,7 +338,7 @@ const makeGridItem = (customize: any, onClose: any) => (comp: any) => { ...@@ -338,7 +338,7 @@ const makeGridItem = (customize: any, onClose: any) => (comp: any) => {
{CLOSING} {CLOSING}
</Hbox> </Hbox>
<TitleContext.Provider value={{ id, label, title }}> <TitleContext.Provider value={{ id, label, title }}>
<Catch title={id}>{children}</Catch> <Catch label={id}>{children}</Catch>
</TitleContext.Provider> </TitleContext.Provider>
</Vfill> </Vfill>
</Grids.GridItem> </Grids.GridItem>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment