diff --git a/src/hooks/useRestyle.ts b/src/hooks/useRestyle.ts index 01d71441..671d9a46 100644 --- a/src/hooks/useRestyle.ts +++ b/src/hooks/useRestyle.ts @@ -7,14 +7,15 @@ import useTheme from './useTheme'; const filterRestyleProps = < TRestyleProps, - TProps extends {[key: string]: unknown} & TRestyleProps, + TProps extends {[key: string]: unknown} & TRestyleProps & {variant?: unknown}, >( componentProps: TProps, omitPropertiesMap: {[key in keyof TProps]: boolean}, ) => { - const cleanProps: TProps = {} as TProps; - const restyleProps: TProps & {variant?: unknown} = {} as TProps; + const cleanProps: Partial = {}; + const restyleProps: Partial = {}; let serializedRestyleProps = ''; + if (omitPropertiesMap.variant) { restyleProps.variant = componentProps.variant ?? 'defaults'; } @@ -27,6 +28,9 @@ const filterRestyleProps = < } } + if (restyleProps.variant) { + cleanProps.variant = restyleProps.variant; + } const keys = {cleanProps, restyleProps, serializedRestyleProps}; return keys; }; diff --git a/src/test/createRestyleComponent.test.tsx b/src/test/createRestyleComponent.test.tsx index 9fe1abce..a4c55463 100644 --- a/src/test/createRestyleComponent.test.tsx +++ b/src/test/createRestyleComponent.test.tsx @@ -179,36 +179,6 @@ describe('createRestyleComponent', () => { ); }); - it('passes styles from default variant when no variant prop is defined', () => { - const {root} = render( - - - , - ); - expect(root.findByType(View).props.style).toStrictEqual([ - { - alignItems: 'flex-start', - backgroundColor: '#FFB6C1', - margin: 8, - }, - ]); - }); - - it('passes styles from the defined variant', () => { - const {root} = render( - - - , - ); - expect(root.findByType(View).props.style).toStrictEqual([ - { - alignItems: 'center', - backgroundColor: '#E0FFFF', - margin: 8, - }, - ]); - }); - it('uses gap values from the theme', () => { const {root} = render( @@ -230,5 +200,54 @@ describe('createRestyleComponent', () => { style: [{gap: 8, columnGap: 8, rowGap: 8}], }); }); + + describe('variant', () => { + it('does not pass variant prop if no variant is created', () => { + const {root} = render( + + + , + ); + expect(root.findByType(View).props).toStrictEqual({ + style: [{opacity: 0.5}], + }); + }); + + it('passes styles from default variant when no variant prop is defined', () => { + const {root} = render( + + + , + ); + expect(root.findByType(View).props).toStrictEqual({ + variant: 'defaults', + style: [ + { + alignItems: 'flex-start', + backgroundColor: '#FFB6C1', + margin: 8, + }, + ], + }); + }); + + it('passes styles from the defined variant', () => { + const {root} = render( + + + , + ); + expect(root.findByType(View).props).toStrictEqual({ + variant: 'regular', + style: [ + { + alignItems: 'center', + backgroundColor: '#E0FFFF', + margin: 8, + }, + ], + }); + }); + }); }); });