有没有办法在 reactjs 中更改 react-datepicker 的月份或年份时选择日期?

Is there any way to keep date selected on changing Month or Year of the react-datepicker in reactjs?

目前,在 react-datepicker 中,我在客人详细信息表单中为 DOB 使用月份选择器和年份选择器。

发生的事情是:- 所选日期在日历上突出显示,这很好。

我需要实现的是:- 当我在日历上更改 Month/Year 时,我想将所选日期更新为与之前更新的 Month/Year.

相同的日期

有任何可能请帮助我。

我找不到解决方案。

以下代码是我用于当前 react-datepicker 的代码:

    <DatePicker
        className="date-field ub-l"
        openToDate={new Date("1993/09/28")}
        selected={this.state.date}
        onSelect={this.handleChange}
        onChange={this.handleChange} //only when value has changed
        maxDate={new Date()}
        placeholderText={this.props.placeholder}
        disabledKeyboardNavigation={true}
        showMonthDropdown={true}
        showYearDropdown={true}
        name={this.props.name}
        disabledNavigation
        inputProps={{readOnly: true}}
        dropdownMode="select"
        autocomplete={false}
    />

如果我更改 Month/Year 是否可以在选择任何日期之前使用更新后的 Month/Year 自动更新所选日期?

您正在寻找 adjustDateOnChange 道具。它会自动执行您要查找的内容

<DatePicker
  selected={selectedDate}
  onChange={date => changeDate(date)}
  dropdownMode="select"
  showMonthDropdown
  showYearDropdown
  adjustDateOnChange
/>

Codesandbox