简体   繁体   English

React on conditions 中的多步表单

[英]Multistep form in React on conditions

I am using Material UI multistep form in my react project.我在我的反应项目中使用 Material UI 多步表单。 I have a requirement in which the next step of the form opens based on the currently selected option.我有一个要求,即根据当前选择的选项打开表单的下一步。

For example, if there are two options A and B. If I choose option A then the next step will have options related to A and if I choose option B then the next step will have options related to B.例如,如果有两个选项 A 和 B。如果我选择选项 A,那么下一步将有与 A 相关的选项,如果我选择选项 B,那么下一步将有与 B 相关的选项。

This is how my code that renders the component based on the current step:这就是我的代码根据当前步骤呈现组件的方式:

 function getStepsContent(stepIndex) {
  switch (stepIndex) {
  case 0:
    return <StepOne handleChange={handleChange} values={values} />;
  case 1:
    return 'Step Two (choose plan)';
  case 2:
    return 'Step three (checkout)';
  default:
    return 'unknown step';
 }
}

And this is my initial function where I have defined my steps of the form:这是我最初的 function ,我在其中定义了表单的步骤:

function getSteps() {
 return ['sign up', 'choose plan', 'checkout'];
}

I am not sure if this is the right way or not.我不确定这是否正确。

Can you please suggest to me the right way to implement the form component based on conditions?您能否根据条件向我建议实现表单组件的正确方法?

Thanks in advance.提前致谢。

This is a good way of doing it, instead making function that returns steps you can just make them as normal const value like:这是一个很好的方法,而不是让 function 返回步骤,您可以将它们设置为正常的const值,例如:

const steps = ['sign up', 'choose plan', 'checkout'];

and then just .map through them with your getStepsContent function:然后使用您的getStepsContent .map通过它们 .map:

steps.map(item => getStepsContent(item))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM