Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support shouldForwardProp in styled's options #616

Merged
merged 3 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions packages/create-emotion-styled/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import type { ElementType } from 'react'
import typeof ReactType from 'react'
import type { CreateStyled, StyledOptions } from './utils'
import {
testOmitPropsOnComponent,
testPickPropsOnComponent,
testAlwaysTrue,
testOmitPropsOnStringTag,
omitAssign,
testPickPropsOnStringTag,
pickAssign,
setTheme
} from './utils'

Expand All @@ -25,22 +25,26 @@ function createEmotionStyled(emotion: Emotion, view: ReactType) {
let staticClassName
let identifierName
let stableClassName
let shouldForwardProp
if (options !== undefined) {
staticClassName = options.e
identifierName = options.label
stableClassName = options.target
shouldForwardProp = options.shouldForwardProp
}
const isReal = tag.__emotion_real === tag
const baseTag =
staticClassName === undefined
? (isReal && tag.__emotion_base) || tag
: tag

const omitFn =
typeof baseTag === 'string' &&
baseTag.charAt(0) === baseTag.charAt(0).toLowerCase()
? testOmitPropsOnStringTag
: testOmitPropsOnComponent
if (typeof shouldForwardProp !== 'function') {
shouldForwardProp =
typeof baseTag === 'string' &&
baseTag.charAt(0) === baseTag.charAt(0).toLowerCase()
? testPickPropsOnStringTag
: testPickPropsOnComponent
}

return function() {
let args = arguments
Expand Down Expand Up @@ -86,7 +90,7 @@ function createEmotionStyled(emotion: Emotion, view: ReactType) {
}
render() {
const { props, state } = this
this.mergedProps = omitAssign(testAlwaysTrue, {}, props, {
this.mergedProps = pickAssign(testAlwaysTrue, {}, props, {
theme: (state !== null && state.theme) || props.theme || {}
})

Expand Down Expand Up @@ -118,7 +122,11 @@ function createEmotionStyled(emotion: Emotion, view: ReactType) {

return view.createElement(
baseTag,
omitAssign(omitFn, {}, props, { className, ref: props.innerRef })
// $FlowFixMe
pickAssign(shouldForwardProp, {}, props, {
className,
ref: props.innerRef
})
)
}
}
Expand All @@ -144,6 +152,7 @@ function createEmotionStyled(emotion: Emotion, view: ReactType) {
) {
return 'NO_COMPONENT_SELECTOR'
}
// $FlowFixMe
return `.${stableClassName}`
}
})
Expand All @@ -156,7 +165,7 @@ function createEmotionStyled(emotion: Emotion, view: ReactType) {
nextTag,
nextOptions !== undefined
? // $FlowFixMe
omitAssign(testAlwaysTrue, {}, options, nextOptions)
pickAssign(testAlwaysTrue, {}, options, nextOptions)
: options
)(...styles)
}
Expand Down
13 changes: 9 additions & 4 deletions packages/create-emotion-styled/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export function setTheme(theme: Object) {
this.setState({ theme })
}

export const testOmitPropsOnStringTag: (key: string) => boolean = memoize(
export const testPickPropsOnStringTag: (key: string) => boolean = memoize(
isPropValid
)
export const testOmitPropsOnComponent = (key: string) =>
export const testPickPropsOnComponent = (key: string) =>
key !== 'theme' && key !== 'innerRef'
export const testAlwaysTrue = () => true

export const omitAssign: (
export const pickAssign: (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imho omit was misleading here, and pick is better suited because pickAssign actually picks properties if predicate (testFn) returns truthy, omit would pick them if predicate would return falsy

testFn: (key: string) => boolean,
target: {},
...sources: Array<{}>
Expand All @@ -33,7 +33,12 @@ export const omitAssign: (
return target
}

export type StyledOptions = { e: string, label: string, target: string }
export type StyledOptions = {
e?: string,
label?: string,
target?: string,
shouldForwardProp?: (?string) => boolean
}

type CreateStyledComponent = (...args: Interpolations) => *

Expand Down
25 changes: 25 additions & 0 deletions packages/react-emotion/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,31 @@ exports[`styled composition with objects 1`] = `
</h1>
`;

exports[`styled custom shouldForwardProp works 1`] = `
.emotion-0,
.emotion-0 * {
fill: #0000ff;
}

<svg
className="emotion-0 emotion-1"
height="100px"
width="100px"
>
<rect
height="100"
style={
Object {
"stroke": "#ff0000",
}
}
width="100"
x="10"
y="10"
/>
</svg>
`;

exports[`styled function in expression 1`] = `
.emotion-0 {
font-size: 20px;
Expand Down
28 changes: 28 additions & 0 deletions packages/react-emotion/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,4 +1120,32 @@ describe('styled', () => {
const tree = renderer.create(<OneMoreComponent />).toJSON()
expect(tree).toMatchSnapshot()
})
test('custom shouldForwardProp works', () => {
const Svg = props => (
<svg {...props}>
<rect
x="10"
y="10"
height="100"
width="100"
style={{ stroke: '#ff0000' }}
/>
</svg>
)

const StyledSvg = styled(Svg, {
shouldForwardProp: prop =>
['className', 'width', 'height'].indexOf(prop) !== -1
})`
&,
& * {
fill: ${({ color }) => color};
}
`

const tree = renderer
.create(<StyledSvg color="#0000ff" width="100px" height="100px" />)
.toJSON()
expect(tree).toMatchSnapshot()
})
})