1
- import { PureComponent , createElement } from 'react' ;
1
+ import { PureComponent , createElement , createRef } from 'react' ;
2
2
import { Scope } from './scope' ;
3
3
4
4
type Props = {
@@ -12,11 +12,44 @@ type State = {
12
12
flip : boolean ;
13
13
} ;
14
14
15
+ let moduleEntries : any = [ ]
16
+
17
+ let onMounts : any [ ] = [ ]
18
+ let onUpdates : any [ ] = [ ]
19
+ let onUnmounts : any [ ] = [ ]
20
+
21
+ export function setModules ( mods : any ) {
22
+ if ( mods === null || typeof mods !== 'object' ) return ;
23
+ moduleEntries = Object . entries ( mods )
24
+ onMounts = moduleEntries . map ( mod => [ mod [ 0 ] , mod [ 1 ] . componentDidMount ] ) . filter ( mod => mod [ 1 ] )
25
+ onUpdates = moduleEntries . map ( mod => [ mod [ 0 ] , mod [ 1 ] . componentDidUpdate ] ) . filter ( mod => mod [ 1 ] )
26
+ onUnmounts = moduleEntries . map ( mod => [ mod [ 0 ] , mod [ 1 ] . componentWillUnmount ] ) . filter ( mod => mod [ 1 ] )
27
+ }
28
+
29
+ export function hasModuleProps ( props ) {
30
+ return props
31
+ ? moduleEntries . some ( ( [ mkey ] ) => props . hasOwnProperty ( mkey ) )
32
+ : false
33
+ }
34
+
35
+ function moduleProcessor ( base , ref , props ) {
36
+ if ( ref && ref . current && base . length ) {
37
+ base . forEach ( ( [ key , f ] ) => {
38
+ f ( ref . current , props [ key ] )
39
+ } ) ;
40
+ }
41
+
42
+ }
43
+
15
44
export default class Incorporator extends PureComponent < Props , State > {
45
+ private ref : any ;
46
+ private moduleProps : string ;
47
+
16
48
constructor ( props : Props ) {
17
49
super ( props ) ;
18
50
this . state = { flip : false } ;
19
51
this . selector = props . targetProps . sel ;
52
+ this . ref = props . targetRef || ( moduleEntries . some ( e => Object . keys ( props . targetProps ) . some ( key => key === e [ 0 ] ) ) ? createRef ( ) : null ) ;
20
53
}
21
54
22
55
private selector : string | symbol ;
@@ -26,6 +59,12 @@ export default class Incorporator extends PureComponent<Props, State> {
26
59
this . unsubscribe = this . props . scope . subscribe ( this . selector , ( ) => {
27
60
this . setState ( ( prev : any ) => ( { flip : ! prev . flip } ) ) ;
28
61
} ) ;
62
+
63
+ moduleProcessor ( onMounts , this . ref , this . props . targetProps )
64
+ }
65
+
66
+ public componentDidUpdate ( ) {
67
+ moduleProcessor ( onUpdates , this . ref , this . props . targetProps )
29
68
}
30
69
31
70
private incorporateHandlers < P > ( props : P , scope : Scope ) : P {
@@ -38,19 +77,21 @@ export default class Incorporator extends PureComponent<Props, State> {
38
77
}
39
78
40
79
private materializeTargetProps ( ) {
41
- const { targetProps, targetRef , scope} = this . props ;
80
+ const { targetProps, scope} = this . props ;
42
81
let output = { ...targetProps } ;
43
82
output = this . incorporateHandlers ( output , scope ) ;
44
- if ( targetRef ) {
45
- output . ref = targetRef ;
83
+ if ( this . ref ) {
84
+ output . ref = this . ref ;
46
85
}
47
86
delete output . sel ;
87
+ moduleEntries . forEach ( pair => delete output [ pair [ 0 ] ] )
48
88
return output ;
49
89
}
50
90
51
91
public render ( ) {
52
92
const { target} = this . props ;
53
93
const targetProps = this . materializeTargetProps ( ) ;
94
+
54
95
if ( targetProps . children ) {
55
96
return createElement ( target , targetProps , targetProps . children ) ;
56
97
} else {
@@ -59,6 +100,8 @@ export default class Incorporator extends PureComponent<Props, State> {
59
100
}
60
101
61
102
public componentWillUnmount ( ) {
103
+ moduleProcessor ( onUnmounts , this . ref , this . props . targetProps )
104
+
62
105
this . unsubscribe ( ) ;
63
106
}
64
107
}
0 commit comments