Skip to content

Commit ebd9020

Browse files
authored
Merge pull request #686 from rofrischmann/theme-provider-merge-overwrite
Adding lost ability to auto-merge & overwrite themes
2 parents 43146fc + 8198b16 commit ebd9020

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

packages/fela-bindings/src/ThemeProviderFactory.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,20 @@ export default function ThemeProviderFactory(
55
BaseComponent: any,
66
ThemeContext: any,
77
createElement: Function,
8-
renderChildren: Function,
9-
statics?: Object
8+
renderChildren: Function
109
): any {
11-
class ThemeProvider extends BaseComponent {
12-
render(): Object {
13-
return createElement(
10+
return function ThemeProvider({ theme = {}, overwrite = false, children }) {
11+
return createElement(ThemeContext.Consumer, null, previousTheme =>
12+
createElement(
1413
ThemeContext.Provider,
1514
{
16-
value: this.props.theme,
15+
value:
16+
!overwrite && previousTheme
17+
? { ...previousTheme, ...theme }
18+
: theme,
1719
},
18-
renderChildren(this.props.children)
20+
renderChildren(children)
1921
)
20-
}
22+
)
2123
}
22-
23-
if (statics) {
24-
objectEach(statics, (value, key) => {
25-
ThemeProvider[key] = value
26-
})
27-
}
28-
29-
return ThemeProvider
3024
}

packages/fela-integration/src/jest-react-fela_react-fela/__tests__/ThemeProviderFactory-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,28 @@ describe('Using the ThemeProvider', () => {
1515

1616
expect(snapshot).toMatchSnapshot()
1717
})
18+
19+
it('should merge theme objects', () => {
20+
const snapshot = createSnapshot(
21+
<ThemeProvider theme={{ color: 'red' }}>
22+
<ThemeProvider theme={{ backgroundColor: 'blue' }}>
23+
<FelaTheme>{theme => <div>{JSON.stringify(theme)}</div>}</FelaTheme>
24+
</ThemeProvider>
25+
</ThemeProvider>
26+
)
27+
28+
expect(snapshot).toMatchSnapshot()
29+
})
30+
31+
it('should overwrite theme objects', () => {
32+
const snapshot = createSnapshot(
33+
<ThemeProvider theme={{ color: 'red' }}>
34+
<ThemeProvider overwrite theme={{ backgroundColor: 'blue' }}>
35+
<FelaTheme>{theme => <div>{JSON.stringify(theme)}</div>}</FelaTheme>
36+
</ThemeProvider>
37+
</ThemeProvider>
38+
)
39+
40+
expect(snapshot).toMatchSnapshot()
41+
})
1842
})

packages/fela-integration/src/jest-react-fela_react-fela/__tests__/__snapshots__/ThemeProviderFactory-test.js.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Using the ThemeProvider should merge theme objects 1`] = `
4+
"
5+
6+
<div>
7+
{{}color:red,backgroundColor:blue{}}
8+
</div>;
9+
"
10+
`;
11+
12+
exports[`Using the ThemeProvider should overwrite theme objects 1`] = `
13+
"
14+
15+
<div>
16+
{{}backgroundColor:blue{}}
17+
</div>;
18+
"
19+
`;
20+
321
exports[`Using the ThemeProvider should pass the theme to rule props 1`] = `
422
"
523

0 commit comments

Comments
 (0)