cw-form-render-mobile
Version:
通过 JSON Schema 生成标准 Form,常用于自定义搭建配置界面生成
152 lines (151 loc) • 8.28 kB
JavaScript
;
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _form = _interopRequireDefault(require("antd-mobile/es/components/form"));
var _grid = _interopRequireDefault(require("antd-mobile/es/components/grid"));
var _tslib = require("tslib");
var _antdMobileIcons = require("antd-mobile-icons");
var _expression = require("cw-form-render/es/models/expression");
var _react = _interopRequireWildcard(require("react"));
var _zustand = require("zustand");
var _context = require("../../models/context");
var _utils = require("../../utils");
require("./index.css");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var UpperContext = /*#__PURE__*/(0, _react.createContext)(function () {});
var getParamValue = function getParamValue(formCtx, upperCtx, schema) {
return function (valueKey) {
var _a, _b;
return (_b = (_a = schema[valueKey]) !== null && _a !== void 0 ? _a : upperCtx[valueKey]) !== null && _b !== void 0 ? _b : formCtx[valueKey];
};
};
var _default = exports.default = function _default(props) {
var _a;
var _schema = props.schema,
path = props.path,
renderCore = props.renderCore,
_rootPath = props.rootPath;
var store = (0, _react.useContext)(_context.FRContext);
var formCtx = (0, _zustand.useStore)(store, function (state) {
return state.context;
});
var upperCtx = (0, _react.useContext)(UpperContext);
var _useContext = (0, _react.useContext)(_context.ConfigContext),
form = _useContext.form,
methods = _useContext.methods;
var formData = form.getFieldsValue(true);
var _store$getState = store.getState(),
formSchema = _store$getState.schema;
var items = _schema.items,
otherSchema = (0, _tslib.__rest)(_schema, ["items"]);
var schema = Object.assign({
items: items
}, (0, _expression.parseAllExpression)(otherSchema, formData, _rootPath, formSchema));
var defaultValue = (_a = schema.default) !== null && _a !== void 0 ? _a : schema.defaultValue || [{}];
var _ref = schema.props || {},
onAdd = _ref.onAdd,
onRemove = _ref.onRemove;
var _useState = (0, _react.useState)(),
_useState2 = _slicedToArray(_useState, 2),
fieldLength = _useState2[0],
setFieldLength = _useState2[1];
(0, _react.useEffect)(function () {
var fieldsLength = getFieldsLength();
setFieldLength(fieldsLength);
}, []);
var getFieldsLength = function getFieldsLength() {
var fieldValue = form.getFieldValue(path);
return Array.isArray(fieldValue) ? fieldValue.length : 0;
};
var handleAdd = function handleAdd(add, data) {
var addFunc = onAdd;
if (typeof onAdd === 'string') {
addFunc = methods[onAdd];
}
if ((0, _utils.isFunction)(addFunc)) {
addFunc(function (funData) {
return add(funData || data);
}, {
schema: schema,
data: data
});
return;
}
add(data);
var fieldsLength = getFieldsLength();
setFieldLength(fieldsLength);
};
var handleRemove = function handleRemove(remove, index) {
var removeFunc = onRemove;
if (typeof onRemove === 'string') {
removeFunc = methods[onRemove];
}
if ((0, _utils.isFunction)(removeFunc)) {
removeFunc(function () {
return remove(index);
}, {
schema: schema,
index: index
});
return;
}
remove(index);
var fieldsLength = getFieldsLength();
setFieldLength(fieldsLength);
};
var getValueFromKey = getParamValue(formCtx, upperCtx, schema);
var readOnly = getValueFromKey('readOnly');
if (schema.hidden) {
return null;
}
var preRootPath = _toConsumableArray(_rootPath || []).splice(0, _rootPath.length - 1);
var rootPath = [].concat(_toConsumableArray(preRootPath), _toConsumableArray(path));
return /*#__PURE__*/_react.default.createElement(_grid.default.Item, {
className: "frm-list"
}, /*#__PURE__*/_react.default.createElement(_form.default.Array, {
name: path,
initialValue: defaultValue,
renderAdd: !readOnly && (!schema.max || fieldLength < schema.max) ? function () {
return /*#__PURE__*/_react.default.createElement("span", null, /*#__PURE__*/_react.default.createElement(_antdMobileIcons.AddCircleOutline, null), " \u6DFB\u52A0");
} : undefined,
onAdd: function onAdd(_ref2) {
var add = _ref2.add;
return handleAdd(add);
},
renderHeader: function renderHeader(_ref3, _ref4) {
var index = _ref3.index;
var remove = _ref4.remove;
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, schema.title && (/*#__PURE__*/_react.default.createElement("span", null, schema.title, " ", index + 1)), !readOnly && (!schema.min || fieldLength > schema.min) && (/*#__PURE__*/_react.default.createElement("a", {
onClick: function onClick() {
return handleRemove(remove, index);
},
style: {
float: 'right'
}
}, "\u5220\u9664")));
}
}, function (fields) {
return fields.map(function (_ref5) {
var index = _ref5.index;
return renderCore({
schema: schema,
parentPath: [index],
rootPath: [].concat(_toConsumableArray(rootPath), [index])
});
});
}));
};