我正在尝试通过使用地图索引来设置动态表单的默认值,下面是代码片段:
<Formik
initialValues={initialInputField}
onSubmit={(data) => handleSubmitForm(data)}
// validationSchema={validationSchema}
>
{({ values, handleSubmit, isSubmitting }) => (
<Form onSubmit={handleSubmit}>
<FieldArray name="processes">
{({ push, remove }) => {
return (
<div>
<Button
onClick={() => push(initialInputField.processes[0])}
className={classes.generateButton}
variant="outlined"
>
Add Process
</Button>
<div className={classes.root}>
{map(values.processes, (data, index: number) => {
return (
<div key={index}>
<Field
label="Process"
value={index} // add index as process value
name={`processes[${index}].process`}
component={Input}
/>
<IconButton onClick={() => remove(index)}>
<RemoveIcon />
</IconButton>
</div>
);
})}
</div>
</div>
);
}}
</FieldArray>
<Button
className={classes.generateButton}
variant="outlined"
type="submit"
>
Generate
</Button>
</Form>
)}
</Formik>
这是我的自定义组件的代码片段:
const Input = ({ field, value, label, form: { errors } }: any) => {
return (
<>
<TextField {...field} label={label} value={value} variant="filled" />
</>
);
};
问题是,当我提交表单时,虽然表单已经更改,但 process 的所有值仍然是 "" 这是我的初始值
示例:(进程填写正确的值)
(提交表单后流程仍然为空)