Skip to content

Commit 844d82b

Browse files
committed
feat: make spacing property as optional
1 parent 9610aac commit 844d82b

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/restyleFunctions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,14 @@ export interface BackgroundColorShorthandProps<Theme extends BaseTheme> {
310310

311311
export type SpacingProps<Theme extends BaseTheme> = {
312312
[Key in keyof typeof spacingProperties]?: ResponsiveValue<
313-
keyof Theme['spacing'],
313+
Theme['spacing'] extends object ? keyof Theme['spacing'] : number,
314314
Theme['breakpoints']
315315
>;
316316
};
317317

318318
export type SpacingShorthandProps<Theme extends BaseTheme> = {
319319
[Key in keyof typeof spacingPropertiesShorthand]?: ResponsiveValue<
320-
keyof Theme['spacing'],
320+
Theme['spacing'] extends object ? keyof Theme['spacing'] : number,
321321
Theme['breakpoints']
322322
>;
323323
};

src/test/createRestyleComponent.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ const themeWithVariant = {
4848
},
4949
};
5050

51+
const themeWithoutSpacing = {
52+
...theme,
53+
spacing: undefined,
54+
};
55+
5156
const {breakpoints, ...themeWithoutBreakpoints} = theme;
5257

5358
type Theme = typeof theme;
5459
type ThemeWithVariant = typeof themeWithVariant;
60+
type ThemeWithoutSpacing = typeof themeWithoutSpacing;
5561

5662
const mockUseWindowDimensions = jest.fn();
5763

@@ -67,6 +73,14 @@ const Component = createRestyleComponent<
6773
ViewProps,
6874
Theme
6975
>([backgroundColor, spacing, spacingShorthand, opacity]);
76+
const ComponentWithoutSpacing = createRestyleComponent<
77+
BackgroundColorProps<ThemeWithoutSpacing> &
78+
SpacingProps<ThemeWithoutSpacing> &
79+
SpacingShorthandProps<ThemeWithoutSpacing> &
80+
OpacityProps<ThemeWithoutSpacing> &
81+
ViewProps,
82+
ThemeWithoutSpacing
83+
>([backgroundColor, spacing, spacingShorthand, opacity]);
7084
const cardVariant = createVariant<ThemeWithVariant, 'cardVariants'>({
7185
themeKey: 'cardVariants',
7286
});
@@ -230,5 +244,24 @@ describe('createRestyleComponent', () => {
230244
style: [{gap: 8, columnGap: 8, rowGap: 8}],
231245
});
232246
});
247+
248+
it('passes gap values from the theme when no spacing prop is defined', () => {
249+
const {root} = render(
250+
<ThemeProvider theme={themeWithoutSpacing}>
251+
<ComponentWithoutSpacing
252+
gap={10}
253+
columnGap={20}
254+
rowGap={22}
255+
marginTop={2}
256+
paddingBottom={3}
257+
/>
258+
</ThemeProvider>,
259+
);
260+
expect(root.findByType(View).props).toStrictEqual({
261+
style: [
262+
{gap: 10, columnGap: 20, rowGap: 22, marginTop: 2, paddingBottom: 3},
263+
],
264+
});
265+
});
233266
});
234267
});

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface KnownBaseTheme {
3535
colors: {
3636
[key: string]: string;
3737
};
38-
spacing: {
38+
spacing?: {
3939
[key: string]: number | string;
4040
};
4141
breakpoints?: {

0 commit comments

Comments
 (0)