Skip to content

Commit 9dac265

Browse files
authored
fix: spread Box props correctly
Closes issue #190 * test: add test that reproduces the bug * fix: add default theme declaration * fix: avoid collision of `color` prop * chore: remove StyledComponentBase typing * fix: update prop compatibility * test: remove snapshot
1 parent a3f7219 commit 9dac265

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

src/components/Box/Box.spec.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { render } from '@testing-library/react';
22
import * as React from 'react';
33
import { Colors } from '../../essentials';
4-
import { Box } from './Box';
4+
import { Box, BoxProps } from './Box';
55

66
describe('Box', () => {
77
it('renders without any props', () => {
@@ -33,4 +33,12 @@ describe('Box', () => {
3333
render(<Box gridColumnGap={2} gridTemplateRows="1fr auto 10%" />).container.firstChild
3434
).toMatchSnapshot();
3535
});
36+
37+
it('accepts props spreading', () => {
38+
const renderBox = () => {
39+
const props = { backgroundColor: Colors.POSITIVE_GREEN_900 };
40+
return <Box {...props} />;
41+
};
42+
expect(renderBox).not.toThrow();
43+
});
3644
});

src/components/Box/Box.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled, { StyledComponentBase } from 'styled-components';
1+
import styled from 'styled-components';
22
import {
33
background,
44
BackgroundProps,
@@ -24,13 +24,13 @@ interface BoxProps
2424
extends SpaceProps,
2525
LayoutProps,
2626
PositionProps,
27-
ColorProps,
27+
Omit<ColorProps, 'color'>, // HACK: avoid collision of `color` prop
2828
FlexboxProps,
2929
GridProps,
3030
BackgroundProps,
3131
TextAlignProps {}
3232

33-
const Box: StyledComponentBase<'div', never, BoxProps, 'theme'> = styled.div.attrs({ theme })<BoxProps>`
33+
const Box = styled.div.attrs({ theme })<BoxProps>`
3434
${compose(space, layout, position, color, flexbox, grid, background, textAlign)}
3535
`;
3636

src/components/FilePicker/FilePicker.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ComponentPropsWithoutRef, FC, useRef, useState } from 'react';
1+
import React, { ComponentPropsWithoutRef, FC, MouseEventHandler, useRef, useState } from 'react';
22
import styled, { css } from 'styled-components';
33
import { MarginProps } from 'styled-system';
44
import { Colors, MediaQueries } from '../../essentials';
@@ -61,7 +61,6 @@ interface OutlinerProps extends BoxProps {
6161
disabled: boolean;
6262
error: boolean;
6363
hasValidFile: boolean;
64-
onClick: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
6564
}
6665

6766
// NOTE: we want to affect the color of only one icon SVG and not the ICON_FILE_FEEDBACK_COLOR
@@ -146,7 +145,7 @@ const FilePicker: FC<FilePickerProps> = ({
146145
onFileChange(eventFile, e);
147146
setFile(eventFile);
148147
};
149-
const onClickHandler = (e: React.MouseEvent<HTMLButtonElement>) => {
148+
const onClickHandler: MouseEventHandler = e => {
150149
// Avoid label trigger file selection twice
151150
e.preventDefault();
152151
// Avoid button trigger file selection twice

src/components/Search/Search.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import styled from 'styled-components';
55
import { Colors, Elevation } from '../../essentials';
66
import { CloseIcon, MagnifyingGlassIcon } from '../../icons/index';
77
import { useControlledState } from '../../utils/hooks/useControlledState';
8-
import { Box } from '../Box/Box';
8+
import { Box, BoxProps } from '../Box/Box';
99

1010
import { Input } from '../Input/Input';
1111

@@ -14,7 +14,9 @@ const ActiveStyle = `
1414
color: ${Colors.ACTION_BLUE_900};
1515
`;
1616

17-
const SearchResultsContainer = styled(Box)`
17+
interface SearchResultsContainerProps extends BoxProps, Pick<SearchProps, 'inverted'> {}
18+
19+
const SearchResultsContainer = styled(Box)<SearchResultsContainerProps>`
1820
position: absolute;
1921
z-index: ${Elevation.SUGGESTIONS_LIST};
2022
margin-top: 0.0625rem;
@@ -32,7 +34,11 @@ const ActiveBox = styled(Box)`
3234
}
3335
`;
3436

35-
const SearchInputContainer = styled(Box)`
37+
interface SearchInputContainerProps extends BoxProps {
38+
isInFocus: boolean;
39+
}
40+
41+
const SearchInputContainer = styled(Box)<SearchInputContainerProps>`
3642
box-sizing: border-box;
3743
background: white;
3844
border-radius: 0.25rem;

src/essentials/theme.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { DefaultTheme } from 'styled-components';
12
import { Colors, SemanticColors } from './Colors/Colors';
23
import { Spaces } from './Spaces/Spaces';
34
import { Breakpoints, MediaQueries } from './Breakpoints/Breakpoints';
45

5-
const theme = {
6+
const theme: DefaultTheme = {
67
breakpoints: Breakpoints,
78
colors: Colors,
89
// todo: rename semanticColors to colors in the next major release

src/styled.d.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'styled-components';
2+
import { BreakpointsArray, MediaQueries } from './essentials/Breakpoints/Breakpoints';
3+
import { Colors, SemanticColors } from './essentials/Colors/Colors';
4+
import { Spaces } from './essentials';
5+
6+
declare module 'styled-components' {
7+
export interface DefaultTheme {
8+
colors: typeof Colors;
9+
semanticColors: typeof SemanticColors;
10+
breakpoints: BreakpointsArray;
11+
fontSizes: string[];
12+
fontWeights: {
13+
normal: number;
14+
semibold: number;
15+
bold: number;
16+
};
17+
fonts: {
18+
normal: string;
19+
monospace: string;
20+
};
21+
mediaQueries: typeof MediaQueries;
22+
space: typeof Spaces;
23+
radii: string[];
24+
iconSizes: {
25+
small: number;
26+
medium: number;
27+
large: number;
28+
};
29+
shadows: {
30+
small: string;
31+
medium: string;
32+
large: string;
33+
};
34+
}
35+
}

0 commit comments

Comments
 (0)