Skip to content

Commit e5ef93f

Browse files
author
Brent Ertz
committed
Compose shouldForwardProp in composed styled components
1 parent b98c40f commit e5ef93f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/create-emotion-styled/src/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ function createEmotionStyled(emotion: Emotion, view: ReactType) {
2929
staticClassName = options.e
3030
identifierName = options.label
3131
stableClassName = options.target
32-
shouldForwardProp = options.shouldForwardProp
32+
shouldForwardProp =
33+
tag.__emotion_forwardProp && options.shouldForwardProp
34+
? propName =>
35+
tag.__emotion_forwardProp(propName) &&
36+
// $FlowFixMe
37+
options.shouldForwardProp(propName)
38+
: options.shouldForwardProp
3339
}
3440
const isReal = tag.__emotion_real === tag
3541
const baseTag =
@@ -75,6 +81,7 @@ function createEmotionStyled(emotion: Emotion, view: ReactType) {
7581
static __emotion_styles: Interpolations
7682
static __emotion_base: Styled
7783
static __emotion_target: string
84+
static __emotion_forwardProp: void | (string => boolean)
7885
static withComponent: (ElementType, options?: StyledOptions) => any
7986

8087
componentWillMount() {
@@ -148,6 +155,7 @@ function createEmotionStyled(emotion: Emotion, view: ReactType) {
148155
Styled.__emotion_styles = styles
149156
Styled.__emotion_base = baseTag
150157
Styled.__emotion_real = Styled
158+
Styled.__emotion_forwardProp = shouldForwardProp
151159
Object.defineProperty(Styled, 'toString', {
152160
enumerable: false,
153161
value() {

packages/react-emotion/test/index.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -1176,3 +1176,18 @@ describe('styled', () => {
11761176
expect(tree).toMatchSnapshot()
11771177
})
11781178
})
1179+
1180+
test('composes shouldForwardProp on composed styled components', () => {
1181+
const StyledDiv = styled('div', {
1182+
shouldForwardProp: prop => prop === 'forwardMe'
1183+
})()
1184+
1185+
const ComposedDiv = styled(StyledDiv, {
1186+
shouldForwardProp: () => true
1187+
})()
1188+
1189+
const tree = renderer.create(<ComposedDiv filterMe forwardMe />).toJSON()
1190+
1191+
expect(tree.props.filterMe).toBeUndefined()
1192+
expect(tree.props.forwardMe).toBeDefined()
1193+
})

0 commit comments

Comments
 (0)