javascript - 我需要使用 useformik 在复选框的字段中设置禁用或启用
问题描述
我需要设置,如果未选中复选框,则日期字段保持禁用如果选中复选框意味着真实日期出现日期字段正在禁用,但是当我选中禁用的复选框并且我检查真假条件启用时功能不起作用我不确定我错过了什么需要在正确的验证中设置此失效我正在使用 useFormik
const validationSchema = Yup.object().shape({
list: Yup.array().of(Yup.string()).min(1, "Required!").required("Required!"),
isCheck: Yup.boolean(),
startDate: Yup.string().when("isCheck", {
is: true,
then: Yup.string().required("Required!"),
otherwise: Yup.boolean,
}),
isCheck: Yup.boolean(),
endDate: Yup.string().when("isCheck", {
is: true,
then: Yup.string().required("Required!"),
otherwise: Yup.boolean,
}),
});
const ruleForm = useFormik({
initialValues: {
// Status: "que",
},
validationSchema,
onSubmit: (values) => {
alert(JSON.stringify(values, null, 2));
deltaChange(values);
},
});
<Box className={classes.formText}>
<Checkbox
checked={ruleForm.values.isCheck}
onChange={ruleForm.handleChange}
color="primary"
inputProps={{ "aria-label": "secondary checkbox" }}
/>
<Typography variant="subtitle2" noWrap={true}>
<Box fontWeight="fontWeightBold"> By Update Date : </Box>
</Typography>
</Box>
<Grid container display="flex" spacing={4}>
<Grid item xs={6}>
<TextField
id="outlined-select-currency"
label="Start Date"
variant="outlined"
disabled={!ruleForm.values.isCheck}
size="small"
type="date"
InputLabelProps={{
shrink: true,
}}
onChange={ruleForm.handleChange}
value={ruleForm.values.startDate}
error={ruleForm.errors.startDate}
helperText={ruleForm.errors.startDate}
name="startDate"
></TextField>
</Grid>
<Grid item xs={6}>
<TextField
id="outlined-select-currency"
label="End Date"
variant="outlined"
size="small"
// onChange={(e) => setDate(e)}
type="date"
disabled={!ruleForm.values.isCheck}
InputLabelProps={{
shrink: true,
}}
onChange={ruleForm.handleChange}
value={ruleForm.values.endDate}
error={ruleForm.errors.endDate}
helperText={ruleForm.errors.endDate}
name="endDate"
></TextField>
</Grid>
</Grid>
解决方案
推荐阅读
- reactjs - 使用 Git 部署 Laravel React 应用程序的正确方法是什么?
- java - Kotlin 问题“ExpandableRecyclerAdapter 类需要一个类型参数”
- php - 使用 Levenshtein 距离重新排列单词
- assembly - 如何解决 TASM 中的“假设数据段是 32 位”
- firebase - 未找到 Travis 部署 Firebase 托管文件
- javascript - Style.backgroundColor 或 style.background 没有执行它的功能
- c++ - godot的NetSocket是如何创建实例的?
- python - python url请求响应解码
- mongodb - 本地数据库 + 服务器数据库 - React Native
- r - 错误:更改堆积条形图 ggplot2 中的颜色