React 中 Material UI DatePicker 中的最小和最大日期

Jos*_*eph 7 reactjs material-ui react-hooks

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

请在此处查看我的代码。

<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}
/>;
Run Code Online (Sandbox Code Playgroud)

Nea*_*arl 10

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

<KeyboardDatePicker
  minDate={!access ? firstDay : undefined}
  maxDate={!access ? lastDay : undefined}
  {...}
/>
Run Code Online (Sandbox Code Playgroud)

现场演示

编辑 64194577/min-and-max-date-in-material-ui-datepicker-in-react