Skip to content

Commit

Permalink
feat(🎣 BlueRainHook Component): New component to handler async hook l…
Browse files Browse the repository at this point in the history
…ogic

Processes the async hook logic and returns final value as a param in
render prop function
  • Loading branch information
artalat committed Sep 30, 2018
1 parent 111d55e commit ffe5e48
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions COMMIT.md
Expand Up @@ -4,7 +4,9 @@ Emoji reference of Commitizen scopes for this repo:
📦 BlueRainModule
🎁 ComponentRegistry
🎣 HookRegistry
🎣 BlueRainHook Component
🗿 Icons
🍱 JSON Schema
🔌 PluginRegistry
📖 README
📕 Storybook
Expand Down
45 changes: 45 additions & 0 deletions src/components/BlueRainHook/BlueRainHook.tsx
@@ -0,0 +1,45 @@
import { BlueRain } from '../../BlueRain';
import { BlueRainConsumer } from '../../Context';
import Loadable from 'react-loadable';
import React from 'react';

// const example = () => (
// <BlueRainHook hook="bluerain.event" value={5} args={{ foo: 'bar' }}>
// {(value) => console.log(value)}
// </BlueRainHook>
// );

export interface BlueRainHookProperties<T = any> {
hook: string;
value: T;
args?: { [key: string]: any };
children: ((value: any) => React.ReactNode);
}

export class BlueRainHook extends React.PureComponent<BlueRainHookProperties> {

public static defaultProps = {
args: {}
};

render() {

const { hook, value, args, children } = this.props;

return (
<BlueRainConsumer children={(BR: BlueRain) => {

const AsyncBlueRainHook = Loadable({
loader: () => BR.Hooks.run(hook, value, args),
loading: () => <BR.Components.Text>Loading</BR.Components.Text>,
render(loadedValue: any) {
return children(loadedValue);
}
});

return <AsyncBlueRainHook />;

}} />
);
}
}
1 change: 1 addition & 0 deletions src/components/BlueRainHook/index.ts
@@ -0,0 +1 @@
export * from './BlueRainHook';

0 comments on commit ffe5e48

Please sign in to comment.