Skip to content

Commit 3bc96c8

Browse files
dominiclinddominic
authored andcommitted
pass along variant name to restyled component
1 parent 5fbb3f4 commit 3bc96c8

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"**/*.js": true
2222
},
2323
"editor.codeActionsOnSave": {
24-
"source.fixAll.eslint": true
24+
"source.fixAll.eslint": "explicit"
2525
},
2626
"editor.formatOnSave": true,
2727
"[typescriptreact]": {

src/hooks/useRestyle.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import useTheme from './useTheme';
77

88
const filterRestyleProps = <
99
TRestyleProps,
10-
TProps extends {[key: string]: unknown} & TRestyleProps,
10+
TProps extends {[key: string]: unknown} & TRestyleProps & {variant?: unknown},
1111
>(
1212
componentProps: TProps,
1313
omitPropertiesMap: {[key in keyof TProps]: boolean},
1414
) => {
15-
const cleanProps: TProps = {} as TProps;
16-
const restyleProps: TProps & {variant?: unknown} = {} as TProps;
15+
const cleanProps: Partial<TProps> = {};
16+
const restyleProps: Partial<TProps> = {};
1717
let serializedRestyleProps = '';
18+
1819
if (omitPropertiesMap.variant) {
1920
restyleProps.variant = componentProps.variant ?? 'defaults';
2021
}
@@ -27,6 +28,9 @@ const filterRestyleProps = <
2728
}
2829
}
2930

31+
if (restyleProps.variant) {
32+
cleanProps.variant = restyleProps.variant;
33+
}
3034
const keys = {cleanProps, restyleProps, serializedRestyleProps};
3135
return keys;
3236
};

src/test/createRestyleComponent.test.tsx

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -179,36 +179,6 @@ describe('createRestyleComponent', () => {
179179
);
180180
});
181181

182-
it('passes styles from default variant when no variant prop is defined', () => {
183-
const {root} = render(
184-
<ThemeProvider theme={themeWithVariant}>
185-
<ComponentWithVariant margin="s" />
186-
</ThemeProvider>,
187-
);
188-
expect(root.findByType(View).props.style).toStrictEqual([
189-
{
190-
alignItems: 'flex-start',
191-
backgroundColor: '#FFB6C1',
192-
margin: 8,
193-
},
194-
]);
195-
});
196-
197-
it('passes styles from the defined variant', () => {
198-
const {root} = render(
199-
<ThemeProvider theme={themeWithVariant}>
200-
<ComponentWithVariant variant="regular" margin="s" />
201-
</ThemeProvider>,
202-
);
203-
expect(root.findByType(View).props.style).toStrictEqual([
204-
{
205-
alignItems: 'center',
206-
backgroundColor: '#E0FFFF',
207-
margin: 8,
208-
},
209-
]);
210-
});
211-
212182
it('uses gap values from the theme', () => {
213183
const {root} = render(
214184
<ThemeProvider theme={theme}>
@@ -230,5 +200,54 @@ describe('createRestyleComponent', () => {
230200
style: [{gap: 8, columnGap: 8, rowGap: 8}],
231201
});
232202
});
203+
204+
describe('variant', () => {
205+
it('does not pass variant prop if no variant is created', () => {
206+
const {root} = render(
207+
<ThemeProvider theme={theme}>
208+
<Component opacity={0.5} />
209+
</ThemeProvider>,
210+
);
211+
expect(root.findByType(View).props).toStrictEqual({
212+
style: [{opacity: 0.5}],
213+
});
214+
});
215+
216+
it('passes styles from default variant when no variant prop is defined', () => {
217+
const {root} = render(
218+
<ThemeProvider theme={themeWithVariant}>
219+
<ComponentWithVariant margin="s" />
220+
</ThemeProvider>,
221+
);
222+
expect(root.findByType(View).props).toStrictEqual({
223+
variant: 'defaults',
224+
style: [
225+
{
226+
alignItems: 'flex-start',
227+
backgroundColor: '#FFB6C1',
228+
margin: 8,
229+
},
230+
],
231+
});
232+
});
233+
234+
it('passes styles from the defined variant', () => {
235+
const {root} = render(
236+
<ThemeProvider theme={themeWithVariant}>
237+
<ComponentWithVariant variant="regular" margin="s" />
238+
</ThemeProvider>,
239+
);
240+
expect(root.findByType(View).props).toStrictEqual({
241+
variant: 'regular',
242+
style: [
243+
{
244+
alignItems: 'center',
245+
backgroundColor: '#E0FFFF',
246+
margin: 8,
247+
},
248+
],
249+
});
250+
});
251+
});
233252
});
234253
});

0 commit comments

Comments
 (0)