Skip to content

Commit

Permalink
fix(react): wrapped component should have className prop (#1136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Nov 27, 2022
1 parent e642089 commit 922f20d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nice-cameras-move.md
@@ -0,0 +1,5 @@
---
'@linaria/react': patch
---

Do not allow to wrap components without props.
11 changes: 11 additions & 0 deletions packages/react/__dtslint__/styled.ts
Expand Up @@ -27,6 +27,17 @@ const StyledDiv = styled.div``;
// $ExpectType "extends"
isExtends<typeof StyledDiv, React.FC<React.DetailedHTMLProps<any, any>>>();

const A = (): React.ReactElement => React.createElement('div', null);
// $ExpectError
styled(A)``;

// foo is not a valid property of div
// $ExpectError
React.createElement(StyledDiv, { foo: 'foo' });

const ReStyledDiv = styled(StyledDiv)<{ foo: string }>``;
React.createElement(ReStyledDiv, { foo: 'foo' });

// component should have className property
// $ExpectError
styled(Fabric<{ a: string }>())``;
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/styled.ts
Expand Up @@ -105,6 +105,10 @@ interface IProps {
[props: string]: unknown;
}

// Components with props are not allowed
function styled(
componentWithStyle: () => any
): (error: 'The target component should have a className prop') => void;
// Property-based interpolation is allowed only if `style` property exists
function styled<
TProps extends Has<TMustHave, { style?: React.CSSProperties }>,
Expand Down Expand Up @@ -225,7 +229,7 @@ type StyledComponent<T> = StyledMeta &
type StaticPlaceholder = string | number | CSSProperties | StyledMeta;

type HtmlStyledTag<TName extends keyof JSX.IntrinsicElements> = <
TAdditionalProps = Record<string, unknown>
TAdditionalProps = Record<never, unknown>
>(
strings: TemplateStringsArray,
...exprs: Array<
Expand Down

0 comments on commit 922f20d

Please sign in to comment.