Skip to content

Commit ed0d7f1

Browse files
committed
Removed .defaultProps support
1 parent 9718d8c commit ed0d7f1

File tree

10 files changed

+17
-120
lines changed

10 files changed

+17
-120
lines changed

.changeset/early-badgers-teach.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@emotion/styled': major
3+
'@emotion/react': major
4+
---
5+
6+
Removed `.defaultProps` support

packages/jest/test/react-enzyme.test.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,10 @@ const cases = {
241241
theming: {
242242
render() {
243243
const Button = styled.button`
244-
color: ${props => props.theme.main};
245-
border: 2px solid ${props => props.theme.main};
244+
color: ${props => props.theme.main ?? 'red'};
245+
border: 2px solid ${props => props.theme.main ?? 'red'};
246246
`
247247

248-
Button.defaultProps = {
249-
theme: {
250-
main: 'red'
251-
}
252-
}
253-
254248
const theme = {
255249
main: 'blue'
256250
}

packages/react/src/theming.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react'
22
import weakMemoize from '@emotion/weak-memoize'
33
import isDevelopment from '#is-development'
44
import hoistNonReactStatics from './_isolated-hnrs'
5-
import { DistributiveOmit, PropsOf } from './types'
5+
import { DistributiveOmit } from './types'
66

77
// tslint:disable-next-line: no-empty-interface
88
export interface Theme {}
@@ -86,7 +86,7 @@ export function withTheme<
8686
>(
8787
Component: C
8888
): React.ForwardRefExoticComponent<
89-
DistributiveOmit<PropsOf<C>, 'theme'> & { theme?: Theme }
89+
DistributiveOmit<React.ComponentProps<C>, 'theme'> & { theme?: Theme }
9090
>
9191
export function withTheme(
9292
Component: React.ComponentType<any>

packages/react/src/types.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
import { ReactJSX } from './jsx-namespace'
2-
3-
/**
4-
* @desc Utility type for getting props type of React component.
5-
* It takes `defaultProps` into an account - making props with defaults optional.
6-
*/
7-
export type PropsOf<
8-
C extends keyof ReactJSX.IntrinsicElements | React.JSXElementConstructor<any>
9-
> = ReactJSX.LibraryManagedAttributes<C, React.ComponentProps<C>>
10-
111
// We need to use this version of Omit as it's distributive (Will preserve unions)
122
export type DistributiveOmit<T, U> = T extends any
133
? Pick<T, Exclude<keyof T, U>>

packages/react/types/tests-theming.tsx

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,10 @@ const ThemedComp = withTheme(CompC)
3838
;<ThemedComp prop />
3939
;<ThemedComp prop theme={theme} />
4040

41-
const CompFCWithDefault = ({ prop }: Props) => (prop ? <span /> : <div />)
42-
CompFCWithDefault.defaultProps = { prop: false }
43-
class CompCWithDefault extends React.Component<Props> {
44-
static defaultProps = { prop: false }
45-
render() {
46-
return this.props.prop ? <span /> : <div />
47-
}
48-
}
49-
5041
{
5142
const theme: Theme = useTheme()
5243
}
5344

54-
const ThemedFCWithDefault = withTheme(CompFCWithDefault)
55-
;<ThemedFCWithDefault />
56-
;<ThemedFCWithDefault theme={theme} />
57-
58-
const ThemedCompWithDefault = withTheme(CompCWithDefault)
59-
;<ThemedCompWithDefault />
60-
;<ThemedCompWithDefault theme={theme} />
61-
6245
{
6346
interface Book {
6447
kind: 'book'

packages/styled/src/base.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ const createStyled = (tag: ElementType, options?: StyledOptions) => {
196196
: baseTag.displayName || baseTag.name || 'Component'
197197
})`
198198

199-
Styled.defaultProps = tag.defaultProps
200199
Styled.__emotion_real = Styled
201200
Styled.__emotion_base = baseTag
202201
Styled.__emotion_styles = styles

packages/styled/src/types.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export interface StyledComponent<
3131
withComponent<C extends React.ComponentClass<React.ComponentProps<C>>>(
3232
component: C
3333
): StyledComponent<
34-
ComponentProps & PropsOf<C>,
34+
ComponentProps & React.ComponentProps<C>,
3535
{},
3636
{ ref?: React.Ref<InstanceType<C>> }
3737
>
3838
withComponent<C extends React.ComponentType<React.ComponentProps<C>>>(
3939
component: C
40-
): StyledComponent<ComponentProps & PropsOf<C>>
40+
): StyledComponent<ComponentProps & React.ComponentProps<C>>
4141
withComponent<Tag extends keyof ReactJSXIntrinsicElements>(
4242
tag: Tag
4343
): StyledComponent<ComponentProps, ReactJSXIntrinsicElements[Tag]>
@@ -113,7 +113,7 @@ export interface CreateStyled {
113113
component: C,
114114
options: FilteringStyledOptions<React.ComponentProps<C>, ForwardedProps>
115115
): CreateStyledComponent<
116-
Pick<PropsOf<C>, ForwardedProps> & {
116+
Pick<React.ComponentProps<C>, ForwardedProps> & {
117117
theme?: Theme
118118
},
119119
{},
@@ -126,7 +126,7 @@ export interface CreateStyled {
126126
component: C,
127127
options?: StyledOptions<React.ComponentProps<C>>
128128
): CreateStyledComponent<
129-
PropsOf<C> & {
129+
React.ComponentProps<C> & {
130130
theme?: Theme
131131
},
132132
{},
@@ -143,7 +143,7 @@ export interface CreateStyled {
143143
component: C,
144144
options: FilteringStyledOptions<React.ComponentProps<C>, ForwardedProps>
145145
): CreateStyledComponent<
146-
Pick<PropsOf<C>, ForwardedProps> & {
146+
Pick<React.ComponentProps<C>, ForwardedProps> & {
147147
theme?: Theme
148148
}
149149
>
@@ -152,7 +152,7 @@ export interface CreateStyled {
152152
component: C,
153153
options?: StyledOptions<React.ComponentProps<C>>
154154
): CreateStyledComponent<
155-
PropsOf<C> & {
155+
React.ComponentProps<C> & {
156156
theme?: Theme
157157
}
158158
>
@@ -182,7 +182,6 @@ export interface CreateStyled {
182182
}
183183

184184
export type ElementType = React.ElementType & {
185-
defaultProps?: Partial<any>
186185
__emotion_real?: ElementType
187186
__emotion_base?: ElementType
188187
__emotion_styles?: Interpolation<Theme>[]

packages/styled/test/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ exports[`styled nested 1`] = `
200200
<div
201201
className="emotion-0 emotion-1"
202202
>
203-
hello
203+
hello
204204
<h1
205205
className="emotion-2 emotion-3"
206206
>
@@ -313,30 +313,6 @@ exports[`styled ref 1`] = `
313313
</h1>
314314
`;
315315

316-
exports[`styled should forward .defaultProps when reusing __emotion_base 1`] = `
317-
.emotion-0 {
318-
text-align: center;
319-
color: red;
320-
}
321-
322-
.emotion-2 {
323-
text-align: center;
324-
color: red;
325-
font-style: italic;
326-
}
327-
328-
<div>
329-
<h1
330-
className="emotion-0 emotion-1"
331-
color="red"
332-
/>
333-
<h1
334-
className="emotion-2 emotion-3"
335-
color="red"
336-
/>
337-
</div>
338-
`;
339-
340316
exports[`styled throws if undefined is passed as the component 1`] = `
341317
"You are trying to create a styled element with an undefined component.
342318
You may have forgotten to import it."

packages/styled/test/index.test.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -326,32 +326,6 @@ describe('styled', () => {
326326
expect(tree).toMatchSnapshot()
327327
})
328328

329-
test('should forward .defaultProps when reusing __emotion_base', () => {
330-
const Title = styled('h1')`
331-
text-align: center;
332-
${props => ({
333-
color: props.color
334-
})};
335-
`
336-
337-
Title.defaultProps = {
338-
color: 'red'
339-
}
340-
341-
const Title2 = styled(Title)`
342-
font-style: italic;
343-
`
344-
345-
const tree = renderer
346-
.create(
347-
<div>
348-
<Title />
349-
<Title2 />
350-
</div>
351-
)
352-
.toJSON()
353-
expect(tree).toMatchSnapshot()
354-
})
355329
test('withComponent will replace tags but keep styling classes', () => {
356330
const Title = styled('h1')`
357331
color: green;

packages/styled/types/tests-base.tsx

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -328,30 +328,6 @@ declare const ref3_2: (element: HTMLDivElement | null) => void
328328
;<StyledReadable kind="magazine" author="Hejlsberg" /> // $ExpectError
329329
}
330330

331-
interface Props {
332-
prop: boolean
333-
}
334-
class ClassWithDefaultProps extends React.Component<Props> {
335-
static defaultProps = { prop: false }
336-
render() {
337-
return this.props.prop ? <Button0 /> : <Button1 />
338-
}
339-
}
340-
const StyledClassWithDefaultProps = styled(ClassWithDefaultProps)`
341-
background-color: red;
342-
`
343-
const classInstance = <StyledClassWithDefaultProps />
344-
345-
const FCWithDefaultProps = ({ prop }: Props) =>
346-
prop ? <Button0 /> : <Button1 />
347-
FCWithDefaultProps.defaultProps = {
348-
prop: false
349-
}
350-
const StyledFCWithDefaultProps = styled(FCWithDefaultProps)`
351-
background-color: red;
352-
`
353-
const fcInstance = <StyledFCWithDefaultProps />
354-
355331
interface PropsA {
356332
title: string
357333
}

0 commit comments

Comments
 (0)