1 | import _extends from 'babel-runtime/helpers/extends';
|
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 |
|
6 | var _class, _temp;
|
7 |
|
8 | import { Component, Children } from 'react';
|
9 | import PropTypes from 'prop-types';
|
10 | import { polyfill } from 'react-lifecycles-compat';
|
11 | import getContextProps from './get-context-props';
|
12 | import { config, initLocales, setLanguage, setLocale, setDirection, getLocale, getLanguage, getDirection } from './config';
|
13 | import Consumer from './consumer';
|
14 | import ErrorBoundary from './error-boundary';
|
15 | import Cache from './cache';
|
16 | import datejs from '../util/date';
|
17 |
|
18 | var childContextCache = new Cache();
|
19 |
|
20 | var setMomentLocale = function setMomentLocale(locale) {
|
21 | var moment = void 0;
|
22 | try {
|
23 | moment = require('moment');
|
24 | if (moment && moment.default && moment.default.isMoment) moment = moment.default;
|
25 | } catch (e) {
|
26 |
|
27 | }
|
28 |
|
29 | if (moment && locale) {
|
30 | moment.locale(locale.momentLocale);
|
31 | }
|
32 | };
|
33 |
|
34 | var setDateLocale = function setDateLocale(locale) {
|
35 | if (locale) {
|
36 | datejs.locale(locale.dateLocale || locale.momentLocale);
|
37 | }
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | var ConfigProvider = (_temp = _class = function (_Component) {
|
45 | _inherits(ConfigProvider, _Component);
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function ConfigProvider() {
|
54 | _classCallCheck(this, ConfigProvider);
|
55 |
|
56 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
57 | args[_key] = arguments[_key];
|
58 | }
|
59 |
|
60 | var _this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args)));
|
61 |
|
62 | childContextCache.add(_this, _extends({}, childContextCache.get(_this, {}), _this.getChildContext()));
|
63 |
|
64 | setMomentLocale(_this.props.locale);
|
65 | setDateLocale(_this.props.locale);
|
66 |
|
67 | _this.state = {
|
68 | locale: _this.props.locale
|
69 | };
|
70 | return _this;
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | ConfigProvider.prototype.getChildContext = function getChildContext() {
|
82 | var _props = this.props,
|
83 | prefix = _props.prefix,
|
84 | locale = _props.locale,
|
85 | defaultPropsConfig = _props.defaultPropsConfig,
|
86 | pure = _props.pure,
|
87 | warning = _props.warning,
|
88 | rtl = _props.rtl,
|
89 | device = _props.device,
|
90 | popupContainer = _props.popupContainer,
|
91 | errorBoundary = _props.errorBoundary;
|
92 | var _context = this.context,
|
93 | nextPrefix = _context.nextPrefix,
|
94 | nextDefaultPropsConfig = _context.nextDefaultPropsConfig,
|
95 | nextLocale = _context.nextLocale,
|
96 | nextPure = _context.nextPure,
|
97 | nextRtl = _context.nextRtl,
|
98 | nextWarning = _context.nextWarning,
|
99 | nextDevice = _context.nextDevice,
|
100 | nextPopupContainer = _context.nextPopupContainer,
|
101 | nextErrorBoundary = _context.nextErrorBoundary;
|
102 |
|
103 |
|
104 | return {
|
105 | nextPrefix: prefix || nextPrefix,
|
106 | nextDefaultPropsConfig: defaultPropsConfig || nextDefaultPropsConfig,
|
107 | nextLocale: locale || nextLocale,
|
108 | nextPure: typeof pure === 'boolean' ? pure : nextPure,
|
109 | nextRtl: typeof rtl === 'boolean' ? rtl : nextRtl,
|
110 | nextWarning: typeof warning === 'boolean' ? warning : nextWarning,
|
111 | nextDevice: device || nextDevice,
|
112 | nextPopupContainer: popupContainer || nextPopupContainer,
|
113 | nextErrorBoundary: errorBoundary || nextErrorBoundary
|
114 | };
|
115 | };
|
116 |
|
117 | ConfigProvider.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
|
118 | if (nextProps.locale !== prevState.locale) {
|
119 | setMomentLocale(nextProps.locale);
|
120 | setDateLocale(nextProps.locale);
|
121 |
|
122 | return {
|
123 | locale: nextProps.locale
|
124 | };
|
125 | }
|
126 |
|
127 | return null;
|
128 | };
|
129 |
|
130 | ConfigProvider.prototype.componentDidUpdate = function componentDidUpdate() {
|
131 | childContextCache.add(this, _extends({}, childContextCache.get(this, {}), this.getChildContext()));
|
132 | };
|
133 |
|
134 | ConfigProvider.prototype.componentWillUnmount = function componentWillUnmount() {
|
135 | childContextCache.remove(this);
|
136 | };
|
137 |
|
138 | ConfigProvider.prototype.render = function render() {
|
139 | return Children.only(this.props.children);
|
140 | };
|
141 |
|
142 | return ConfigProvider;
|
143 | }(Component), _class.propTypes = {
|
144 | |
145 |
|
146 |
|
147 | prefix: PropTypes.string,
|
148 | |
149 |
|
150 |
|
151 | locale: PropTypes.object,
|
152 | |
153 |
|
154 |
|
155 | defaultPropsConfig: PropTypes.object,
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | errorBoundary: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
|
164 | |
165 |
|
166 |
|
167 | pure: PropTypes.bool,
|
168 | |
169 |
|
170 |
|
171 | warning: PropTypes.bool,
|
172 | |
173 |
|
174 |
|
175 | rtl: PropTypes.bool,
|
176 | |
177 |
|
178 |
|
179 | device: PropTypes.oneOf(['tablet', 'desktop', 'phone']),
|
180 | |
181 |
|
182 |
|
183 | children: PropTypes.any,
|
184 | |
185 |
|
186 |
|
187 | popupContainer: PropTypes.any
|
188 | }, _class.defaultProps = {
|
189 | warning: true,
|
190 | errorBoundary: false
|
191 | }, _class.contextTypes = {
|
192 | nextPrefix: PropTypes.string,
|
193 | nextLocale: PropTypes.object,
|
194 | nextDefaultPropsConfig: PropTypes.object,
|
195 | nextPure: PropTypes.bool,
|
196 | nextRtl: PropTypes.bool,
|
197 | nextWarning: PropTypes.bool,
|
198 | nextDevice: PropTypes.oneOf(['tablet', 'desktop', 'phone']),
|
199 | nextPopupContainer: PropTypes.any,
|
200 | nextErrorBoundary: PropTypes.oneOfType([PropTypes.bool, PropTypes.object])
|
201 | }, _class.childContextTypes = {
|
202 | nextPrefix: PropTypes.string,
|
203 | nextLocale: PropTypes.object,
|
204 | nextDefaultPropsConfig: PropTypes.object,
|
205 | nextPure: PropTypes.bool,
|
206 | nextRtl: PropTypes.bool,
|
207 | nextWarning: PropTypes.bool,
|
208 | nextDevice: PropTypes.oneOf(['tablet', 'desktop', 'phone']),
|
209 | nextPopupContainer: PropTypes.any,
|
210 | nextErrorBoundary: PropTypes.oneOfType([PropTypes.bool, PropTypes.object])
|
211 | }, _class.config = function (Component, options) {
|
212 | return config(Component, options);
|
213 | }, _class.getContextProps = function (props, displayName) {
|
214 | return getContextProps(props, childContextCache.root() || {}, displayName);
|
215 | }, _class.clearCache = function () {
|
216 | childContextCache.clear();
|
217 | }, _class.initLocales = initLocales, _class.setLanguage = setLanguage, _class.setLocale = setLocale, _class.setDirection = setDirection, _class.getLanguage = getLanguage, _class.getLocale = getLocale, _class.getDirection = getDirection, _class.Consumer = Consumer, _class.ErrorBoundary = ErrorBoundary, _class.getContext = function () {
|
218 | var _ref = childContextCache.root() || {},
|
219 | nextPrefix = _ref.nextPrefix,
|
220 | nextLocale = _ref.nextLocale,
|
221 | nextDefaultPropsConfig = _ref.nextDefaultPropsConfig,
|
222 | nextPure = _ref.nextPure,
|
223 | nextRtl = _ref.nextRtl,
|
224 | nextWarning = _ref.nextWarning,
|
225 | nextDevice = _ref.nextDevice,
|
226 | nextPopupContainer = _ref.nextPopupContainer,
|
227 | nextErrorBoundary = _ref.nextErrorBoundary;
|
228 |
|
229 | return {
|
230 | prefix: nextPrefix,
|
231 | locale: nextLocale,
|
232 | defaultPropsConfig: nextDefaultPropsConfig,
|
233 | pure: nextPure,
|
234 | rtl: nextRtl,
|
235 | warning: nextWarning,
|
236 | device: nextDevice,
|
237 | popupContainer: nextPopupContainer,
|
238 | errorBoundary: nextErrorBoundary
|
239 | };
|
240 | }, _temp);
|
241 | ConfigProvider.displayName = 'ConfigProvider';
|
242 |
|
243 |
|
244 | export default polyfill(ConfigProvider); |
\ | No newline at end of file |