UNPKG

@sula/ruler

Version:

363 lines (273 loc) 15.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("antd/es/form/style"); var _form = _interopRequireDefault(require("antd/es/form")); var _react = _interopRequireDefault(require("react")); var _rcFieldForm = require("rc-field-form"); var _isUndefined = _interopRequireDefault(require("lodash/isUndefined")); var _isFunction = _interopRequireDefault(require("lodash/isFunction")); var _RulerContext = _interopRequireWildcard(require("./RulerContext")); var _ListContext = _interopRequireDefault(require("./ListContext")); var _RowContext = _interopRequireDefault(require("./RowContext")); var _value = require("./utils/value"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } 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; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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 } }); 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); } var FormItem = _form.default.Item; var DEFAULT_VALUE_PROP_NAME = 'value'; var DEFAULT_TRIGGER = 'onChange'; var Field = /*#__PURE__*/ function (_React$Component) { _inherits(Field, _React$Component); function Field() { var _getPrototypeOf2; var _this; _classCallCheck(this, Field); for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) { _args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Field)).call.apply(_getPrototypeOf2, [this].concat(_args))); _this.destroy = false; _this.inited = false; _this.source = void 0; _this.visible = void 0; _this.disabled = void 0; _this.listName = void 0; _this.rowName = void 0; _this.cancelRegisterFunc = null; _this.getName = function (name) { var prefixName = _this.getPrefixName(); return prefixName.concat(name || _this.props.name); }; _this.setSiblingSource = function (siblingName, siblingSource) { var siblingNamePath = _this.getName(siblingName); _this.getFieldEntity(siblingNamePath).setSource(siblingSource); }; _this.getSiblingValue = function (siblingName) { var siblingNamePath = _this.getName(siblingName); return _this.getFormInstance().getFieldValue(siblingNamePath); }; _this.setSiblingValue = function (siblingName, siblingValue) { var siblingNamePath = _this.getName(siblingName); _this.getFormInstance().setFields([{ name: siblingNamePath, value: siblingValue }]); }; _this.setSiblingValues = function (siblingValues) { var fields = []; Object.keys(siblingValues).forEach(function (siblingName) { var siblingNamePath = _this.getName(siblingName); fields.push({ name: siblingNamePath, value: siblingValues[siblingName] }); }); _this.getFormInstance().setFields(fields); }; _this.setSource = function (source) { _this.source = source; _this.reRender(); }; _this.setValue = function (value) { var namePath = _this.getName(); _this.getFormInstance().setFields([{ name: namePath, value: value }]); }; _this.reRender = function () { if (_this.destroy) return; _this.forceUpdate(); }; _this.getPrefixName = function () { return (0, _isUndefined.default)(_this.rowName) ? [_this.listName] : [_this.listName, _this.rowName]; }; _this.getFieldNamePath = function () { return (0, _isUndefined.default)(_this.rowName) ? [_this.props.name] : [_this.rowName, _this.props.name]; }; _this.getFormInstance = function () { return _this.context.getRuler(); }; _this.getFieldEntity = function (namePath) { var _this$context$getInte = _this.context.getInternalHooks(_RulerContext.HOOK_MARK), getFieldEntity = _this$context$getInte.getFieldEntity; return getFieldEntity(namePath); }; _this.renderField = function () { var fieldNamePath = _this.getFieldNamePath(); var _this$props = _this.props, name = _this$props.name, rules = _this$props.rules, children = _this$props.children, restFieldProps = _objectWithoutProperties(_this$props, ["name", "rules", "children"]); var _this$context$getInte2 = _this.context.getInternalHooks(_RulerContext.HOOK_MARK), fieldValueChange = _this$context$getInte2.fieldValueChange, getCtx = _this$context$getInte2.getCtx, getGlobal = _this$context$getInte2.getGlobal; var disabled = getGlobal('disabled'); var hasValidate = getGlobal('hasValidate'); return _react.default.createElement(_rcFieldForm.Field, _extends({ name: fieldNamePath, rules: hasValidate && !disabled ? rules : [] }, restFieldProps, { isListField: true }), function (control, meta) { var element; if ((0, _isFunction.default)(children)) { var renderCtx = getCtx({ name: name, listName: _this.listName, rowName: _this.rowName, value: control[restFieldProps.valuePropName], source: _this.source, meta: meta, getSiblingValue: _this.getSiblingValue }); // @ts-ignore element = children(renderCtx, control); } else { element = children; } var _ref = element.props || {}, selfDisabled = _ref.disabled, onOriginChange = _ref[restFieldProps.trigger]; var finalOnChange = function finalOnChange() { var getValueFromEvent = restFieldProps.getValueFromEvent, valuePropName = restFieldProps.valuePropName; var newValue; for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } if (getValueFromEvent) { newValue = getValueFromEvent.apply(void 0, args); } else { newValue = _value.defaultGetValueFromEvent.apply(void 0, [valuePropName].concat(args)); } /** trigger onFieldValueChange */ var ctx = getCtx({ name: name, listName: _this.listName, rowName: _this.rowName, value: newValue, source: _this.source, meta: meta, getSiblingValue: _this.getSiblingValue, setSiblingValue: _this.setSiblingValue, setSiblingValues: _this.setSiblingValues, setSiblingSource: _this.setSiblingSource }); // 完成关联操作 fieldValueChange(ctx); if (onOriginChange) { onOriginChange.apply(void 0, args); } control[restFieldProps.trigger].apply(control, args); }; element = _react.default.cloneElement(element, _objectSpread({}, control, _defineProperty({}, restFieldProps.trigger, finalOnChange), (0, _isUndefined.default)(selfDisabled) ? { disabled: disabled } : {})); if (hasValidate && !disabled) { var validateStatus = (meta.errors || []).length > 0 ? 'error' : undefined; var help = (meta.errors || []).length > 0 ? meta.errors[0] : undefined; // @ts-ignore var required = !!(rules || []).find(function (item) { return item.required; }); var cls = validateStatus === 'error' ? '' : 'sula-ruler-field-no-error'; return _react.default.createElement(FormItem, { help: help, className: cls, validateStatus: validateStatus, required: required }, element); } return _react.default.createElement("div", { className: "sula-ruler-field-no-error" }, element); }); }; return _this; } _createClass(Field, [{ key: "componentDidMount", value: function componentDidMount() { var name = this.props.name; var _this$context$getInte3 = this.context.getInternalHooks(_RulerContext.HOOK_MARK), registerField = _this$context$getInte3.registerField, getCtx = _this$context$getInte3.getCtx, fieldDidMount = _this$context$getInte3.fieldDidMount; this.cancelRegisterFunc = registerField(this); var value = this.getSiblingValue(name); var ctx = getCtx({ name: name, listName: this.listName, rowName: this.rowName, value: value, getSiblingValue: this.getSiblingValue, setSiblingValue: this.setSiblingValue, setSiblingValues: this.setSiblingValues, setSource: this.setSource, setSiblingSource: this.setSiblingSource }); fieldDidMount(ctx); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.destroy = true; if (this.cancelRegisterFunc) { this.cancelRegisterFunc(); } this.cancelRegisterFunc = null; } // ============== 实例方法 =============== /** * 按fieldName获取namePath */ }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, name = _this$props2.name, initialSource = _this$props2.initialSource; if (!this.inited) { this.inited = true; this.source = initialSource; } return _react.default.createElement(_ListContext.default.Consumer, null, function (_ref2) { var listName = _ref2.name; _this2.listName = listName; return _react.default.createElement(_RowContext.default.Consumer, null, function (_ref3) { var rowName = _ref3.name; _this2.rowName = rowName; return _this2.renderField(); }); }); } }]); return Field; }(_react.default.Component); Field.contextType = _RulerContext.default; Field.defaultProps = { valuePropName: DEFAULT_VALUE_PROP_NAME, trigger: DEFAULT_TRIGGER }; var _default = Field; exports.default = _default;