From cb88c75cd564f023d32ac03dd9d674e5623601ba Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Sun, 11 Feb 2018 00:52:48 +0000 Subject: [PATCH] feat: implement withErrorBoundary() --- README.md | 4 +--- docs/en/ErrorBoundary.md | 19 +++++++++++++++++++ docs/en/README.md | 2 +- src/ErrorBoundary/__story__/story.tsx | 9 +++++++-- src/ErrorBoundary/index.ts | 9 ++++++--- src/util/faccToHoc.ts | 2 +- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 968d4594..8eda1f36 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,6 @@ const MyComponent = mock(); - [``](./docs/en/ViewportSensor.md#viewportscrollsensor) and [``](./docs/en/ViewportSensor.md#viewportobserversensor) - [``](./docs/en/WindowScrollSensor.md), [`withWindowScroll()`](./docs/en/WindowScrollSensor.md#withwindowscroll-hoc), and [`@withWindowScroll`](./docs/en/WindowScrollSensor.md#withwindowscroll-decorator) - [``](./docs/en/WindowSizeSensor.md), [`withWindowSize()`](./docs/en/WindowSizeSensor.md#withwindowsize-hoc), and [`@withWindowSize`](./docs/en/WindowSizeSensor.md#withwindowsize-decorator) - - `ActiveSensor`, `withActive()`, and `@withActive` - - `FocusSensor`, `withFocus()`, and `@withFocus` - [Context](./docs/en/Context.md) - [``](./docs/en/Provider.md#provider), [``](./docs/en/Provider.md#consumer), [`withContext()`](./docs/en/Provider.md#withcontext-hoc), and [`@withContext`](./docs/en/Provider.md#withcontext-decorator) - [``](./docs/en/theme.md#theme), [``](./docs/en/theme.md#themed), [`withTheme()`](./docs/en/theme.md#withtheme-hoc), and [`@withTheme`](./docs/en/theme.md#withtheme-decorator) @@ -81,7 +79,7 @@ const MyComponent = mock(); - [`go()`](./docs/en/routing.md#go), ``, ``, [``](./docs/en/Sms.md), [``](./docs/en/Mailto.md), and `` - [Boundaries](./docs/en/Boundaries.md) - [``](./docs/en/BrowserOnly.md), [``](./docs/en/ServerOnly.md), and [``](./docs/en/ElectronOnly.md) - - [``](./docs/en/ErrorBoundary.md) and `withErrorBoundary()` + - [``](./docs/en/ErrorBoundary.md) and [`withErrorBoundary()`](./docs/en/ErrorBoundary.md#witherrorboundary-hoc) - `` - [UI](./docs/en/UI.md) - [``](./docs/en/Portal.md) and [``](./docs/en/Overlay.md) diff --git a/docs/en/ErrorBoundary.md b/docs/en/ErrorBoundary.md index b241072e..d8d0ae04 100644 --- a/docs/en/ErrorBoundary.md +++ b/docs/en/ErrorBoundary.md @@ -37,3 +37,22 @@ interface IErrorBoundaryState { - `renderError` — renderer called if error happened in error boundary's children. - `onError` — event called every time error detected. + + +## `withErrorBoundary` HOC + +Wraps your component into an error boundary. + +```jsx +import {withErrorBoundary} from 'libreact/lib/ErrorBoundary'; + +const SafeComponent = withErrorBoundary(UnsafeComponent, { + renderError: () =>
Error happened!
+}); +``` + +Signature + +```ts +withErrorBoundary(Comp, boundaryProps: IErrorBoundaryProps) +``` diff --git a/docs/en/README.md b/docs/en/README.md index df7863ce..bdbceee6 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -48,7 +48,7 @@ - [`go()`](./routing.md#go), ``, ``, [``](./Sms.md), [``](./Mailto.md), and `` - [Boundaries](./Boundaries.md) - [``](./BrowserOnly.md), [``](./ServerOnly.md), and [``](./ElectronOnly.md) - - [``](./ErrorBoundary.md) and `withErrorBoundary()` + - [``](./ErrorBoundary.md) and [`withErrorBoundary()`](./ErrorBoundary.md#witherrorboundary-hoc) - `` - [UI](./UI.md) - [``](./Portal.md) and [``](./Overlay.md) diff --git a/src/ErrorBoundary/__story__/story.tsx b/src/ErrorBoundary/__story__/story.tsx index 0a2dd084..2e11dda6 100644 --- a/src/ErrorBoundary/__story__/story.tsx +++ b/src/ErrorBoundary/__story__/story.tsx @@ -2,7 +2,7 @@ import {Component, createElement as h} from 'react'; import {storiesOf} from '@storybook/react'; import {action} from '@storybook/addon-actions'; import {linkTo} from '@storybook/addon-links'; -import {ErrorBoundary} from '..'; +import {ErrorBoundary, withErrorBoundary} from '..'; import ShowDocs from '../../../.storybook/ShowDocs' class BuggyCounter extends Component { @@ -29,6 +29,10 @@ class BuggyCounter extends Component { } } +const Hoc1 = withErrorBoundary(BuggyCounter, { + renderError: () =>
Errror!
+}); + storiesOf('Boundaries/ErrorBoundary', module) .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/ErrorBoundary.md')})) .add('Renders children', () => @@ -42,4 +46,5 @@ storiesOf('Boundaries/ErrorBoundary', module)
Caught error!
}>
- ); + ) + .add('HOC', () => ); diff --git a/src/ErrorBoundary/index.ts b/src/ErrorBoundary/index.ts index 6c857d9e..3210b585 100644 --- a/src/ErrorBoundary/index.ts +++ b/src/ErrorBoundary/index.ts @@ -1,6 +1,5 @@ import {Component} from 'react'; -import faccToHoc from '../util/faccToHoc'; -import {noop} from '../util'; +import {h, noop} from '../util'; export interface IErrorBoundaryProps { children: any; @@ -36,4 +35,8 @@ export class ErrorBoundary extends Component { } } -export const withErrorBoundary = faccToHoc(ErrorBoundary, 'error'); +export const withErrorBoundary = (Comp, boundaryProps) => (props) => { + return h(ErrorBoundary, boundaryProps, + h(Comp, props) + ); +}; diff --git a/src/util/faccToHoc.ts b/src/util/faccToHoc.ts index 993c662c..5a3ee359 100644 --- a/src/util/faccToHoc.ts +++ b/src/util/faccToHoc.ts @@ -20,7 +20,7 @@ const faccToHoc = (Facc, prop?: string, wrapper = noWrap) => { const Enhanced = (props) => h(Facc, faccProps, (state) => wrapper(Comp, propName, props, state)); - return addClassDecoratorSupport(Enhanced); + return isClassDecoratorMethodCall ? addClassDecoratorSupport(Enhanced) : Enhanced; }; return hoc;