mirror of
https://github.com/zhigang1992/react-native-paper.git
synced 2026-06-17 23:15:47 +08:00
Add HOC for theme
This commit is contained in:
1
index.js
1
index.js
@@ -1,6 +1,7 @@
|
||||
/* @flow */
|
||||
|
||||
export { default as ThemeProvider } from './src/core/ThemeProvider';
|
||||
export { default as withTheme } from './src/core/withTheme';
|
||||
|
||||
export * as Colors from './src/styles/colors.js';
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ import {
|
||||
} from 'react';
|
||||
import DefaultTheme from '../styles/DefaultTheme';
|
||||
|
||||
export const theme = 'react-native-paper$theme';
|
||||
|
||||
export default class ThemeProvider extends Component {
|
||||
|
||||
static propTypes = {
|
||||
@@ -15,12 +17,12 @@ export default class ThemeProvider extends Component {
|
||||
};
|
||||
|
||||
static childContextTypes = {
|
||||
theme: PropTypes.object,
|
||||
[theme]: PropTypes.object,
|
||||
};
|
||||
|
||||
getChildContext() {
|
||||
return {
|
||||
theme: this.props.theme || DefaultTheme,
|
||||
[theme]: this.props.theme || DefaultTheme,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
25
src/core/withTheme.js
Normal file
25
src/core/withTheme.js
Normal file
@@ -0,0 +1,25 @@
|
||||
/* @flow */
|
||||
|
||||
import React, {
|
||||
Component,
|
||||
PropTypes,
|
||||
} from 'react';
|
||||
import { theme } from './ThemeProvider';
|
||||
|
||||
export default function withTheme<T>(Comp: ReactClass<T>): ReactClass<T> {
|
||||
|
||||
const name = Comp.displayName || Comp.name;
|
||||
|
||||
return class extends Component {
|
||||
|
||||
static displayName = `Themed${name}`;
|
||||
|
||||
static contextTypes = {
|
||||
[theme]: PropTypes.object,
|
||||
};
|
||||
|
||||
render() {
|
||||
return <Comp theme={this.context[theme]} {...this.props} />;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -7,7 +7,9 @@ import {
|
||||
} from './colors';
|
||||
|
||||
export default {
|
||||
primaryColor: indigo500,
|
||||
primaryDarkColor: indigo700,
|
||||
accentColor: pinkA200,
|
||||
colors: {
|
||||
primary: indigo500,
|
||||
primaryDark: indigo700,
|
||||
accent: pinkA200,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user