-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme): implementing ThemeProvider and withTheme
- Loading branch information
1 parent
a38af67
commit efaf224
Showing
5 changed files
with
66 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export const description = '@rk-kit/theme'; | ||
import getThemeProvider from './primitives/themeProvider'; | ||
import withTheme from './primitives/themeConsumer'; | ||
import ThemeProvider from './primitives/themeProvider'; | ||
import {withTheme, WithThemeProps} from './primitives/themeConsumer'; | ||
|
||
export {getThemeProvider, withTheme}; | ||
export {ThemeProvider, withTheme, WithThemeProps}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
import React from 'react'; | ||
import {Consumer} from './createContext'; | ||
|
||
function withTheme(Component) { | ||
return ( | ||
<Consumer> | ||
{theme => { | ||
return React.cloneElement(Component, {theme}); | ||
}} | ||
</Consumer> | ||
) | ||
export interface WithThemeProps { | ||
theme: Object; | ||
} | ||
|
||
export default withTheme; | ||
export function withTheme<T extends WithThemeProps>(Component: React.ComponentType<T>) { | ||
|
||
type TExcept = Exclude<keyof T, keyof WithThemeProps>; | ||
type ForwardedProps = Pick<T, TExcept>; | ||
|
||
return class WithTheme extends React.Component<ForwardedProps, {}> { | ||
|
||
render() { | ||
return ( | ||
<Consumer> | ||
{theme => <Component {...this.props} theme={theme}/>} | ||
</Consumer> | ||
); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import {View} from 'react-native'; | ||
import * as UITest from 'react-native-testing-library'; | ||
import {ThemeProvider, withTheme, WithThemeProps} from './index'; | ||
|
||
interface TestProps extends WithThemeProps { | ||
name: string; | ||
} | ||
|
||
class TestComponent extends React.Component<TestProps> { | ||
render() { | ||
const {theme, name} = this.props; | ||
|
||
return ( | ||
<View/> | ||
); | ||
} | ||
} | ||
|
||
it('Checks simple Provider/Consumer theme pass', async () => { | ||
const ThemedComponent = withTheme(TestComponent); | ||
|
||
const component = UITest.render( | ||
<ThemeProvider> | ||
<ThemedComponent name={'test'}/> | ||
</ThemeProvider>, | ||
); | ||
|
||
// TODO: finish test | ||
}); | ||
|