@txdfe/at
Version:
一个设计体系组件库
209 lines (207 loc) • 10.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _getContextProps = _interopRequireDefault(require("./get-context-props"));
var _config = require("./config");
var _consumer = _interopRequireDefault(require("./consumer"));
var _errorBoundary = _interopRequireDefault(require("./error-boundary"));
var _cache = _interopRequireDefault(require("./cache"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var childContextCache = new _cache["default"]();
/**
* ConfigProvider
* @propsExtends false
*/
var ConfigProvider = /*#__PURE__*/function (_Component) {
function ConfigProvider() {
var _this;
_classCallCheck(this, ConfigProvider);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, ConfigProvider, [].concat(args));
childContextCache.add(_this, _extends({}, childContextCache.get(_this, {}), _this.getChildContext()));
return _this;
}
_inherits(ConfigProvider, _Component);
return _createClass(ConfigProvider, [{
key: "getChildContext",
value: function getChildContext() {
var _this$props = this.props,
prefix = _this$props.prefix,
locale = _this$props.locale,
pure = _this$props.pure,
warning = _this$props.warning,
rtl = _this$props.rtl,
errorBoundary = _this$props.errorBoundary,
scriptUrl = _this$props.scriptUrl;
return {
nextPrefix: prefix,
nextLocale: locale,
nextPure: pure,
nextRtl: rtl,
nextWarning: warning,
nextErrorBoundary: errorBoundary,
nextScriptUrl: scriptUrl
};
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
this.setMomentLocale(this.props.locale);
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if (this.props.locale !== nextProps.locale) {
this.setMomentLocale(nextProps.locale);
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
childContextCache.add(this, _extends({}, childContextCache.get(this, {}), this.getChildContext()));
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
childContextCache.remove(this);
}
}, {
key: "setMomentLocale",
value: function setMomentLocale(locale) {
var moment;
try {
moment = require('moment');
} catch (e) {
// ignore
}
if (moment && locale) {
moment.locale(locale.momentLocale);
}
}
}, {
key: "render",
value: function render() {
return _react.Children.only(this.props.children);
}
}]);
}(_react.Component);
_defineProperty(ConfigProvider, "propTypes", {
/**
* 样式类名的品牌前缀
*/
prefix: _propTypes["default"].string,
/**
* 国际化文案对象,属性为组件的 displayName
*/
locale: _propTypes["default"].object,
/**
* 是否开启错误捕捉 errorBoundary
* 如需自定义参数,请传入对象 对象接受参数列表如下:
*
* fallbackUI `Function(error?: {}, errorInfo?: {}) => Element` 捕获错误后的展示
* afterCatch `Function(error?: {}, errorInfo?: {})` 捕获错误后的行为, 比如埋点上传
*/
errorBoundary: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].object]),
/**
* 是否开启 Pure Render 模式,会提高性能,但是也会带来副作用
*/
pure: _propTypes["default"].bool,
/**
* 是否在开发模式下显示组件属性被废弃的 warning 提示
*/
warning: _propTypes["default"].bool,
/**
* 是否开启 rtl 模式
*/
rtl: _propTypes["default"].bool,
/**
* 组件树
*/
children: _propTypes["default"].element,
/**
* Icon symbol加载的js资源
*/
scriptUrl: _propTypes["default"].string
});
_defineProperty(ConfigProvider, "defaultProps", {
warning: true,
errorBoundary: false
});
_defineProperty(ConfigProvider, "childContextTypes", {
nextPrefix: _propTypes["default"].string,
nextLocale: _propTypes["default"].object,
nextPure: _propTypes["default"].bool,
nextRtl: _propTypes["default"].bool,
nextWarning: _propTypes["default"].bool,
nextErrorBoundary: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].object]),
nextScriptUrl: _propTypes["default"].string
});
/**
* 传入组件,生成受 ConfigProvider 控制的 HOC 组件
* @param {Component} Component 组件类
* @param {Object} options 可选项
* @returns {Component} HOC
*/
_defineProperty(ConfigProvider, "config", function (Component, options) {
return (0, _config.config)(Component, options);
});
/**
* 传入组件的 props 和 displayName,得到和 childContext 计算过的包含有 preifx/locale/pure 的对象,一般用于通过静态方法生成脱离组件树的组件
* @param {Object} props 组件的 props
* @param {String} displayName 组件的 displayName
* @returns {Object} 新的 context props
*/
_defineProperty(ConfigProvider, "getContextProps", function (props, displayName) {
return (0, _getContextProps["default"])(props, childContextCache.root() || {}, displayName);
});
_defineProperty(ConfigProvider, "initLocales", _config.initLocales);
_defineProperty(ConfigProvider, "setLanguage", _config.setLanguage);
_defineProperty(ConfigProvider, "setLocale", _config.setLocale);
_defineProperty(ConfigProvider, "setDirection", _config.setDirection);
_defineProperty(ConfigProvider, "setScriptUrl", _config.setScriptUrl);
_defineProperty(ConfigProvider, "getLanguage", _config.getLanguage);
_defineProperty(ConfigProvider, "getLocale", _config.getLocale);
_defineProperty(ConfigProvider, "getDirection", _config.getDirection);
_defineProperty(ConfigProvider, "Consumer", _consumer["default"]);
_defineProperty(ConfigProvider, "ErrorBoundary", _errorBoundary["default"]);
_defineProperty(ConfigProvider, "getContext", function () {
var _ref = childContextCache.root() || {},
nextPrefix = _ref.nextPrefix,
nextLocale = _ref.nextLocale,
nextPure = _ref.nextPure,
nextRtl = _ref.nextRtl,
nextWarning = _ref.nextWarning,
nextErrorBoundary = _ref.nextErrorBoundary,
nextScriptUrl = _ref.nextScriptUrl;
return {
prefix: nextPrefix,
locale: nextLocale,
pure: nextPure,
rtl: nextRtl,
warning: nextWarning,
errorBoundary: nextErrorBoundary,
scriptUrl: nextScriptUrl
};
});
var _default = exports["default"] = ConfigProvider;