UNPKG

8.88 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
3import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
4import _inherits from 'babel-runtime/helpers/inherits';
5
6var _class, _temp;
7
8import { Component, Children } from 'react';
9import PropTypes from 'prop-types';
10import { polyfill } from 'react-lifecycles-compat';
11import getContextProps from './get-context-props';
12import { config, initLocales, setLanguage, setLocale, setDirection, getLocale, getLanguage, getDirection } from './config';
13import Consumer from './consumer';
14import ErrorBoundary from './error-boundary';
15import Cache from './cache';
16import datejs from '../util/date';
17
18var childContextCache = new Cache();
19
20var 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 // ignore
27 }
28
29 if (moment && locale) {
30 moment.locale(locale.momentLocale);
31 }
32};
33
34var setDateLocale = function setDateLocale(locale) {
35 if (locale) {
36 datejs.locale(locale.dateLocale || locale.momentLocale);
37 }
38};
39
40/**
41 * ConfigProvider
42 * @propsExtends false
43 */
44var ConfigProvider = (_temp = _class = function (_Component) {
45 _inherits(ConfigProvider, _Component);
46
47 /**
48 * 传入组件的 props 和 displayName,得到和 childContext 计算过的包含有 preifx/locale/pure 的对象,一般用于通过静态方法生成脱离组件树的组件
49 * @param {Object} props 组件的 props
50 * @param {String} displayName 组件的 displayName
51 * @returns {Object} 新的 context props
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 * 传入组件,生成受 ConfigProvider 控制的 HOC 组件
75 * @param {Component} Component 组件类
76 * @param {Object} options 可选项
77 * @returns {Component} HOC
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 * 国际化文案对象,属性为组件的 displayName
150 */
151 locale: PropTypes.object,
152 /**
153 * 组件 API 的默认配置
154 */
155 defaultPropsConfig: PropTypes.object,
156 /**
157 * 是否开启错误捕捉 errorBoundary
158 * 如需自定义参数,请传入对象 对象接受参数列表如下:
159 *
160 * fallbackUI `Function(error?: {}, errorInfo?: {}) => Element` 捕获错误后的展示
161 * afterCatch `Function(error?: {}, errorInfo?: {})` 捕获错误后的行为, 比如埋点上传
162 */
163 errorBoundary: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
164 /**
165 * 是否开启 Pure Render 模式,会提高性能,但是也会带来副作用
166 */
167 pure: PropTypes.bool,
168 /**
169 * 是否在开发模式下显示组件属性被废弃的 warning 提示
170 */
171 warning: PropTypes.bool,
172 /**
173 * 是否开启 rtl 模式
174 */
175 rtl: PropTypes.bool,
176 /**
177 * 设备类型,针对不同的设备类型组件做出对应的响应式变化
178 */
179 device: PropTypes.oneOf(['tablet', 'desktop', 'phone']),
180 /**
181 * 组件树
182 */
183 children: PropTypes.any,
184 /**
185 * 指定浮层渲染的父节点, 可以为节点id的字符串,也可以返回节点的函数
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);
241ConfigProvider.displayName = 'ConfigProvider';
242
243
244export default polyfill(ConfigProvider);
\No newline at end of file