@@ -5,6 +5,10 @@ import {
55 ElementType ,
66 ReactHTML ,
77 Attributes ,
8+ Component ,
9+ ComponentType ,
10+ createRef ,
11+ forwardRef
812} from 'react' ;
913import { incorporate } from './incorporate' ;
1014
@@ -17,6 +21,78 @@ type PropsLike<P> = P & PropsExtensions & Attributes;
1721
1822type Children = string | Array < ReactNode > ;
1923
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+
2096function createElementSpreading < P = any > (
2197 type : ElementType < P > | keyof ReactHTML ,
2298 props : PropsLike < P > | null ,
@@ -36,7 +112,7 @@ function hyperscriptProps<P = any>(
36112 if ( ! props . sel ) {
37113 return createElement ( type , props ) ;
38114 } else {
39- return createElement ( incorporate ( type ) , props ) ;
115+ return createElement ( domHookify ( domPropify ( incorporate ( type ) ) ) , props ) ;
40116 }
41117}
42118
@@ -55,7 +131,7 @@ function hyperscriptPropsChildren<P = any>(
55131 if ( ! props . sel ) {
56132 return createElementSpreading ( type , props , children ) ;
57133 } else {
58- return createElementSpreading ( incorporate ( type ) , props , children ) ;
134+ return createElementSpreading ( domHookify ( domPropify ( incorporate ( type ) ) ) , props , children ) ;
59135 }
60136}
61137
0 commit comments