我们在日历实现中使用react-datepicker.显示的默认月份由monthsShowd datepicker控制
组件属性.当我们说2时,它表示下个月的本月.
我正在使用如下的reat DatePicker
<DatePicker
customInput={<CustomInput disableDateSelection={this.props.dateProps.disableDateSelection} />}
className="w-dtsr-date-picker"
selected={this.state.startDate} //called when the date is clicked
onChange={this.handleChange} //called only when the value is chnaged
monthsShown={2} //number of months to be shown
minDate={this.props.dateProps.minDate} //min days to be shown in the calendar
maxDate={this.props.dateProps.maxDate} //max days to be shown in the calendar
onChangeRaw={this.handleDateChangeRaw} //preventing the user from manually entering the dates or text in the input text Box
dayClassName={date => date.getTime() === new Date(this.props.dateProps.defaultDate).getTime() && this.props.dateProps.disableDefaultDate ? 'random' : undefined}
//dayClassName - to disable the proposed forecast dates or actual dates if user has already selected/proposed the same forecast/actual dates
disabled={this.props.dateProps.disableDateSelection}
/>
最佳答案
不幸的是,在react-datepicker存储库中查看该组件的源代码,似乎它将始终显示当月和此后的几个月,这由monthShown属性控制.除了派生github存储库并自己添加功能(或提交功能请求)以外,别无其他办法让它做您想要的事情.有问题的问题:
原文链接:https://www.f2er.com/js/531263.htmlvar monthList = [];
for (var i = 0; i < this.props.monthsShown; ++i) {
var monthsToAdd = i - this.props.monthSelectedIn;
var monthDate = addMonths(this.state.date,monthsToAdd);
var monthKey = `month-${i}`;
monthsShown属性控制显示的月份,默认值为1.为了使它能够执行您想要的操作,可以添加一个标志来反转月份的增加,而改为减去.