簡體   English   中英

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

[英]Min and Max Date in Material UI DatePicker in React

我有一個條件,如果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}
/>;

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

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

現場演示

編輯 64194577/min-and-max-date-in-material-ui-datepicker-in-react

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM