Skip to content

Commit d2612e9

Browse files
authored
Merge pull request #28 from wellguimaraes/master
Fixed issue for dynamic props update
2 parents 084d92a + 829864b commit d2612e9

6 files changed

Lines changed: 63 additions & 11 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"homepage": "https://github.com/cssinjs/styled-jss#readme",
4040
"dependencies": {
4141
"is-react-prop": "^0.1.3",
42-
"jss": "^7.1.5",
42+
"jss": "^7.1.7",
4343
"jss-preset-default": "^2.0.0"
4444
},
4545
"peerDependencies": {

src/createStyled.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const createStyled = (jss: Function) => (baseStyles: BaseStylesType = {}): Style
3434
return sheet
3535
}
3636

37-
return Object.assign((
37+
const styledWrapper = (
3838
tagNameOrStyledElement: TagNameOrStyledElementType
3939
) => (
4040
ownStyle: ComponentStyleType
@@ -43,7 +43,13 @@ const createStyled = (jss: Function) => (baseStyles: BaseStylesType = {}): Style
4343
const elementStyle = {...style, ...ownStyle}
4444

4545
return styled({tagName, baseStyles, elementStyle, mountSheet})
46-
}, {mountSheet, sheet, styles: baseStyles})
46+
}
47+
48+
Object.defineProperty(styledWrapper, 'sheet', ({
49+
get: () => sheet,
50+
}: Object)) // https://github.com/facebook/flow/issues/285
51+
52+
return Object.assign(styledWrapper, {mountSheet, styles: baseStyles})
4753
}
4854

4955
export default createStyled

src/styled.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ const styled = ({tagName, elementStyle, mountSheet}: StyledArgs) => {
7171
updateSheet(props: StyledElementPropsType) {
7272
let rule
7373
let ruleIndex = 0
74+
7475
// nested styles become to flatten rules, so we need to update each nested rule
7576
for (ruleIndex; ruleIndex < classMap[this.dynamicTagName].length; ruleIndex++) {
7677
rule = classMap[this.dynamicTagName][ruleIndex]
77-
this.sheet.update(rule.name, props)
78+
79+
if (rule.name) this.sheet.update(rule.name, props)
80+
if (rule.rules) rule.rules.update(props)
7881
}
7982
}
8083

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
exports[`functional tests should update dynamic props for conditional rules 1`] = `
2+
".button-1-id {
3+
padding: 10px;
4+
}
5+
@media screen {
6+
.button-1-id .button {
7+
margin: 10px;
8+
}
9+
}"
10+
`;
11+
12+
exports[`functional tests should update dynamic props for conditional rules 2`] = `
13+
".button-1-id {
14+
padding: 0;
15+
}
16+
@media screen {
17+
.button-1-id .button {
18+
margin: 0;
19+
}
20+
}"
21+
`;
22+
123
exports[`functional tests should update nested props 1`] = `
224
".button-id {
325
font-size: 12px;

src/tests/functional.spec.jsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ describe('functional tests', () => {
4040
})
4141

4242
afterEach(() => {
43-
styled.mountSheet().detach()
43+
styled.sheet.detach()
4444
})
4545

4646
it('should update props and unmount', () => {
4747
const App = CreateApp(styled)
4848
const wrapper = mount(<App />)
49-
const sheet = styled.mountSheet()
49+
const {sheet} = styled
5050

5151
assertSheet(sheet)
5252
wrapper.setProps({margin: 20})
@@ -86,7 +86,7 @@ describe('functional tests', () => {
8686
)
8787

8888
const wrapper = mount(<App />)
89-
const sheet = styled.mountSheet()
89+
const {sheet} = styled
9090

9191
assertSheet(sheet)
9292
wrapper.setProps({primary: true})
@@ -100,7 +100,7 @@ describe('functional tests', () => {
100100
})
101101

102102
const wrapper = mount(<Button />)
103-
const sheet = styled.mountSheet()
103+
const {sheet} = styled
104104

105105
assertSheet(sheet)
106106
wrapper
@@ -110,4 +110,25 @@ describe('functional tests', () => {
110110
assertSheet(sheet)
111111
wrapper.unmount()
112112
})
113+
114+
it('should update dynamic props for conditional rules', () => {
115+
const Button = styled('button')({
116+
padding: props => (props.spaced ? 10 : 0),
117+
118+
'@media screen': {
119+
'& .button': {
120+
margin: props => (props.spaced ? 10 : 0)
121+
}
122+
}
123+
})
124+
125+
const wrapper = mount(<Button spaced />)
126+
const {sheet} = styled
127+
128+
assertSheet(sheet)
129+
wrapper.setProps({spaced: false})
130+
assertSheet(sheet)
131+
132+
wrapper.unmount()
133+
})
113134
})

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,9 +2690,9 @@ jss-vendor-prefixer@^5.0.0:
26902690
dependencies:
26912691
css-vendor "^0.3.8"
26922692

2693-
jss@^7.1.5:
2694-
version "7.1.5"
2695-
resolved "https://registry.yarnpkg.com/jss/-/jss-7.1.5.tgz#7d27b07b0f2f7f1a2ec9fe0ee1e2ab3f9d903de5"
2693+
jss@^7.1.7:
2694+
version "7.1.7"
2695+
resolved "https://registry.yarnpkg.com/jss/-/jss-7.1.7.tgz#f2ace208247f1a68166dd20ccd69c866105fa404"
26962696
dependencies:
26972697
is-in-browser "1.0.2"
26982698
warning "3.0.0"

0 commit comments

Comments
 (0)