mobile-more
Version:
基于 antd-mobile v5 扩展移动端 UI 组件
72 lines • 2.9 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["form", "name", "onFinish"];
import React, { useContext, useRef } from 'react';
import { checkResult } from 'util-helpers';
import { useMount } from 'rc-hooks';
import BizForm from '../Form';
import StepsFormContext from './StepsFormContext';
import StepFormContext from './StepFormContext';
var StepForm = function StepForm(_ref) {
var _ctx$formProps;
var formProp = _ref.form,
customName = _ref.name,
customOnFinish = _ref.onFinish,
restProps = _objectWithoutProperties(_ref, _excluded);
var _BizForm$useForm = BizForm.useForm(),
_BizForm$useForm2 = _slicedToArray(_BizForm$useForm, 1),
form = _BizForm$useForm2[0];
var formRef = useRef(formProp || form);
var ctx = useContext(StepsFormContext);
var stepCtx = useContext(StepFormContext);
var name = customName || stepCtx.name;
var _onFinish = customOnFinish || (ctx === null || ctx === void 0 ? void 0 : (_ctx$formProps = ctx.formProps) === null || _ctx$formProps === void 0 ? void 0 : _ctx$formProps.onFinish);
useMount(function () {
if (ctx && stepCtx) {
ctx.formArrayRef.current[stepCtx.step] = formRef.current;
}
});
return /*#__PURE__*/React.createElement(BizForm, _objectSpread(_objectSpread(_objectSpread({}, ctx === null || ctx === void 0 ? void 0 : ctx.formProps), restProps), {}, {
name: name,
form: formRef.current,
onFinish: function () {
var _onFinish2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(values) {
var ret;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return checkResult(_onFinish, values);
case 2:
ret = _context.sent;
if (ret) {
_context.next = 5;
break;
}
return _context.abrupt("return");
case 5:
if (ctx && stepCtx) {
ctx.onFormFinish(name, values);
if (stepCtx.step !== stepCtx.total) {
ctx.next();
} else {
ctx.submit();
}
}
case 6:
case "end":
return _context.stop();
}
}, _callee);
}));
function onFinish(_x) {
return _onFinish2.apply(this, arguments);
}
return onFinish;
}()
}));
};
export default StepForm;