【问题标题】:Min and Max Date in Material UI DatePicker in ReactReact 中 Material UI DatePicker 中的最小和最大日期
【发布时间】:2021-01-19 11:45:51
【问题描述】:

我有一个条件,如果access 的值为 1,那么选择日期没有限制。但是,如果access 的值不是 1,那么您只能选择今天。我的问题是,即使access 的值为1,我也无法选择其他月份。我只能从月份内选择。我该如何解决这个问题?

请在此处查看我的代码。

<KeyboardDatePicker
  minDate={access !== 1 ? new Date() : ""}
  maxDate={access !== 1 ? new Date() : ""}
  fullWidth
  InputLabelProps={{ shrink: true }}
  inputVariant="outlined"
  id="date-picker-dialog"
  label="Select Date"
  format="MM/dd/yyyy"
  clearable
  value={values.start_date}
  onChange={(val) => {
    setFieldValue("start_date", val);
  }}
  onBlur={handleBlur}
  helperText={touched.start_date ? errors.start_date : ""}
  error={touched.start_date && Boolean(errors.start_date)}
  TextFieldComponent={TextFieldComponent}
/>;

【问题讨论】:

    标签: reactjs material-ui react-hooks react-material


    【解决方案1】:

    如果您不想添加最小/最大约束,请使用 undefined 而不是空字符串。

    <KeyboardDatePicker
      minDate={!access ? firstDay : undefined}
      maxDate={!access ? lastDay : undefined}
      {...}
    />
    

    现场演示

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多