forked from baidu/amis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.tsx
188 lines (158 loc) · 4.27 KB
/
theme.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 主题管理
import cx from 'classnames';
import React from 'react';
import hoistNonReactStatic from 'hoist-non-react-statics';
export type ClassValue =
| string
| number
| ClassDictionary
| ClassArray
| undefined
| null
| boolean;
interface ClassDictionary {
[id: string]: any;
}
interface ClassArray extends Array<ClassValue> {}
export type ClassNamesFn = (...classes: ClassValue[]) => string;
interface ThemeConfig {
classPrefix?: string;
renderers?: {
[propName: string]: any;
};
components?: {
[propName: string]: any;
};
[propsName: string]: any;
}
const themes: {
[propName: string]: ThemeConfig;
} = {
default: {}
};
export function theme(name: string, config: Partial<ThemeConfig>) {
themes[name] = {
...config
};
}
const fns: {
[propName: string]: (...classes: ClassValue[]) => string;
} = {};
export function makeClassnames(ns?: string) {
if (ns && fns[ns]) {
return fns[ns];
}
const fn = (...classes: ClassValue[]) => {
const str = cx(...(classes as any));
return str && ns
? str
.replace(/(^|\s)([A-Z])/g, '$1' + ns + '$2')
.replace(/(^|\s)\:/g, '$1')
: str || '';
};
ns && (fns[ns] = fn);
return fn;
}
export type ThemeInstance = ThemeConfig & {
getRendererConfig: (name?: string) => any;
getComponentConfig: (name?: string) => any;
classnames: ClassNamesFn;
};
export function hasTheme(theme: string): boolean {
return !!themes[theme];
}
export function setDefaultTheme(theme: string) {
if (hasTheme(theme)) {
defaultTheme = theme;
}
}
export function classnames(...classes: ClassValue[]) {
return getTheme(defaultTheme).classnames(...classes);
}
export function getClassPrefix() {
return getTheme(defaultTheme).classPrefix;
}
export function getTheme(theme: string): ThemeInstance {
if (!themes[theme]) {
throw new Error(`Theme with name "${theme}" does not exist!`);
}
const config = themes[theme];
if (!config.getRendererConfig) {
config.getRendererConfig = (name?: string) =>
config.renderers && name ? config.renderers[name] : null;
}
if (!config.classnames) {
const ns = config.classPrefix;
config.classnames = config.classnames || makeClassnames(ns);
}
if (!config.getComponentConfig) {
config.getComponentConfig = (name?: string) =>
config.components && name ? config.components[name] : null;
}
return config as ThemeInstance;
}
export interface ThemeProps {
className?: string;
classPrefix: string;
classnames: ClassNamesFn;
theme?: string;
}
export interface ThemeOutterProps {
theme?: string;
className?: string;
classPrefix?: string;
classnames?: ClassNamesFn;
}
export let defaultTheme: string = 'default';
export const ThemeContext = React.createContext('');
export function themeable<
T extends React.ComponentType<React.ComponentProps<T> & ThemeProps> & {
themeKey?: string;
}
>(ComposedComponent: T) {
type OuterProps = JSX.LibraryManagedAttributes<
T,
Omit<React.ComponentProps<T>, keyof ThemeProps>
> &
ThemeOutterProps;
const result = hoistNonReactStatic(
class extends React.Component<OuterProps> {
static displayName = `Themeable(${
ComposedComponent.displayName || ComposedComponent.name
})`;
static contextType = ThemeContext;
static ComposedComponent = ComposedComponent;
render() {
const theme: string = this.props.theme || this.context || defaultTheme;
const config = hasTheme(theme)
? getTheme(theme)
: getTheme(defaultTheme);
const injectedProps: {
classPrefix: string;
classnames: ClassNamesFn;
theme: string;
} = {
classPrefix: config.classPrefix as string,
classnames: config.classnames,
theme
};
return (
<ThemeContext.Provider value={theme}>
<ComposedComponent
{...config.getComponentConfig(ComposedComponent.themeKey)}
{...(this.props as JSX.LibraryManagedAttributes<
T,
React.ComponentProps<T>
>)}
{...injectedProps}
/>
</ThemeContext.Provider>
);
}
},
ComposedComponent
);
return result as typeof result & {
ComposedComponent: T;
};
}