当我将DatePicker的初始值设置为null时,它会阻止键盘输入。如何避免呢?
沙盒示例https://codesandbox.io/embed/silly-black-y0dn7

function KeyboardDatePicker(props) {
  const [selectedDate, handleDateChange] = useState(null);

  return (
    <MuiPickersUtilsProvider utils={MomentUtils}>
      <DatePicker
        keyboard
        clearable
        label="Masked input"
        format="DD.MM.YYYY"
        mask={value =>
          value
            ? [/\d/, /\d/, ".", /\d/, /\d/, ".", /\d/, /\d/, /\d/, /\d/]
            : []
        }
        value={moment(selectedDate)}
        onChange={date => {
          handleDateChange(date);
        }}
        disableOpenOnEnter
        animateYearScrolling={false}
      />
    </MuiPickersUtilsProvider>
  );
}

最佳答案

尝试从有效的日期对象开始,而不是像这样的null

  const [selectedDate, handleDateChange] = useState(new Date());

或者,如果您希望以空白日期开头,则应该可以使用:
  const [selectedDate, handleDateChange] = useState(null);
...
        value={selectedDate ? moment(selectedDate) : null}


10-06 04:18