【发布时间】:2021-12-24 14:57:57
【问题描述】:
我的 formik 字段存在问题,在某些情况下需要使用 FastFields,而在其他情况下需要使用 Fields。因此,我的 FormikControl,一个用于构建 formik 输入的类需要在包装器之间切换和作为包装器。我不知道该怎么做。 这是课程的一部分:
const FormikControl = (props) => {
const {
name,
type,
label,
disabled,
fullWidth,
isDynamic, // Field (true) or FastField
} = props;
const classes = useStyles();
const inputProps = {};
switch (type) {
case 'text':
return (
<>
<ComplexField name={name} isDynamic={isDynamic} {...props}> // meant to be the wrapper that switches
{({ field }) => {
return (
<TextField
{...field}
variant="standard"
label={label}
fullWidth={fullWidth}
disabled={disabled}
InputLabelProps={{
shrink: true,
}}
InputProps={inputProps}
value={field.value || ''}
/>
);
}}
</ComplexField>
<ErrorMessage error name={name} component={FormHelperText} />
</>
);
ComplexField 旨在成为灵活的包装器,需要能够基于“isDynamic”在 Field 和 FastField 之间切换。 我最好的尝试是这样的:
const ComplexField = ({ name, children, isDynamic, ...props }) => {
return isDynamic ? (
<Field name={name} props={props}>{({ field }) => children({ ...props, field })}</Field>
) : (
<FastField name={name} props={props}>{({ field }) => children({ ...props, field })}</FastField>
);
};
没用:(
【问题讨论】:
-
切换现在可以工作 - 但我面临将禁用状态传达给 FastField 的问题,或者在禁用状态更改时强制它呈现...... >.
标签: javascript reactjs material-ui formik