Skip to content
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
26 changes: 10 additions & 16 deletions packages/fela-bindings/src/ThemeProviderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@ export default function ThemeProviderFactory(
BaseComponent: any,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

baseComponent and the objectEach import don't seem to be needed an more

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Fair enough, I will remove it before pushing. This is kind of a hot fix as I need this functionality now :D

ThemeContext: any,
createElement: Function,
renderChildren: Function,
statics?: Object
renderChildren: Function
): any {
class ThemeProvider extends BaseComponent {
render(): Object {
return createElement(
return function ThemeProvider({ theme = {}, overwrite = false, children }) {
return createElement(ThemeContext.Consumer, null, previousTheme =>
createElement(
ThemeContext.Provider,
{
value: this.props.theme,
value:
!overwrite && previousTheme
? { ...previousTheme, ...theme }
: theme,
},
renderChildren(this.props.children)
renderChildren(children)
)
}
)
}

if (statics) {
objectEach(statics, (value, key) => {
ThemeProvider[key] = value
})
}

return ThemeProvider
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,28 @@ describe('Using the ThemeProvider', () => {

expect(snapshot).toMatchSnapshot()
})

it('should merge theme objects', () => {
const snapshot = createSnapshot(
<ThemeProvider theme={{ color: 'red' }}>
<ThemeProvider theme={{ backgroundColor: 'blue' }}>
<FelaTheme>{theme => <div>{JSON.stringify(theme)}</div>}</FelaTheme>
</ThemeProvider>
</ThemeProvider>
)

expect(snapshot).toMatchSnapshot()
})

it('should overwrite theme objects', () => {
const snapshot = createSnapshot(
<ThemeProvider theme={{ color: 'red' }}>
<ThemeProvider overwrite theme={{ backgroundColor: 'blue' }}>
<FelaTheme>{theme => <div>{JSON.stringify(theme)}</div>}</FelaTheme>
</ThemeProvider>
</ThemeProvider>
)

expect(snapshot).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Using the ThemeProvider should merge theme objects 1`] = `
"

<div>
{{}color:red,backgroundColor:blue{}}
</div>;
"
`;

exports[`Using the ThemeProvider should overwrite theme objects 1`] = `
"

<div>
{{}backgroundColor:blue{}}
</div>;
"
`;

exports[`Using the ThemeProvider should pass the theme to rule props 1`] = `
"

Expand Down