File tree Expand file tree Collapse file tree 5 files changed +66
-28
lines changed Expand file tree Collapse file tree 5 files changed +66
-28
lines changed Original file line number Diff line number Diff line change 1
1
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' ;
4
4
5
- export { getThemeProvider , withTheme } ;
5
+ export { ThemeProvider , withTheme , WithThemeProps } ;
Original file line number Diff line number Diff line change @@ -2,4 +2,4 @@ import React from 'react';
2
2
3
3
const { Consumer, Provider} = React . createContext ( { } ) ;
4
4
5
- export { Consumer , Provider } ;
5
+ export { Consumer , Provider } ;
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import { Consumer } from './createContext' ;
3
3
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 ;
12
6
}
13
7
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
+ }
Original file line number Diff line number Diff line change @@ -2,25 +2,23 @@ import React from 'react';
2
2
import { Provider } from './createContext' ;
3
3
4
4
interface PropsType {
5
- children : JSX . Element ,
6
- theme : Object
5
+ children : JSX . Element ;
6
+ theme : Object ;
7
7
}
8
8
9
- function getThemeProvider ( ) {
10
- return class ThemeProvider extends React . PureComponent < PropsType > {
9
+ class ThemeProvider extends React . PureComponent < PropsType > {
11
10
12
- static defaultProps = {
13
- theme : { }
14
- } ;
11
+ static defaultProps = {
12
+ theme : { } ,
13
+ } ;
15
14
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
+ ) ;
23
21
}
24
22
}
25
23
26
- export default getThemeProvider ;
24
+ export default ThemeProvider ;
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments