@@ -15,12 +15,43 @@ import {incorporate} from './incorporate';
15
15
export type PropsExtensions = {
16
16
sel ?: string | symbol ;
17
17
domProps ?: any
18
+ domHook ?: any
19
+ domClass ?: any
18
20
} ;
19
21
20
22
type PropsLike < P > = P & PropsExtensions & Attributes ;
21
23
22
24
type Children = string | Array < ReactNode > ;
23
25
26
+ export function domClassify ( Comp : any ) : ComponentType < any > {
27
+ class DomProps extends Component < any , any > {
28
+ private ref : any ;
29
+ private additionalClassname : string ;
30
+ constructor ( props ) {
31
+ super ( props ) ;
32
+ this . additionalClassname = this . props . domClass
33
+ ? ( ' ' + Object . entries ( this . props . domClass ) . filter ( x => x [ 1 ] ) . map ( x => x [ 0 ] ) . join ( ' ' ) )
34
+ : ''
35
+ this . ref = props . forwardedRef || createRef ( ) ;
36
+ }
37
+
38
+ render ( ) {
39
+ const p : any = {
40
+ ref : this . ref ,
41
+ ...this . props ,
42
+ className : ( this . props . className || '' ) + this . additionalClassname
43
+ }
44
+ delete p . forwardedRef
45
+ delete p . domClass ;
46
+ return createElement ( Comp , p ) ;
47
+ }
48
+ }
49
+
50
+ return forwardRef ( ( props , ref ) => {
51
+ return createElement ( DomProps , { ...props , forwardedRef : ref } ) ;
52
+ } ) ;
53
+ }
54
+
24
55
export function domPropify ( Comp : any ) : ComponentType < any > {
25
56
class DomProps extends Component < any , any > {
26
57
private ref : any ;
@@ -55,28 +86,28 @@ export function domPropify(Comp: any): ComponentType<any> {
55
86
export function domHookify ( Comp : any ) : ComponentType < any > {
56
87
class DomHooks extends Component < any , any > {
57
88
private ref : any ;
58
- private hooks : any ;
89
+ private hook : any ;
59
90
constructor ( props ) {
60
91
super ( props ) ;
61
- this . hooks = this . props . domHooks ;
92
+ this . hook = this . props . domHook ;
62
93
this . ref = props . forwardedRef || createRef ( ) ;
63
94
}
64
95
65
96
public componentDidMount ( ) {
66
- if ( this . hooks && this . hooks . insert && this . ref ) {
67
- this . hooks . insert ( { elm : this . ref . current } )
97
+ if ( this . hook && this . hook . insert && this . ref ) {
98
+ this . hook . insert ( { elm : this . ref . current } )
68
99
}
69
100
}
70
101
71
102
public componentDidUpdate ( ) {
72
- if ( this . hooks && this . hooks . update && this . ref ) {
73
- this . hooks . update ( { elm : this . ref . current } )
103
+ if ( this . hook && this . hook . update && this . ref ) {
104
+ this . hook . update ( { elm : this . ref . current } )
74
105
}
75
106
}
76
107
77
108
public componentWillUnmount ( ) {
78
- if ( this . hooks && this . hooks . destroy && this . ref ) {
79
- this . hooks . destroy ( { elm : this . ref . current } )
109
+ if ( this . hook && this . hook . destroy && this . ref ) {
110
+ this . hook . destroy ( { elm : this . ref . current } )
80
111
}
81
112
}
82
113
@@ -109,10 +140,10 @@ function hyperscriptProps<P = any>(
109
140
type : ElementType < P > | keyof ReactHTML ,
110
141
props : PropsLike < P > ,
111
142
) : ReactElement < P > {
112
- if ( ! props . sel ) {
143
+ if ( ! props . sel && ! props . domClass && ! props . domHook ) {
113
144
return createElement ( type , props ) ;
114
145
} else {
115
- return createElement ( domHookify ( domPropify ( incorporate ( type ) ) ) , props ) ;
146
+ return createElement ( domHookify ( domPropify ( domClassify ( incorporate ( type ) ) ) ) , props ) ;
116
147
}
117
148
}
118
149
@@ -128,10 +159,10 @@ function hyperscriptPropsChildren<P = any>(
128
159
props : PropsLike < P > ,
129
160
children : Children ,
130
161
) : ReactElement < P > {
131
- if ( ! props . sel ) {
162
+ if ( ! props . sel && ! props . domClass && ! props . domHook ) {
132
163
return createElementSpreading ( type , props , children ) ;
133
164
} else {
134
- return createElementSpreading ( domHookify ( domPropify ( incorporate ( type ) ) ) , props , children ) ;
165
+ return createElementSpreading ( domHookify ( domPropify ( domClassify ( incorporate ( type ) ) ) ) , props , children ) ;
135
166
}
136
167
}
137
168
0 commit comments