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