-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(🎁 ComponentRegistry): Added a completely new ComponentRegistry
- Loading branch information
Showing
16 changed files
with
5,439 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,7 @@ | |
|
||
## 🏆 Features | ||
|
||
- 🔌 Plugins | ||
- 🎣 Lifecycle events and hooks | ||
- 🎁 Dynamic Components | ||
- 📔 Logging API |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { ComponentInput, ComponentRegistryItem } from './types'; | ||
import { createComponentRegistryItem, isComponentRegistryItem } from './helpers'; | ||
import { BlueRain } from '../../BlueRain'; | ||
import { Registry } from '../Registry'; | ||
import { getAsyncComponent } from './getAsyncComponent'; | ||
import flowRight from 'lodash.flowright'; | ||
|
||
export class ComponentRegistry extends Registry<ComponentRegistryItem> { | ||
|
||
// For proxy methods | ||
[key: string]: any; | ||
|
||
constructor(BR: BlueRain) { | ||
super(BR); | ||
|
||
// Create proxy to enable BR.Components.Name method | ||
return new Proxy(this, { | ||
get: (target, name, value) => { | ||
if (typeof name === 'string' && typeof (this[name]) === 'undefined') { | ||
if (typeof name === 'string') { | ||
|
||
if (this.has(name)) { | ||
return this.resolve(name); | ||
} | ||
throw Error(`Could not find "${name}" component. Did you forget to register it?`); | ||
} | ||
} | ||
|
||
return Reflect.get(target, name, value); | ||
} | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* Register a React Component | ||
* | ||
* "component" (2nd) param can be one of the following: | ||
* | ||
* 1. A RegistryItem with the following variations: | ||
* 1a. ".rawComponent" prop is a react component | ||
* 1b. ".rawComponent" prop is an ES Module | ||
* 1c. ".rawComponent" prop is a promise | ||
* 1d. ".rawComponent" prop is a BlueRain Module | ||
* | ||
* 2. A react compoment with following variations: | ||
* 2a. Is a react component | ||
* 2b. Is an ES module that has a react component in .default prop | ||
* 2c. Is a promise that resolves a react component (In ES module or otherwise) | ||
* 2d. Is a BlueRainModule that resolves a react component | ||
* | ||
* @param name | ||
* @param component | ||
*/ | ||
public async register(name: string, component: ComponentInput) { | ||
|
||
// (Point 1) If the input component is an object of RegistryItem | ||
if (isComponentRegistryItem(component)) { | ||
|
||
this.set(name, createComponentRegistryItem({ | ||
...component, | ||
rawComponent: component.rawComponent, | ||
})); | ||
return; | ||
} | ||
|
||
// (Point 2) If the input component react component | ||
this.set(name, createComponentRegistryItem({ | ||
rawComponent: component as React.ComponentType<any> | ||
})); | ||
} | ||
|
||
|
||
public resolve(name: string) { | ||
|
||
const registryItem = super.get(name); | ||
|
||
if (!registryItem) { | ||
throw new Error(`Component "${name}" is registered.`); | ||
} | ||
|
||
const rawComponent = getAsyncComponent(registryItem.rawComponent.promise); | ||
|
||
const hocs = registryItem.hocs.map(hoc => (Array.isArray(hoc) ? hoc[0](hoc[1]) : hoc)); | ||
|
||
return flowRight([...hocs])(rawComponent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Loadable, { LoadingComponentProps } from 'react-loadable'; | ||
import React from 'react'; | ||
|
||
export function getAsyncComponent(componentPromise: Promise<React.ComponentType<any>>) { | ||
|
||
return Loadable({ | ||
loader: () => componentPromise, | ||
|
||
loading(props: LoadingComponentProps) { | ||
if (props.error) { | ||
return <div>Error! <button onClick={props.retry}>Retry</button></div>; | ||
} else if (props.timedOut) { | ||
return <div>Taking a long time... <button onClick={props.retry}>Retry</button></div>; | ||
} else if (props.pastDelay) { | ||
return <div>Loading...</div>; | ||
} else { | ||
return null; | ||
} | ||
}, | ||
|
||
render(Component: React.ComponentType<any>, props: any) { | ||
return <Component {...props} />; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ComponentRegistryItem, ComponentRegistryItemInternal } from './types'; | ||
import { getDefiniteBlueRainModule } from '../../api'; | ||
|
||
export function createComponentRegistryItem(input: ComponentRegistryItemInternal): ComponentRegistryItem { | ||
|
||
if (!input.rawComponent) { | ||
// tslint:disable-next-line:max-line-length | ||
throw Error(`"rawComponent" property is required to register a component. Please provide valid component while adding component.`); | ||
} | ||
|
||
const rawComponent = getDefiniteBlueRainModule(input.rawComponent); | ||
|
||
return { | ||
hocs: [], | ||
isAsync: rawComponent.isAsync, | ||
preload: false, | ||
|
||
...input, | ||
|
||
rawComponent, | ||
}; | ||
} | ||
|
||
/** | ||
* Type guard to check if an object is a ComponentRegistryItem | ||
* @param item | ||
*/ | ||
export function isComponentRegistryItem(item: any): item is ComponentRegistryItem { | ||
return (item as ComponentRegistryItem).rawComponent !== undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ComponentRegistry'; |
Oops, something went wrong.