[英]react-final-form stepper (wizard form) using typescript form is not getting displayed
I am integrating react-final-form wizard form (react stepper) with typescript this is what i have done till now.我正在将 react-final-form 向导形式(react stepper)与 typescript 集成,这是我到目前为止所做的。
Wizard is the main wizard page were the Final form Form field is written Wizard 是主要的向导页面,其中写入了 Final form Form 字段
import React, { useState } from "react"; import { Form } from "react-final-form"; type Wizard = { onSubmit: (values: Values) => void; }; type Values = { name: String; surname: String; email: String; password: String; city: String; birthDay: Number; birthMonth: Number; birthYear: Number; }; // 3-steps form const Wizard: React.FC<Wizard> = ({ onSubmit, children }) => { const [page, setPage] = useState(0); const [values, setValues] = useState<Values | undefined>(undefined); const activePage = React.Children.toArray(children)[page]; const isLastPage = page === React.Children.count(children) - 1; console.log("CONNECTED"); // const static Page = (children) => children; // next page const next = (values: Values) => { setPage(Math.min(page + 1, React.Children.count(children))); setValues(values); }; // previous page const previous = () => { setPage(Math.max(page - 1, 0)); }; const handleSubmit = (values: Values) => { const isLastPage = page === React.Children.count(children) - 1; if (isLastPage) { return onSubmit(values); } else { next(values); } }; return ( <Form onSubmit={handleSubmit}> {({ handleSubmit, submitting, values }) => { <form onSubmit={handleSubmit}> {activePage} <div className="buttons"> {page > 0 && ( <button type="button" onClick={previous}> « Powrót </button> )} {;isLastPage && <button type="submit">Dalej »</button>} {isLastPage && ( <button type="submit" disabled={submitting}> Zakończ </button> )} </div> </form>; }} </Form> ); }; export default Wizard;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
WizardPage component, as per example given is React final form -wizard example I changed it into typescript while doing that static Page = ({ children }) => children
was throwing error so I moved it to different component. WizardPage 组件,根据给出的示例是React final form -wizard example我在执行该操作时将其更改为 typescript
static Page = ({ children }) => children
正在抛出错误,因此我将其移至不同的组件。
import React from "react"; type props = { children: React.ReactNode; }; export const WizardPage: React.FC<props> = ({ children }) => { console.log(children); return <div>{children}</div>; };
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
BusinessRegister is the main page were the component is getting rendered. BusinessRegister 是组件正在呈现的主页。
import React from "react"; import Wizard from "./Wizard"; import { WizardPage } from "./WizardPage"; import { Field } from "react-final-form"; const BusinessRegister: React.FC = () => { const onSubmit = () => { console.log("onSubmit"); }; return ( <div> <h1>Step form</h1> <Wizard onSubmit={onSubmit}> <WizardPage>Page 1</WizardPage> <WizardPage>Page 2</WizardPage> <WizardPage>Page 3</WizardPage> </Wizard> </div> ); }; export default BusinessRegister;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
I found what was wrong,我发现出了什么问题,
{({ handleSubmit, submitting, values }) => {
<form onSubmit={handleSubmit}>
{activePage}
<div className="buttons">
{page > 0 && (
<button type="button" onClick={previous}>
« Powrót
</button>
)}
{!isLastPage && <button type="submit">Dalej »</button>}
{isLastPage && (
<button type="submit" disabled={submitting}>
Zakończ
</button>
)}
</div>
</form>;
}}
In here I haven't added the return for form that was the reason the form was not getting displayed.在这里我没有添加表单返回,这是表单未显示的原因。 Thank you
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.