@txdfe/at
Version:
一个设计体系组件库
266 lines (207 loc) • 10.7 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
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(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var childContextCache = new _cache["default"]();
/**
* ConfigProvider
* @propsExtends false
*/
var ConfigProvider = /*#__PURE__*/function (_Component) {
_inherits(ConfigProvider, _Component);
var _super = _createSuper(ConfigProvider);
/**
* 传入组件,生成受 ConfigProvider 控制的 HOC 组件
* @param {Component} Component 组件类
* @param {Object} options 可选项
* @returns {Component} HOC
*/
/**
* 传入组件的 props 和 displayName,得到和 childContext 计算过的包含有 preifx/locale/pure 的对象,一般用于通过静态方法生成脱离组件树的组件
* @param {Object} props 组件的 props
* @param {String} displayName 组件的 displayName
* @returns {Object} 新的 context props
*/
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 = _super.call.apply(_super, [this].concat(args));
childContextCache.add(_assertThisInitialized(_this), _extends({}, childContextCache.get(_assertThisInitialized(_this), {}), _this.getChildContext()));
return _this;
}
_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);
}
}]);
return ConfigProvider;
}(_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
});
_defineProperty(ConfigProvider, "config", function (Component, options) {
return (0, _config.config)(Component, options);
});
_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 = ConfigProvider;
exports["default"] = _default;