-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComponentRegistry.ts
163 lines (127 loc) 路 4.21 KB
/
ComponentRegistry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {
BlueBaseModuleRegistry,
BlueBaseModuleRegistryInputItem,
BlueBaseModuleRegistryItem,
} from './BlueBaseModuleRegistry';
import { MaybeThunk, Thunk, getDefiniteBlueBaseModule, isBlueBaseModule } from '../utils';
import { BlueBase } from '../BlueBase';
import { ComponentStyles, applyStyles } from '../models';
import { ItemCollection } from './Registry';
import Loadable from 'react-loadable';
import { ReactLoadableLoading } from '../components';
import flowRight from 'lodash.flowright';
/**
* Definition of the HOC
*/
export type ComponentRegistryHocItem = (...args: any[]) => React.ComponentType<any>;
export type ComponentRegistryHocItemWithArgs<T = any> = [Thunk<ComponentRegistryHocItem>, T];
/**
* Source of this component. Contains information about who registered this component.
*/
export interface ComponentSource {
type: 'plugin' | 'api' | 'custom',
slug: string,
}
interface ComponentRegistryItemExtras {
hocs: Array<ComponentRegistryHocItem | ComponentRegistryHocItemWithArgs>,
source: ComponentSource,
styles: MaybeThunk<ComponentStyles>,
isAsync: boolean,
}
export type ComponentRegistryItem =
BlueBaseModuleRegistryItem<React.ComponentType<any>> & ComponentRegistryItemExtras;
export type ComponentRegistryInputItem =
BlueBaseModuleRegistryInputItem<React.ComponentType<any>> & Partial<ComponentRegistryItemExtras>;
export type ComponentInputCollection = ItemCollection<ComponentRegistryInputItem>;
/**
* 馃巵 ComponentRegistry
*/
export class ComponentRegistry extends BlueBaseModuleRegistry<ComponentRegistryItem, ComponentRegistryInputItem> {
// For proxy methods
[key: string]: any;
constructor(BB: BlueBase) {
super(BB);
// Create proxy to enable BB.Components.Name method
return new Proxy(this, {
get: (target, name, value) => {
if (typeof name === 'string' && typeof (this[name]) === 'undefined') {
if (this.has(name)) {
return this.resolve(name);
}
throw Error(`BlueBase could not find "${name}" component. Did you forget to register it?`);
}
return Reflect.get(target, name, value);
}
});
}
public resolve(...keys: string[]): React.ComponentType<any> {
const item = this.findOne(...keys);
if (!item) {
throw Error(`Could not resolve any of the following components: [${keys.join(', ')}].`);
}
let rawComponent = item.value.isAsync
? Loadable({
loader: () => item.value,
loading: ReactLoadableLoading,
})
: item.value.module as React.ComponentType<any>;
// Add withStyles HOC
rawComponent = applyStyles(item.key, rawComponent, item.styles);
const hocs = item.hocs
.map(hoc => (Array.isArray(hoc) ? hoc[0](hoc[1]) : hoc));
return flowRight([...hocs])(rawComponent);
}
/**
* Adds higher order component to the registered component
* @param {string} key The name of the registered component to whom hocs are to added
* @param {Array<ComponentRegistryHocItem>} hocs The HOCs to compose with the raw component.
*/
addHocs(key: string, ...hocs: Array<ComponentRegistryHocItem | ComponentRegistryHocItemWithArgs>) {
const item = super.get(key);
if (!item) {
throw Error(
`Could not add hocs for "${key}" component. Reason: Component not found.`
);
}
this.set(key, { ...item, hocs, });
}
// public removeHocs() {
// // TODO:
// }
// TODO: Add docs
public setStyles(key: string, styles: MaybeThunk<ComponentStyles>) {
const item = this.get(key);
if (!item) {
throw Error(
`Cannot set styles "${key}" component. Reason: Component not found.`
);
}
item.styles = styles;
this.data = this.data.set(key, item);
}
// TODO: Add docs
public getStyles(key: string): MaybeThunk<ComponentStyles> | undefined {
const item = this.get(key);
if (!item) {
return;
}
return item.styles;
}
protected createItem(key: string, partial: any): ComponentRegistryItem {
const value = getDefiniteBlueBaseModule(partial.value);
return super.createItem(key, {
hocs: [],
isAsync: value.isAsync,
preload: false,
...partial,
value,
});
}
/**
* Typeguard to check a given object is an input value
* @param value
*/
protected isInputValue(value: any): value is ComponentRegistryInputItem['value'] {
return isBlueBaseModule(value) || typeof value === 'function';
}
}