Skip to content

Commit 50ff937

Browse files
authored
Merge pull request #48 from cssinjs/feature/pass-props-to-components
Pass props to composed React Components
2 parents 2318cc2 + e850c4a commit 50ff937

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

src/styled.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ const styled = ({element, ownStyle, mountSheet, jss}: StyledArgs) => {
3838
const {
3939
style,
4040
tagName,
41-
reactComponent = tagName
41+
reactComponent,
4242
}: {
4343
style?: ComponentStyleType,
4444
tagName: string,
45-
reactComponent?: string | typeof element
45+
reactComponent?: typeof element
4646
} = getParamsByElement(element)
4747

4848
const elementStyle = {...style, ...ownStyle}
@@ -114,15 +114,15 @@ const styled = ({element, ownStyle, mountSheet, jss}: StyledArgs) => {
114114
render() {
115115
const {children, className, ...attrs} = this.props
116116

117-
const props = filterProps(tagName, attrs)
117+
const props = reactComponent ? attrs : filterProps(tagName, attrs)
118118
const tagClass = composeClasses([
119119
this.staticClassName,
120120
staticTagName && this.sheet.classes[staticTagName],
121121
this.dynamicTagName && this.sheet.classes[this.dynamicTagName],
122122
className
123123
])
124124

125-
return createElement(reactComponent, {...props, className: tagClass}, children)
125+
return createElement(reactComponent || tagName, {...props, className: tagClass}, children)
126126
}
127127
}
128128

src/tests/__snapshots__/functional.spec.jsx.snap

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`functional tests Compose React Components should pass props 1`] = `
4+
<Component>
5+
<StyledElement
6+
className="testClassName"
7+
testProp={1}
8+
testProp2="2"
9+
>
10+
<Component
11+
className="StyledElement-1-id testClassName"
12+
testProp={1}
13+
testProp2="2"
14+
>
15+
{"testProp":1,"testProp2":"2","className":"StyledElement-1-id testClassName"}
16+
</Component>
17+
</StyledElement>
18+
</Component>
19+
`;
20+
21+
exports[`functional tests Compose React Components should pass props 2`] = `
22+
".StyledElement-1-id {
23+
padding: 10px;
24+
}"
25+
`;
26+
327
exports[`functional tests Compose React Components should use .displayName 1`] = `
428
<StyledElement>
529
<TestDisplayName
@@ -56,7 +80,7 @@ exports[`functional tests Compose React Components should use .name 3`] = `
5680

5781
exports[`functional tests Compose React Components should use .name 4`] = `
5882
".StyledElement-1-id {
59-
padding: 30px;
83+
padding: 10px;
6084
}"
6185
`;
6286

src/tests/functional.spec.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,18 @@ describe('functional tests', () => {
189189

190190
it('should use .name', () => {
191191
const StyledTest = styled(props => <h1 {...props}>test</h1>)({
192-
padding: 30,
192+
padding: 10,
193193
})
194194
assertComponent(StyledTest)
195195
assertSheet(styled.sheet)
196196
})
197+
198+
it('should pass props', () => {
199+
const StyledTest = styled(props => JSON.stringify(props))({
200+
padding: 10,
201+
})
202+
assertComponent(() => <StyledTest testProp={1} testProp2="2" className="testClassName" />)
203+
assertSheet(styled.sheet)
204+
})
197205
})
198206
})

0 commit comments

Comments
 (0)