UNPKG

react-admin-kit

Version:

A react based UI components for admin system

162 lines (158 loc) 7.3 kB
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime"; import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["onFinish", "formRef", "initialValues", "children", "innerRef", "submitter", "readonlyType", "descriptionsProps"]; import { ProForm as AntProForm } from '@ant-design/pro-form'; import { Form } from 'antd'; import { createContext, useEffect, useImperativeHandle, useRef } from 'react'; import { collectDataIndex, splitValues, transformValuesForConvention } from "../SchemaForm/utils"; import { CreateBaseInner } from "../context"; import { jsx as _jsx } from "react/jsx-runtime"; export var InnerRefContext = /*#__PURE__*/createContext(undefined); export var LayoutContext = /*#__PURE__*/createContext('vertical'); export var ReadonlyContext = /*#__PURE__*/createContext(false); export var ReadonlyTypeContext = /*#__PURE__*/createContext('form'); export var DescriptionsPropsContext = /*#__PURE__*/createContext({}); export var EmbedColumnContext = /*#__PURE__*/createContext({}); var ProForm = function ProForm(props) { var onFinish = props.onFinish, propsFormRef = props.formRef, initialValues = props.initialValues, children = props.children, propsInnerRef = props.innerRef, _props$submitter = props.submitter, submitter = _props$submitter === void 0 ? false : _props$submitter, _props$readonlyType = props.readonlyType, readonlyType = _props$readonlyType === void 0 ? 'form' : _props$readonlyType, _props$descriptionsPr = props.descriptionsProps, descriptionsProps = _props$descriptionsPr === void 0 ? {} : _props$descriptionsPr, rest = _objectWithoutProperties(props, _excluded); // 当 innerRef 不传时提供一个内部默认值, 保证 innerRef 不为空 var innerRef = useRef(CreateBaseInner()); /** * 给 innerRef 增加方法 */ useImperativeHandle(propsInnerRef, function () { return innerRef.current; }, []); var handleOnFinish = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(values) { return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!onFinish) { _context.next = 4; break; } _context.next = 3; return onFinish(splitValues(values)); case 3: return _context.abrupt("return", _context.sent); case 4: case "end": return _context.stop(); } }, _callee); })); return function handleOnFinish(_x) { return _ref.apply(this, arguments); }; }(); var selfFormRef = useRef(); var initialValuesRef = useRef(initialValues); var embedColumnsRef = useRef([]); var getDataIndexes = function getDataIndexes(values) { return embedColumnsRef.current.reduce(function (prev, item) { return prev.concat(collectDataIndex(item.columns, values, item.baseName)); }, []); }; var wrapForm = function wrapForm(form) { var setFieldsValue = form.setFieldsValue; form.from = 'proform'; form.setFieldsValue = function (values) { var _innerRef$current; var $values = transformValuesForConvention(values, getDataIndexes(values)); setFieldsValue($values); /** 将赋值的值额外存在 innerRef 里, 在 render 函数(只读模式), 表单提交等场景里可用 */ (_innerRef$current = innerRef.current) === null || _innerRef$current === void 0 || _innerRef$current.setData($values || {}); }; return form; }; var _Form$useForm = Form.useForm(), _Form$useForm2 = _slicedToArray(_Form$useForm, 1), form = _Form$useForm2[0]; var formInstance = Form.useFormInstance(); var formInstanceRef = useRef(wrapForm(props.form || formInstance || form)); useEffect(function () { if (formInstanceRef.current) { var _formInstanceRef$curr2; // @ts-ignore var _formInstanceRef$curr = formInstanceRef.current.getInternalHooks('RC_FORM_INTERNAL_HOOKS'), setInitialValues = _formInstanceRef$curr.setInitialValues; var $values = transformValuesForConvention(initialValues, getDataIndexes(initialValues)); (_formInstanceRef$curr2 = formInstanceRef.current) === null || _formInstanceRef$curr2 === void 0 || _formInstanceRef$curr2.setFieldsValue($values); setInitialValues($values); } }, []); /* 包装 form 实例的取值相关的方法, 需要约定式转化 * getFieldsValue, validateFields, getFieldsFormatValue, validateFieldsReturnFormatValue */ useImperativeHandle(propsFormRef, function () { if (selfFormRef.current) { var _selfFormRef$current = selfFormRef.current, _getFieldsValue = _selfFormRef$current.getFieldsValue, _validateFields = _selfFormRef$current.validateFields, getFieldsFormatValue = _selfFormRef$current.getFieldsFormatValue, validateFieldsReturnFormatValue = _selfFormRef$current.validateFieldsReturnFormatValue; return _objectSpread(_objectSpread({}, selfFormRef.current), {}, { getFieldsValue: function getFieldsValue() { // @ts-ignore return splitValues(_getFieldsValue.apply(void 0, arguments)); }, getFieldsFormatValue: getFieldsFormatValue ? function (namePath, filter) { return splitValues(getFieldsFormatValue(namePath, filter)); } : undefined, validateFields: function validateFields() { return _validateFields.apply(void 0, arguments).then(function (res) { return splitValues(res); }); }, validateFieldsReturnFormatValue: validateFieldsReturnFormatValue ? function (namePath) { return validateFieldsReturnFormatValue(namePath).then(function (res) { return splitValues(res); }); } : undefined }); } }, [selfFormRef.current]); return /*#__PURE__*/_jsx(InnerRefContext.Provider, { value: innerRef, children: /*#__PURE__*/_jsx(LayoutContext.Provider, { value: props.layout, children: /*#__PURE__*/_jsx(ReadonlyContext.Provider, { value: props.readonly || false, children: /*#__PURE__*/_jsx(ReadonlyTypeContext.Provider, { value: readonlyType, children: /*#__PURE__*/_jsx(DescriptionsPropsContext.Provider, { value: descriptionsProps, children: /*#__PURE__*/_jsx(EmbedColumnContext.Provider, { value: embedColumnsRef, children: /*#__PURE__*/_jsx(AntProForm, _objectSpread(_objectSpread({ onFinish: handleOnFinish, initialValues: initialValuesRef.current, formRef: selfFormRef, submitter: submitter === true ? {} : submitter }, rest), {}, { form: formInstanceRef.current, children: children })) }) }) }) }) }) }); }; export default ProForm;