1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
|
6 |
|
7 | var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
|
8 |
|
9 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
10 |
|
11 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
12 |
|
13 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
14 |
|
15 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
16 |
|
17 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
18 |
|
19 | var _inherits3 = _interopRequireDefault(_inherits2);
|
20 |
|
21 | var _extends2 = require('babel-runtime/helpers/extends');
|
22 |
|
23 | var _extends3 = _interopRequireDefault(_extends2);
|
24 |
|
25 | exports.initLocales = initLocales;
|
26 | exports.setLanguage = setLanguage;
|
27 | exports.setLocale = setLocale;
|
28 | exports.setDirection = setDirection;
|
29 | exports.getLocale = getLocale;
|
30 | exports.getLanguage = getLanguage;
|
31 | exports.getDirection = getDirection;
|
32 | exports.config = config;
|
33 |
|
34 | var _react = require('react');
|
35 |
|
36 | var _react2 = _interopRequireDefault(_react);
|
37 |
|
38 | var _propTypes = require('prop-types');
|
39 |
|
40 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
41 |
|
42 | var _hoistNonReactStatics = require('hoist-non-react-statics');
|
43 |
|
44 | var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics);
|
45 |
|
46 | var _util = require('../util');
|
47 |
|
48 | var _getContextProps = require('./get-context-props');
|
49 |
|
50 | var _getContextProps2 = _interopRequireDefault(_getContextProps);
|
51 |
|
52 | var _errorBoundary = require('./error-boundary');
|
53 |
|
54 | var _errorBoundary2 = _interopRequireDefault(_errorBoundary);
|
55 |
|
56 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
57 |
|
58 | var shallowEqual = _util.obj.shallowEqual;
|
59 |
|
60 |
|
61 | function getDisplayName(Component) {
|
62 | return Component.displayName || Component.name || 'Component';
|
63 | }
|
64 |
|
65 | var globalLocales = void 0;
|
66 | var currentGlobalLanguage = 'zh-cn';
|
67 | var currentGlobalLocale = {};
|
68 | var currentGlobalRtl = void 0;
|
69 |
|
70 | function initLocales(locales) {
|
71 | globalLocales = locales;
|
72 |
|
73 | if (locales) {
|
74 | currentGlobalLocale = locales[currentGlobalLanguage];
|
75 |
|
76 | if (typeof currentGlobalRtl !== 'boolean') {
|
77 | currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
|
78 | }
|
79 | }
|
80 | }
|
81 |
|
82 | function setLanguage(language) {
|
83 | if (globalLocales) {
|
84 | currentGlobalLanguage = language;
|
85 | currentGlobalLocale = globalLocales[language];
|
86 |
|
87 | if (typeof currentGlobalRtl !== 'boolean') {
|
88 | currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
|
89 | }
|
90 | }
|
91 | }
|
92 |
|
93 | function setLocale(locale) {
|
94 | currentGlobalLocale = (0, _extends3.default)({}, globalLocales ? globalLocales[currentGlobalLanguage] : {}, locale);
|
95 |
|
96 | if (typeof currentGlobalRtl !== 'boolean') {
|
97 | currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
|
98 | }
|
99 | }
|
100 |
|
101 | function setDirection(dir) {
|
102 | currentGlobalRtl = dir === 'rtl';
|
103 | }
|
104 |
|
105 | function getLocale() {
|
106 | return currentGlobalLocale;
|
107 | }
|
108 |
|
109 | function getLanguage() {
|
110 | return currentGlobalLanguage;
|
111 | }
|
112 |
|
113 | function getDirection() {
|
114 | return currentGlobalRtl;
|
115 | }
|
116 |
|
117 | function config(Component) {
|
118 | var _class, _temp;
|
119 |
|
120 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
121 |
|
122 |
|
123 | if (_util.obj.isClassComponent(Component) && Component.prototype.shouldComponentUpdate === undefined) {
|
124 |
|
125 | Component.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
|
126 | if (this.props.pure) {
|
127 | return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
|
128 | }
|
129 |
|
130 | return true;
|
131 | };
|
132 | }
|
133 |
|
134 | var ConfigedComponent = (_temp = _class = function (_React$Component) {
|
135 | (0, _inherits3.default)(ConfigedComponent, _React$Component);
|
136 |
|
137 | function ConfigedComponent(props, context) {
|
138 | (0, _classCallCheck3.default)(this, ConfigedComponent);
|
139 |
|
140 | var _this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call(this, props, context));
|
141 |
|
142 | _this._getInstance = _this._getInstance.bind(_this);
|
143 | _this._deprecated = _this._deprecated.bind(_this);
|
144 | return _this;
|
145 | }
|
146 |
|
147 | ConfigedComponent.prototype._getInstance = function _getInstance(ref) {
|
148 | var _this2 = this;
|
149 |
|
150 | this._instance = ref;
|
151 |
|
152 | if (this._instance && options.exportNames) {
|
153 | options.exportNames.forEach(function (name) {
|
154 | var field = _this2._instance[name];
|
155 | if (typeof field === 'function') {
|
156 | _this2[name] = field.bind(_this2._instance);
|
157 | } else {
|
158 | _this2[name] = field;
|
159 | }
|
160 | });
|
161 | }
|
162 | };
|
163 |
|
164 | ConfigedComponent.prototype._deprecated = function _deprecated() {
|
165 | if (this.context.nextWarning !== false) {
|
166 | _util.log.deprecated.apply(_util.log, arguments);
|
167 | }
|
168 | };
|
169 |
|
170 | ConfigedComponent.prototype.getInstance = function getInstance() {
|
171 | return this._instance;
|
172 | };
|
173 |
|
174 | ConfigedComponent.prototype.render = function render() {
|
175 | var _props = this.props,
|
176 | prefix = _props.prefix,
|
177 | locale = _props.locale,
|
178 | defaultPropsConfig = _props.defaultPropsConfig,
|
179 | pure = _props.pure,
|
180 | rtl = _props.rtl,
|
181 | device = _props.device,
|
182 | popupContainer = _props.popupContainer,
|
183 | errorBoundary = _props.errorBoundary,
|
184 | others = (0, _objectWithoutProperties3.default)(_props, ['prefix', 'locale', 'defaultPropsConfig', 'pure', 'rtl', 'device', 'popupContainer', 'errorBoundary']);
|
185 | var _context = this.context,
|
186 | nextPrefix = _context.nextPrefix,
|
187 | _context$nextLocale = _context.nextLocale,
|
188 | nextLocale = _context$nextLocale === undefined ? {} : _context$nextLocale,
|
189 | _context$nextDefaultP = _context.nextDefaultPropsConfig,
|
190 | nextDefaultPropsConfig = _context$nextDefaultP === undefined ? {} : _context$nextDefaultP,
|
191 | nextPure = _context.nextPure,
|
192 | nextRtl = _context.nextRtl,
|
193 | nextDevice = _context.nextDevice,
|
194 | nextPopupContainer = _context.nextPopupContainer,
|
195 | nextErrorBoundary = _context.nextErrorBoundary;
|
196 |
|
197 |
|
198 | var displayName = options.componentName || getDisplayName(Component);
|
199 | var contextProps = (0, _getContextProps2.default)({
|
200 | prefix: prefix,
|
201 | locale: locale,
|
202 | defaultPropsConfig: defaultPropsConfig,
|
203 | pure: pure,
|
204 | device: device,
|
205 | popupContainer: popupContainer,
|
206 | rtl: rtl,
|
207 | errorBoundary: errorBoundary
|
208 | }, {
|
209 | nextPrefix: nextPrefix,
|
210 | nextLocale: (0, _extends3.default)({}, currentGlobalLocale, nextLocale),
|
211 | nextDefaultPropsConfig: nextDefaultPropsConfig,
|
212 | nextPure: nextPure,
|
213 | nextDevice: nextDevice,
|
214 | nextPopupContainer: nextPopupContainer,
|
215 | nextRtl: typeof nextRtl === 'boolean' ? nextRtl : currentGlobalRtl === true ? true : undefined,
|
216 | nextErrorBoundary: nextErrorBoundary
|
217 | }, displayName);
|
218 |
|
219 |
|
220 | var newContextProps = ['prefix', 'locale', 'pure', 'rtl', 'device', 'popupContainer'].reduce(function (ret, name) {
|
221 | if (typeof contextProps[name] !== 'undefined') {
|
222 | ret[name] = contextProps[name];
|
223 | }
|
224 | return ret;
|
225 | }, {});
|
226 |
|
227 | if ('pure' in newContextProps && newContextProps.pure) {
|
228 | _util.log.warning('pure of ConfigProvider is deprecated, use Function Component or React.PureComponent');
|
229 | }
|
230 |
|
231 |
|
232 | if ('popupContainer' in newContextProps && this.props.container === undefined && ['Overlay', 'Popup'].indexOf(displayName) > -1) {
|
233 | newContextProps.container = newContextProps.popupContainer;
|
234 | delete newContextProps.popupContainer;
|
235 | }
|
236 |
|
237 | var newOthers = options.transform ? options.transform(others, this._deprecated) : others;
|
238 |
|
239 | var content = _react2.default.createElement(Component, (0, _extends3.default)({}, contextProps.defaultPropsConfig[displayName], newOthers, newContextProps, {
|
240 | ref: _util.obj.isClassComponent(Component) ? this._getInstance : null
|
241 | }));
|
242 |
|
243 | var _contextProps$errorBo = contextProps.errorBoundary,
|
244 | open = _contextProps$errorBo.open,
|
245 | othersBoundary = (0, _objectWithoutProperties3.default)(_contextProps$errorBo, ['open']);
|
246 |
|
247 |
|
248 | return open ? _react2.default.createElement(
|
249 | _errorBoundary2.default,
|
250 | othersBoundary,
|
251 | content
|
252 | ) : content;
|
253 | };
|
254 |
|
255 | return ConfigedComponent;
|
256 | }(_react2.default.Component), _class.propTypes = (0, _extends3.default)({}, Component.propTypes || {}, {
|
257 | prefix: _propTypes2.default.string,
|
258 | locale: _propTypes2.default.object,
|
259 | defaultPropsConfig: _propTypes2.default.object,
|
260 | pure: _propTypes2.default.bool,
|
261 | rtl: _propTypes2.default.bool,
|
262 | device: _propTypes2.default.oneOf(['tablet', 'desktop', 'phone']),
|
263 | popupContainer: _propTypes2.default.any,
|
264 | errorBoundary: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.object])
|
265 | }), _class.contextTypes = (0, _extends3.default)({}, Component.contextTypes || {}, {
|
266 | nextPrefix: _propTypes2.default.string,
|
267 | nextLocale: _propTypes2.default.object,
|
268 | nextDefaultPropsConfig: _propTypes2.default.object,
|
269 | nextPure: _propTypes2.default.bool,
|
270 | nextRtl: _propTypes2.default.bool,
|
271 | nextWarning: _propTypes2.default.bool,
|
272 | nextDevice: _propTypes2.default.oneOf(['tablet', 'desktop', 'phone']),
|
273 | nextPopupContainer: _propTypes2.default.any,
|
274 | nextErrorBoundary: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.object])
|
275 | }), _temp);
|
276 | ConfigedComponent.displayName = 'ConfigedComponent';
|
277 |
|
278 |
|
279 | ConfigedComponent.displayName = 'Config(' + getDisplayName(Component) + ')';
|
280 |
|
281 | (0, _hoistNonReactStatics2.default)(ConfigedComponent, Component);
|
282 |
|
283 | return ConfigedComponent;
|
284 | } |
\ | No newline at end of file |