-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathModulizer.ts
More file actions
82 lines (68 loc) · 2.31 KB
/
Modulizer.ts
File metadata and controls
82 lines (68 loc) · 2.31 KB
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
import {Component, ComponentType, forwardRef, createRef, createElement} from 'react';
import Incorporator from './Incorporator'
let moduleEntries: any = []
let onMounts: any[] = []
let onUpdates: any[] = []
let onUnmounts: any[] = []
export function setModules(mods: any) {
if (mods === null || typeof mods !== 'object') return;
moduleEntries = Object.entries(mods)
onMounts = moduleEntries.map(mod => [mod[0], mod[1].componentDidMount]).filter(mod => mod[1])
onUpdates = moduleEntries.map(mod => [mod[0], mod[1].componentDidUpdate]).filter(mod => mod[1])
onUnmounts = moduleEntries.map(mod => [mod[0], mod[1].componentWillUnmount]).filter(mod => mod[1])
}
export function usesModules() {
return moduleEntries.length > 0
}
export function hasModuleProps (props) {
return props
? moduleEntries.some(([mkey]) => props.hasOwnProperty(mkey))
: false
}
function moduleProcessor (base, current, props) {
if (current && base.length) {
base.forEach(([key, f]) => {
const prop = props[key]
if (prop) f(current, prop)
});
}
}
export class Modulizer extends Component<any, any> {
private ref: any;
private element: any;
private setRef: any;
constructor(props) {
super(props);
this.element = null
const {targetProps, targetRef} = props
const useRef = hasModuleProps(targetProps)
if (targetRef) {
if (typeof targetRef === 'function' && useRef) {
this.setRef = element => {
this.element = element;
targetRef(element);
};
this.ref = this.setRef;
} else {
this.ref = targetRef;
}
} else {
this.ref = useRef ? createRef() : null;
}
}
public componentDidMount() {
moduleProcessor(onMounts, this.element || (this.ref && this.ref.current), this.props.targetProps)
}
public componentDidUpdate() {
moduleProcessor(onUpdates, this.element || (this.ref && this.ref.current), this.props.targetProps)
}
public componentWillUnmount() {
moduleProcessor(onUnmounts, this.element || (this.ref && this.ref.current), this.props.targetProps)
}
render() {
const targetProps = {...this.props.targetProps}
moduleEntries.forEach(pair => delete targetProps[pair[0]])
const output: any = {...this.props, targetRef: this.ref, targetProps};
return createElement(Incorporator, output);
}
}