Skip to content

Commit 55ef071

Browse files
authored
Removed .defaultProps support (#3286)
* Removed `.defaultProps` support * fix tests * remove unused import
1 parent 9718d8c commit 55ef071

File tree

12 files changed

+49
-152
lines changed

12 files changed

+49
-152
lines changed

.changeset/early-badgers-teach.md

+6
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/__snapshots__/react-enzyme.test.js.snap

+10-29
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,13 @@ exports[`enzyme mount theming 1`] = `
250250
border: 2px solid red;
251251
}
252252
253+
.emotion-2 {
254+
color: blue;
255+
border: 2px solid blue;
256+
}
257+
253258
<div>
254-
<Button
255-
theme={
256-
{
257-
"main": "red",
258-
}
259-
}
260-
>
259+
<Button>
261260
<button
262261
className="emotion-0 emotion-1"
263262
>
@@ -271,15 +270,9 @@ exports[`enzyme mount theming 1`] = `
271270
}
272271
}
273272
>
274-
<Button
275-
theme={
276-
{
277-
"main": "red",
278-
}
279-
}
280-
>
273+
<Button>
281274
<button
282-
className="emotion-0 emotion-1"
275+
className="emotion-2 emotion-1"
283276
>
284277
Themed
285278
</button>
@@ -664,13 +657,7 @@ exports[`enzyme shallow styled with css prop 1`] = `
664657

665658
exports[`enzyme shallow theming 1`] = `
666659
<div>
667-
<Button
668-
theme={
669-
{
670-
"main": "red",
671-
}
672-
}
673-
>
660+
<Button>
674661
Normal
675662
</Button>
676663
<ThemeProvider
@@ -680,13 +667,7 @@ exports[`enzyme shallow theming 1`] = `
680667
}
681668
}
682669
>
683-
<Button
684-
theme={
685-
{
686-
"main": "red",
687-
}
688-
}
689-
>
670+
<Button>
690671
Themed
691672
</Button>
692673
</ThemeProvider>

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

+22-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,33 @@ import 'test-utils/enzyme-env'
33

44
import jestInCase from 'jest-in-case'
55
import * as enzyme from 'enzyme'
6-
import { css, jsx, ThemeProvider } from '@emotion/react'
6+
import {
7+
__unsafe_useEmotionCache,
8+
css,
9+
jsx,
10+
ThemeProvider,
11+
EmotionCache
12+
} from '@emotion/react'
713
import styled from '@emotion/styled'
814
import React from 'react'
915
import toJson from 'enzyme-to-json'
1016

1117
import { matchers } from '@emotion/jest'
1218
import * as serializer from '@emotion/jest/enzyme-serializer'
1319

20+
afterEach(() => {
21+
let cache
22+
function GetCache() {
23+
cache = __unsafe_useEmotionCache()
24+
return null
25+
}
26+
enzyme.shallow(<GetCache />)
27+
28+
cache.registered = {}
29+
cache.inserted = {}
30+
cache.sheet.flush()
31+
})
32+
1433
const isReact16 = React.version.split('.')[0] === '16'
1534

1635
expect.extend(matchers)
@@ -241,16 +260,10 @@ const cases = {
241260
theming: {
242261
render() {
243262
const Button = styled.button`
244-
color: ${props => props.theme.main};
245-
border: 2px solid ${props => props.theme.main};
263+
color: ${props => props.theme.main ?? 'red'};
264+
border: 2px solid ${props => props.theme.main ?? 'red'};
246265
`
247266

248-
Button.defaultProps = {
249-
theme: {
250-
main: 'red'
251-
}
252-
}
253-
254267
const theme = {
255268
main: 'blue'
256269
}

packages/react/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type {
3030
export { ThemeContext, useTheme, ThemeProvider, withTheme } from './theming'
3131
export type { Theme, ThemeProviderProps, WithTheme } from './theming'
3232
export { default as css } from './css'
33-
export type { DistributiveOmit, PropsOf } from './types'
33+
export type { DistributiveOmit } from './types'
3434

3535
declare const global: Record<string, unknown>
3636
declare const jest: unknown

packages/react/src/theming.tsx

+2-2
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

-10
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

-17
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

-1
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

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ComponentSelector, Interpolation } from '@emotion/serialize'
22
import { ReactJSXIntrinsicElements } from './jsx-namespace'
3-
import { PropsOf, Theme } from '@emotion/react'
3+
import { Theme } from '@emotion/react'
44

55
/** Same as StyledOptions but shouldForwardProp must be a type guard */
66
export interface FilteringStyledOptions<
@@ -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

+1-25
Original file line numberDiff line numberDiff line change
@@ -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."
@@ -384,7 +360,7 @@ exports[`styled withComponent will replace tags but keep styling classes 1`] = `
384360
My Title
385361
</h1>
386362
<h2
387-
className="emotion-2 emotion-3"
363+
className="emotion-2 emotion-19"
388364
>
389365
My Subtitle
390366
</h2>

packages/styled/test/index.test.js

-26
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

-24
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)