Skip to content

Commit ed61dd5

Browse files
authored
Merge pull request #64 from cssinjs/feature/fix-function-values
Fix function values in composition
2 parents 58871ae + 33088dc commit ed61dd5

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ exports[`functional tests composable styles should merge and compose all styles
135135
padding: 20px;
136136
}
137137
.button-2-id {
138+
font-weight: 400;
138139
border-radius: 100%;
139140
color: red;
140141
background-color: green;
@@ -147,9 +148,11 @@ exports[`functional tests composable styles should merge and compose all styles
147148
padding: 20px;
148149
}
149150
.button-2-id {
151+
font-weight: 400;
150152
border-radius: 100%;
151153
color: black;
152154
background-color: white;
155+
font-size: 15px;
153156
}"
154157
`;
155158

src/tests/functional.spec.jsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ describe('functional tests', () => {
281281
const Button = styled('button')(
282282
(props => props.theme === 'action' && ({
283283
color: 'red',
284-
'background-color': 'green'
284+
backgroundColor: 'green'
285285
})),
286286
(props => props.theme === 'normal' && ({
287287
color: 'white',
288-
'background-color': 'black'
288+
backgroundColor: 'black'
289289
})),
290290
(() => ({
291291
margin: 20,
@@ -312,26 +312,27 @@ describe('functional tests', () => {
312312
const theme = props => ({
313313
action: {
314314
color: 'red',
315-
'background-color': 'green',
315+
backgroundColor: 'green',
316316
},
317317
normal: {
318318
color: 'black',
319-
'background-color': 'white',
319+
backgroundColor: 'white',
320320
},
321321
})[props.theme]
322322

323323
const Button = styled('button')({
324324
margin: 20,
325-
padding: 20
326-
}, round, theme)
325+
padding: 20,
326+
fontWeight: () => 400,
327+
}, round, theme, {fontSize: ({fontSize}) => fontSize})
327328

328329
const wrapper = mount(
329330
<Button theme="action" round />
330331
)
331332
const {sheet} = styled
332333

333334
assertSheet(sheet)
334-
wrapper.setProps({theme: 'normal', round: false})
335+
wrapper.setProps({theme: 'normal', fontSize: 15, round: false})
335336
assertSheet(sheet)
336337
wrapper.unmount()
337338
})

src/types/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export type JssStyle = Object
33
export type JssSheet = Object
44

55
export type BaseStylesType = JssStyles
6-
export type ComponentStyleType = JssStyle | (props: Object) => ?JssStyle
6+
export type ComponentStyleType = JssStyle
77
export type StyledType = Function & {
88
mountSheet: Function,
99
styles: JssStyles

src/utils/getSeparatedStyles.js

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,38 @@ import {type ComponentStyleType} from '../types'
55
const isObject = value =>
66
typeof value === 'object' && value !== null && !Array.isArray(value)
77

8-
type separatedStyles = {dynamicStyle?: ComponentStyleType, staticStyle?: Object}
8+
const separateStyles = (styles: Object): {
9+
dynamicStyle?: ComponentStyleType,
10+
staticStyle?: ComponentStyleType
11+
} => {
12+
const result = {}
13+
const keys = Object.keys(styles)
14+
15+
for (let i = 0; i < keys.length; i++) {
16+
const key = keys[i]
17+
const value = styles[key]
18+
const itemStyles = Object.create(null)
19+
20+
if (typeof value === 'function' || isObservable(value)) itemStyles.dynamicStyle = value
21+
else if (isObject(value)) Object.assign(itemStyles, separateStyles(value))
22+
else itemStyles.staticStyle = value
23+
24+
for (const styleType in itemStyles) {
25+
result[styleType] = result[styleType] || {}
26+
result[styleType][key] = itemStyles[styleType]
27+
}
28+
}
29+
30+
return result
31+
}
932

1033
/**
1134
* Extracts static and dynamic styles objects separately
1235
*/
13-
const getSeparatedStyles = (...initialStyles: ComponentStyleType[]): separatedStyles => {
14-
const result = {}
15-
36+
const getSeparatedStyles = (...initialStyles: ComponentStyleType[]): {
37+
dynamicStyle?: ComponentStyleType | (props: Object) => ?ComponentStyleType,
38+
staticStyle?: ComponentStyleType,
39+
} => {
1640
const styles = {}
1741
const fns = []
1842

@@ -27,34 +51,25 @@ const getSeparatedStyles = (...initialStyles: ComponentStyleType[]): separatedSt
2751
}
2852
}
2953

30-
const keys = Object.keys(styles)
31-
32-
for (let i = 0; i < keys.length; i++) {
33-
const key = keys[i]
34-
const value = styles[key]
35-
const itemStyles = Object.create(null)
36-
37-
if (typeof value === 'function' || isObservable(value)) itemStyles.dynamicStyle = value
38-
else if (isObject(value)) Object.assign(itemStyles, getSeparatedStyles(value))
39-
else itemStyles.staticStyle = value
40-
41-
for (const styleType in itemStyles) {
42-
result[styleType] = result[styleType] || {}
43-
result[styleType][key] = itemStyles[styleType]
44-
}
45-
}
54+
const result = separateStyles(styles)
4655

4756
if (fns.length) {
48-
const {dynamicStyle} = result
57+
const {dynamicStyle = {}} = result
4958

5059
result.dynamicStyle = (props) => {
5160
const fnObjects = []
61+
const dynamicResult = {}
5262

5363
for (let i = 0; i < fns.length; i++) {
5464
fnObjects.push(fns[i](props))
5565
}
5666

57-
return Object.assign({}, dynamicStyle, ...fnObjects)
67+
const keys = Object.keys(dynamicStyle)
68+
for (let i = 0; i < keys.length; i++) {
69+
dynamicResult[keys[i]] = dynamicStyle[keys[i]](props)
70+
}
71+
72+
return Object.assign(dynamicResult, ...fnObjects)
5873
}
5974
}
6075

0 commit comments

Comments
 (0)