11import { useMemo } from 'react' ;
2- import { StyleProp } from 'react-native' ;
2+ import { StyleProp , ViewStyle , TextStyle , ImageStyle } from 'react-native' ;
33
4- import { BaseTheme , RestyleFunctionContainer , RNStyle } from '../types' ;
5- import composeRestyleFunctions from '../composeRestyleFunctions' ;
4+ import { BaseTheme , RNStyle , Dimensions } from '../types' ;
65import { getKeys } from '../typeHelpers' ;
76
87import useDimensions from './useDimensions' ;
@@ -13,24 +12,20 @@ const filterRestyleProps = <
1312 TProps extends Record < string , unknown > & TRestyleProps
1413> (
1514 props : TProps ,
16- omitList : ( keyof TRestyleProps ) [ ] ,
17- ) : Omit < TProps , keyof TRestyleProps > => {
18- const omittedProp = omitList . reduce < Record < keyof TRestyleProps , boolean > > (
19- ( acc , prop ) => {
20- acc [ prop ] = true ;
21- return acc ;
22- } ,
23- { } as Record < keyof TRestyleProps , boolean > ,
24- ) ;
25-
15+ omitPropertiesMap : Record < keyof TProps , boolean > ,
16+ ) => {
2617 return getKeys ( props ) . reduce (
27- ( acc , key ) => {
28- if ( ! omittedProp [ key as keyof TRestyleProps ] ) {
29- acc [ key ] = props [ key ] ;
18+ ( { cleanProps, restyleProps} , key ) => {
19+ if ( omitPropertiesMap [ key as keyof TProps ] ) {
20+ return { cleanProps, restyleProps : { ...restyleProps , [ key ] : props [ key ] } } ;
21+ } else {
22+ return { cleanProps : { ...cleanProps , [ key ] : props [ key ] } , restyleProps} ;
3023 }
31- return acc ;
3224 } ,
33- { } as TProps ,
25+ { cleanProps : { } , restyleProps : { } } as {
26+ cleanProps : TProps ;
27+ restyleProps : TRestyleProps ;
28+ } ,
3429 ) ;
3530} ;
3631
@@ -39,28 +34,39 @@ const useRestyle = <
3934 TRestyleProps extends Record < string , any > ,
4035 TProps extends TRestyleProps & { style ?: StyleProp < RNStyle > }
4136> (
42- restyleFunctions : (
43- | RestyleFunctionContainer < TProps , Theme >
44- | RestyleFunctionContainer < TProps , Theme > [ ] ) [ ] ,
37+ composedRestyleFunction : {
38+ buildStyle : < TInputProps extends TProps > (
39+ props : TInputProps ,
40+ {
41+ theme,
42+ dimensions,
43+ } : {
44+ theme : Theme ;
45+ dimensions : Dimensions ;
46+ } ,
47+ ) => ViewStyle | TextStyle | ImageStyle ;
48+ properties : ( keyof TProps ) [ ] ;
49+ propertiesMap : Record < keyof TProps , boolean > ;
50+ } ,
4551 props : TProps ,
4652) => {
4753 const theme = useTheme < Theme > ( ) ;
4854
4955 const dimensions = useDimensions ( ) ;
5056
5157 const restyled = useMemo ( ( ) => {
52- const composedRestyleFunction = composeRestyleFunctions ( restyleFunctions ) ;
53- const style = composedRestyleFunction . buildStyle ( props , {
58+ const { cleanProps, restyleProps} = filterRestyleProps (
59+ props ,
60+ composedRestyleFunction . propertiesMap ,
61+ ) ;
62+ const style = composedRestyleFunction . buildStyle ( restyleProps , {
5463 theme,
5564 dimensions,
5665 } ) ;
57- const cleanProps = filterRestyleProps (
58- props ,
59- composedRestyleFunction . properties ,
60- ) ;
61- ( cleanProps as TProps ) . style = [ style , props . style ] . filter ( Boolean ) ;
66+
67+ cleanProps . style = [ style , props . style ] . filter ( Boolean ) ;
6268 return cleanProps ;
63- } , [ restyleFunctions , props , dimensions , theme ] ) ;
69+ } , [ composedRestyleFunction , props , dimensions , theme ] ) ;
6470
6571 return restyled ;
6672} ;
0 commit comments