form-render
Version:
通过 JSON Schema 生成标准 Form,常用于自定义搭建配置界面生成
99 lines • 5.43 kB
JavaScript
import "antd/es/form/style";
import _Form from "antd/es/form";
import "antd/es/col/style";
import _Col from "antd/es/col";
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; }
import { __rest } from "tslib";
import React, { createContext, useContext, useState } from 'react';
import { useStore } from 'zustand';
import { FRContext } from '../../models/context';
import { parseAllExpression } from '../../models/expression';
import { getFormListLayout, getParamValue, getLabel, getTooltip } from './modules';
import Main from './field';
var UpperContext = /*#__PURE__*/createContext(function () {});
export default (function (props) {
var _useState = useState([]),
_useState2 = _slicedToArray(_useState, 2),
_ = _useState2[0],
setListData = _useState2[1];
var configContext = props.configContext;
var store = useContext(FRContext);
var formCtx = useStore(store, function (state) {
return state.context;
});
var upperCtx = useContext(UpperContext);
var form = configContext.form,
widgets = configContext.widgets,
methods = configContext.methods,
globalConfig = configContext.globalConfig;
var displayType = formCtx.displayType;
var isDisplayColumn = displayType === 'column';
var _schema = props.schema;
var formData = form.getFieldsValue(true);
var _store$getState = store.getState(),
formSchema = _store$getState.schema;
var items = _schema.items,
className = _schema.className,
otherSchema = __rest(_schema, ["items", "className"]);
var schema = (globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.mustacheDisabled) ? _schema : Object.assign({
items: items
}, parseAllExpression(otherSchema, formData, props.rootPath, formSchema));
var widget = schema.widget;
var widgetName = widget || 'list1';
var getValueFromKey = getParamValue(formCtx, upperCtx, schema);
var label = getLabel(schema, displayType, widgets);
var tooltip = getTooltip(schema, displayType);
var _getFormListLayout = getFormListLayout(getValueFromKey, displayType),
labelCol = _getFormListLayout.labelCol,
fieldCol = _getFormListLayout.fieldCol;
var isInline = schema.display === 'inline';
var preRootPath = _toConsumableArray(props.rootPath || []).splice(0, props.rootPath.length - 1);
var listData = form.getFieldValue([].concat(_toConsumableArray(preRootPath), _toConsumableArray(props.path)));
if (!(listData === null || listData === void 0 ? void 0 : listData.length) && widgetName !== 'drawerList') {
isInline = true;
}
if (schema.hidden) {
return null;
}
return /*#__PURE__*/React.createElement(_Col, {
span: 24,
className: className
}, !isInline && !isDisplayColumn && label && (/*#__PURE__*/React.createElement(_Form.Item, {
className: 'ant-form-item-optional-hide',
label: label,
labelAlign: 'left',
colon: false,
tooltip: tooltip,
style: {
marginBottom: 0
},
labelCol: {
span: 24
}
})), /*#__PURE__*/React.createElement(_Form.Item, {
label: label,
labelCol: isDisplayColumn ? {
span: 24
} : labelCol,
wrapperCol: fieldCol,
noStyle: !isInline && !isDisplayColumn,
tooltip: tooltip
}, /*#__PURE__*/React.createElement(Main, Object.assign({}, props, {
form: form,
methods: methods,
formCtx: formCtx,
upperCtx: upperCtx,
widgets: widgets,
configContext: configContext,
setListData: setListData
}))));
});