Skip to content

Commit

Permalink
fix: ensure typing for static properties defined on third-party compo…
Browse files Browse the repository at this point in the history
…nents wrapped by styled-components (#4141)

* fix access to static attributes for components structure used in some UI library

* test: add type test as well

---------

Co-authored-by: Martin PELCAT <[email protected]>
Co-authored-by: Evan Jacobs <[email protected]>
  • Loading branch information
3 people authored Sep 11, 2023
1 parent 01a8d91 commit 6ee6c23
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface Styled<
<Props extends object = BaseObject, Statics extends object = BaseObject>(
initialStyles: Styles<Substitute<OuterProps, NoInfer<Props>>>,
...interpolations: Interpolation<Substitute<OuterProps, NoInfer<Props>>>[]
): IStyledComponent<R, Substitute<OuterProps, Props>> & OuterStatics & Statics;
): IStyledComponent<R, Substitute<OuterProps, Props>> & OuterStatics & Statics & Target;

attrs: <
Props extends object = BaseObject,
Expand Down
10 changes: 0 additions & 10 deletions packages/styled-components/src/constructors/test/styled.test.ts

This file was deleted.

49 changes: 49 additions & 0 deletions packages/styled-components/src/constructors/test/styled.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { ComponentProps } from 'react';
import TestRenderer from 'react-test-renderer';
import { getRenderedCSS, resetStyled } from '../../test/utils';
import domElements from '../../utils/domElements';

let styled: ReturnType<typeof resetStyled>;

describe('styled', () => {
beforeEach(() => {
styled = resetStyled();
});

it('should have all valid HTML5 elements defined as properties', () => {
domElements.forEach(domElement => {
expect(styled[domElement]).toBeTruthy();
});
});

it('should expose the component static attribute like components', () => {
const CollapseComponent = (props: ComponentProps<'div'>) => {
return <div {...props} />;
};

const Panel = (props: ComponentProps<'div'>) => {
return <div {...props} />;
};

const Collapse = Object.assign(CollapseComponent, { Panel, PI: '3.14' });

const StyledCollapse = styled(Collapse)`
background: red;
`;

expect(Collapse).toBeTruthy();
expect(Collapse.Panel).toBeTruthy();
expect(Collapse.PI).toBe('3.14');

expect(StyledCollapse).toBeTruthy();
expect(StyledCollapse.Panel).toBeTruthy();
expect(StyledCollapse.PI).toBe('3.14');

TestRenderer.create(<StyledCollapse />);
expect(getRenderedCSS()).toMatchInlineSnapshot(`
".b {
background: red;
}"
`);
});
});
6 changes: 6 additions & 0 deletions packages/styled-components/src/test/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,9 @@ styled.div<PropsWithVariant>`
color: 'blue',
})};
`;

const TargetWithStaticProperties = (p: React.PropsWithChildren<{}>) => <div {...p} />;
TargetWithStaticProperties.foo = 'bar';

const StyledTargetWithStaticProperties = styled(TargetWithStaticProperties)``;
StyledTargetWithStaticProperties.foo;

0 comments on commit 6ee6c23

Please sign in to comment.