forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.jsx
234 lines (199 loc) · 7.5 KB
/
config.jsx
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import React from 'react';
import PropTypes from 'prop-types';
import hoistNonReactStatic from 'hoist-non-react-statics';
import { obj, log } from '../util';
import getContextProps from './get-context-props';
import ErrorBoundary from './error-boundary';
const { shallowEqual } = obj;
function getDisplayName(Component) {
return Component.displayName || Component.name || 'Component';
}
let globalLocales;
let currentGlobalLanguage = 'zh-cn';
let currentGlobalLocale = {};
let currentGlobalRtl;
export function initLocales(locales) {
globalLocales = locales;
if (locales) {
currentGlobalLocale = locales[currentGlobalLanguage];
if (typeof currentGlobalRtl !== 'boolean') {
currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
}
}
}
export function setLanguage(language) {
if (globalLocales) {
currentGlobalLanguage = language;
currentGlobalLocale = globalLocales[language];
if (typeof currentGlobalRtl !== 'boolean') {
currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
}
}
}
export function setLocale(locale) {
currentGlobalLocale = {
...(globalLocales ? globalLocales[currentGlobalLanguage] : {}),
...locale,
};
if (typeof currentGlobalRtl !== 'boolean') {
currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
}
}
export function setDirection(dir) {
currentGlobalRtl = dir === 'rtl';
}
export function getLocale() {
return currentGlobalLocale;
}
export function getLanguage() {
return currentGlobalLanguage;
}
export function getDirection() {
return currentGlobalRtl;
}
export function config(Component, options = {}) {
// 非 forwardRef 创建的 class component
if (obj.isClassComponent(Component) && Component.prototype.shouldComponentUpdate === undefined) {
// class component: 通过定义 shouldComponentUpdate 改写成 pure component, 有refs
Component.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
if (this.props.pure) {
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
}
return true;
};
}
class ConfigedComponent extends React.Component {
static propTypes = {
...(Component.propTypes || {}),
prefix: PropTypes.string,
locale: PropTypes.object,
defaultPropsConfig: PropTypes.object,
pure: PropTypes.bool,
rtl: PropTypes.bool,
device: PropTypes.oneOf(['tablet', 'desktop', 'phone']),
popupContainer: PropTypes.any,
errorBoundary: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
};
static contextTypes = {
...(Component.contextTypes || {}),
nextPrefix: PropTypes.string,
nextLocale: PropTypes.object,
nextDefaultPropsConfig: PropTypes.object,
nextPure: PropTypes.bool,
nextRtl: PropTypes.bool,
nextWarning: PropTypes.bool,
nextDevice: PropTypes.oneOf(['tablet', 'desktop', 'phone']),
nextPopupContainer: PropTypes.any,
nextErrorBoundary: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
};
constructor(props, context) {
super(props, context);
this._getInstance = this._getInstance.bind(this);
this._deprecated = this._deprecated.bind(this);
}
_getInstance(ref) {
this._instance = ref;
if (this._instance && options.exportNames) {
options.exportNames.forEach(name => {
const field = this._instance[name];
if (typeof field === 'function') {
this[name] = field.bind(this._instance);
} else {
this[name] = field;
}
});
}
}
_deprecated(...args) {
if (this.context.nextWarning !== false) {
log.deprecated(...args);
}
}
getInstance() {
return this._instance;
}
render() {
const {
prefix,
locale,
defaultPropsConfig,
pure,
rtl,
device,
popupContainer,
errorBoundary,
...others
} = this.props;
const {
nextPrefix,
nextLocale = {},
nextDefaultPropsConfig = {},
nextPure,
nextRtl,
nextDevice,
nextPopupContainer,
nextErrorBoundary,
} = this.context;
const displayName = options.componentName || getDisplayName(Component);
const contextProps = getContextProps(
{
prefix,
locale,
defaultPropsConfig,
pure,
device,
popupContainer,
rtl,
errorBoundary,
},
{
nextPrefix,
nextLocale: { ...currentGlobalLocale, ...nextLocale },
nextDefaultPropsConfig,
nextPure,
nextDevice,
nextPopupContainer,
nextRtl: typeof nextRtl === 'boolean' ? nextRtl : currentGlobalRtl === true ? true : undefined,
nextErrorBoundary,
},
displayName
);
// errorBoundary is only for <ErrorBoundary>
const newContextProps = ['prefix', 'locale', 'pure', 'rtl', 'device', 'popupContainer'].reduce(
(ret, name) => {
if (typeof contextProps[name] !== 'undefined') {
ret[name] = contextProps[name];
}
return ret;
},
{}
);
if ('pure' in newContextProps && newContextProps.pure) {
log.warning('pure of ConfigProvider is deprecated, use Function Component or React.PureComponent');
}
// 对于两个真正消费 popupContainer 的组件来说,正确的名字是 container,
if (
'popupContainer' in newContextProps &&
this.props.container === undefined &&
['Overlay', 'Popup'].indexOf(displayName) > -1
) {
newContextProps.container = newContextProps.popupContainer;
delete newContextProps.popupContainer;
}
const newOthers = options.transform ? options.transform(others, this._deprecated) : others;
const content = (
<Component
{...contextProps.defaultPropsConfig[displayName]}
{...newOthers}
{...newContextProps}
ref={obj.isClassComponent(Component) ? this._getInstance : null}
/>
);
const { open, ...othersBoundary } = contextProps.errorBoundary;
return open ? <ErrorBoundary {...othersBoundary}>{content}</ErrorBoundary> : content;
}
}
ConfigedComponent.displayName = `Config(${getDisplayName(Component)})`;
hoistNonReactStatic(ConfigedComponent, Component);
return ConfigedComponent;
}