11import React from 'react' ;
22
3- const useCustomElement = props => {
3+ const useCustomElement = ( props , customMapping = { } ) => {
44 const ref = React . createRef ( ) ;
55
66 React . useLayoutEffect ( ( ) => {
77 const fns = Object . keys ( props )
88 . filter ( key => props [ key ] instanceof Function )
99 . map ( key => ( {
10- key,
11- fn : customEvent => props [ key ] ( customEvent . detail ) ,
10+ key : customMapping [ key ] || key ,
11+ fn : customEvent =>
12+ props [ key ] ( customEvent . detail , customEvent ) ,
1213 } ) ) ;
1314
1415 fns . forEach ( ( { key, fn } ) =>
@@ -21,19 +22,19 @@ const useCustomElement = props => {
2122 ) ;
2223 } , [ ] ) ;
2324
24- const customElementProps = Object . keys ( props ) . reduce ( ( acc , key ) => {
25- const prop = props [ key ] ;
25+ const customElementProps = Object . keys ( props )
26+ . filter ( key => ! ( props [ key ] instanceof Function ) )
27+ . reduce ( ( acc , key ) => {
28+ const prop = props [ key ] ;
2629
27- if ( prop instanceof Object || prop instanceof Array ) {
28- return { ...acc , [ key ] : JSON . stringify ( prop ) } ;
29- }
30+ const computedKey = customMapping [ key ] || key ;
3031
31- if ( prop instanceof Function ) {
32- return acc ;
33- }
32+ if ( prop instanceof Object || prop instanceof Array ) {
33+ return { ... acc , [ computedKey ] : JSON . stringify ( prop ) } ;
34+ }
3435
35- return { ...acc , [ key ] : prop } ;
36- } , { } ) ;
36+ return { ...acc , [ computedKey ] : prop } ;
37+ } , { } ) ;
3738
3839 return [ customElementProps , ref ] ;
3940} ;
0 commit comments