@@ -5,6 +5,10 @@ import {
5
5
ElementType ,
6
6
ReactHTML ,
7
7
Attributes ,
8
+ Component ,
9
+ ComponentType ,
10
+ createRef ,
11
+ forwardRef
8
12
} from 'react' ;
9
13
import { incorporate } from './incorporate' ;
10
14
@@ -17,6 +21,78 @@ type PropsLike<P> = P & PropsExtensions & Attributes;
17
21
18
22
type Children = string | Array < ReactNode > ;
19
23
24
+ export function domPropify ( Comp : any ) : ComponentType < any > {
25
+ class DomProps extends Component < any , any > {
26
+ private ref : any ;
27
+ private domProps : any ;
28
+ constructor ( props ) {
29
+ super ( props ) ;
30
+ this . domProps = this . props . domProps ;
31
+ this . ref = props . forwardedRef || createRef ( ) ;
32
+ }
33
+
34
+ public componentDidMount ( ) {
35
+ if ( this . domProps && this . ref ) {
36
+ Object . entries ( this . domProps ) . forEach ( ( [ key , val ] ) => {
37
+ this . ref . current [ key ] = val ;
38
+ } ) ;
39
+ }
40
+ }
41
+
42
+ render ( ) {
43
+ const p : any = { ref : this . ref , ...this . props } ;
44
+ delete p . forwardedRef
45
+ delete p . domProps ;
46
+ return createElement ( Comp , p ) ;
47
+ }
48
+ }
49
+
50
+ return forwardRef ( ( props , ref ) => {
51
+ return createElement ( DomProps , { ...props , forwardedRef : ref } ) ;
52
+ } ) ;
53
+ }
54
+
55
+ export function domHookify ( Comp : any ) : ComponentType < any > {
56
+ class DomHooks extends Component < any , any > {
57
+ private ref : any ;
58
+ private hooks : any ;
59
+ constructor ( props ) {
60
+ super ( props ) ;
61
+ this . hooks = this . props . domHooks ;
62
+ this . ref = props . forwardedRef || createRef ( ) ;
63
+ }
64
+
65
+ public componentDidMount ( ) {
66
+ if ( this . hooks && this . hooks . insert && this . ref ) {
67
+ this . hooks . insert ( { elm : this . ref . current } )
68
+ }
69
+ }
70
+
71
+ public componentDidUpdate ( ) {
72
+ if ( this . hooks && this . hooks . update && this . ref ) {
73
+ this . hooks . update ( { elm : this . ref . current } )
74
+ }
75
+ }
76
+
77
+ public componentWillUnmount ( ) {
78
+ if ( this . hooks && this . hooks . destroy && this . ref ) {
79
+ this . hooks . destroy ( { elm : this . ref . current } )
80
+ }
81
+ }
82
+
83
+ render ( ) {
84
+ const p : any = { ref : this . ref , ...this . props } ;
85
+ delete p . forwardedRef
86
+ delete p . domHooks ;
87
+ return createElement ( Comp , p ) ;
88
+ }
89
+ }
90
+
91
+ return forwardRef ( ( props , ref ) => {
92
+ return createElement ( DomHooks , { ...props , forwardedRef : ref } ) ;
93
+ } ) ;
94
+ }
95
+
20
96
function createElementSpreading < P = any > (
21
97
type : ElementType < P > | keyof ReactHTML ,
22
98
props : PropsLike < P > | null ,
@@ -36,7 +112,7 @@ function hyperscriptProps<P = any>(
36
112
if ( ! props . sel ) {
37
113
return createElement ( type , props ) ;
38
114
} else {
39
- return createElement ( incorporate ( type ) , props ) ;
115
+ return createElement ( domHookify ( domPropify ( incorporate ( type ) ) ) , props ) ;
40
116
}
41
117
}
42
118
@@ -55,7 +131,7 @@ function hyperscriptPropsChildren<P = any>(
55
131
if ( ! props . sel ) {
56
132
return createElementSpreading ( type , props , children ) ;
57
133
} else {
58
- return createElementSpreading ( incorporate ( type ) , props , children ) ;
134
+ return createElementSpreading ( domHookify ( domPropify ( incorporate ( type ) ) ) , props , children ) ;
59
135
}
60
136
}
61
137
0 commit comments