Skip to content

Commit efaf224

Browse files
committed
feat(theme): implementing ThemeProvider and withTheme
1 parent a38af67 commit efaf224

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

src/framework/theme/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const description = '@rk-kit/theme';
2-
import getThemeProvider from './primitives/themeProvider';
3-
import withTheme from './primitives/themeConsumer';
2+
import ThemeProvider from './primitives/themeProvider';
3+
import {withTheme, WithThemeProps} from './primitives/themeConsumer';
44

5-
export {getThemeProvider, withTheme};
5+
export {ThemeProvider, withTheme, WithThemeProps};

src/framework/theme/primitives/createContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import React from 'react';
22

33
const {Consumer, Provider} = React.createContext({});
44

5-
export {Consumer, Provider};
5+
export {Consumer, Provider};
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import React from 'react';
22
import {Consumer} from './createContext';
33

4-
function withTheme(Component) {
5-
return (
6-
<Consumer>
7-
{theme => {
8-
return React.cloneElement(Component, {theme});
9-
}}
10-
</Consumer>
11-
)
4+
export interface WithThemeProps {
5+
theme: Object;
126
}
137

14-
export default withTheme;
8+
export function withTheme<T extends WithThemeProps>(Component: React.ComponentType<T>) {
9+
10+
type TExcept = Exclude<keyof T, keyof WithThemeProps>;
11+
type ForwardedProps = Pick<T, TExcept>;
12+
13+
return class WithTheme extends React.Component<ForwardedProps, {}> {
14+
15+
render() {
16+
return (
17+
<Consumer>
18+
{theme => <Component {...this.props} theme={theme}/>}
19+
</Consumer>
20+
);
21+
}
22+
};
23+
}

src/framework/theme/primitives/themeProvider.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@ import React from 'react';
22
import {Provider} from './createContext';
33

44
interface PropsType {
5-
children: JSX.Element,
6-
theme: Object
5+
children: JSX.Element;
6+
theme: Object;
77
}
88

9-
function getThemeProvider() {
10-
return class ThemeProvider extends React.PureComponent<PropsType> {
9+
class ThemeProvider extends React.PureComponent<PropsType> {
1110

12-
static defaultProps = {
13-
theme: {}
14-
};
11+
static defaultProps = {
12+
theme: {},
13+
};
1514

16-
render() {
17-
return (
18-
<Provider value={this.props.theme}>
19-
{this.props.children}
20-
</Provider>
21-
)
22-
}
15+
render() {
16+
return (
17+
<Provider value={this.props.theme}>
18+
{this.props.children}
19+
</Provider>
20+
);
2321
}
2422
}
2523

26-
export default getThemeProvider;
24+
export default ThemeProvider;

src/framework/theme/theme.spec.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import {View} from 'react-native';
3+
import * as UITest from 'react-native-testing-library';
4+
import {ThemeProvider, withTheme, WithThemeProps} from './index';
5+
6+
interface TestProps extends WithThemeProps {
7+
name: string;
8+
}
9+
10+
class TestComponent extends React.Component<TestProps> {
11+
render() {
12+
const {theme, name} = this.props;
13+
14+
return (
15+
<View/>
16+
);
17+
}
18+
}
19+
20+
it('Checks simple Provider/Consumer theme pass', async () => {
21+
const ThemedComponent = withTheme(TestComponent);
22+
23+
const component = UITest.render(
24+
<ThemeProvider>
25+
<ThemedComponent name={'test'}/>
26+
</ThemeProvider>,
27+
);
28+
29+
// TODO: finish test
30+
});
31+

0 commit comments

Comments
 (0)