在 React-Day-Picker 中禁用所有未来的日子?
Disable all future days in React-Day-Picker?
我有一个使用 react-day-picker 的日期范围日历。除了我无法从选择中禁用 "all" 未来的日子之外,它的效果非常好。 api 文档显示您可以输入一天或多天但不能输入所有未来的日子?
<DayPicker
disabledDays={new Date()} // how to disable all days after today?
modifiers={{
sunday: day => day.getDay() === 0,
firstOfMonth: day => day.getDate() === 1
}}
onDayClick={this.handleDayClick}
onDayMouseEnter={this.handleDayMouseEnter}
/>
看起来 disabledDays
prop 也可以是一个函数:(day: Date) ⇒ boolean
所以你应该可以这样做:
disabledDays={day => day > (new Date())}
稍微好一点的版本是
const today = new Date();
<DayPicker
disabledDays={{ after: today }}
selectedDays={new Date()}
/>
下面的代码将有助于禁用,
selected future days
disable all days before the current day
weekends (Saturdays and Sundays)
方式一:
API 响应是一个对象数组。
import DayPicker from "react-day-picker";
import moment from "moment";
import "react-day-picker/lib/style.css";
const selectedDaysToDisable = [];
const arr = [
{ id: 1, date: "Sep 28 2021 10:00:00 AM" },
{ id: 2, date: "Sep 29 2021 08:00:00 AM" },
{ id: 3, date: "Oct 5 2021 08:00:00 AM" }
];
arr.map((val) => {
return selectedDaysToDisable.push(dateFormat(val.date));
});
function dateFormat(date) {
return new Date(
moment(date).format("MM") +
"/" +
moment(date).format("DD") +
"/" +
moment(date).format("YYYY")
);
}
<DayPicker
disabledDays={[
{ before: new Date() },
{ daysOfWeek: [0, 6] },
...selectedDaysToDisable
]}
/>
方式二:
简单理解
const selectedDaysToDisable = [new Date(2021, 8, 28), new Date(2021, 8, 23), new Date(2021, 8, 29)];
<DayPicker
disabledDays={[
{ before: new Date() },
{ daysOfWeek: [0, 6] },
...selectedDaysToDisable
]}
/>
我有一个使用 react-day-picker 的日期范围日历。除了我无法从选择中禁用 "all" 未来的日子之外,它的效果非常好。 api 文档显示您可以输入一天或多天但不能输入所有未来的日子?
<DayPicker
disabledDays={new Date()} // how to disable all days after today?
modifiers={{
sunday: day => day.getDay() === 0,
firstOfMonth: day => day.getDate() === 1
}}
onDayClick={this.handleDayClick}
onDayMouseEnter={this.handleDayMouseEnter}
/>
看起来 disabledDays
prop 也可以是一个函数:(day: Date) ⇒ boolean
所以你应该可以这样做:
disabledDays={day => day > (new Date())}
稍微好一点的版本是
const today = new Date();
<DayPicker
disabledDays={{ after: today }}
selectedDays={new Date()}
/>
下面的代码将有助于禁用,
selected future days
disable all days before the current day
weekends (Saturdays and Sundays)
方式一:
API 响应是一个对象数组。
import DayPicker from "react-day-picker";
import moment from "moment";
import "react-day-picker/lib/style.css";
const selectedDaysToDisable = [];
const arr = [
{ id: 1, date: "Sep 28 2021 10:00:00 AM" },
{ id: 2, date: "Sep 29 2021 08:00:00 AM" },
{ id: 3, date: "Oct 5 2021 08:00:00 AM" }
];
arr.map((val) => {
return selectedDaysToDisable.push(dateFormat(val.date));
});
function dateFormat(date) {
return new Date(
moment(date).format("MM") +
"/" +
moment(date).format("DD") +
"/" +
moment(date).format("YYYY")
);
}
<DayPicker
disabledDays={[
{ before: new Date() },
{ daysOfWeek: [0, 6] },
...selectedDaysToDisable
]}
/>
方式二:
简单理解
const selectedDaysToDisable = [new Date(2021, 8, 28), new Date(2021, 8, 23), new Date(2021, 8, 29)];
<DayPicker
disabledDays={[
{ before: new Date() },
{ daysOfWeek: [0, 6] },
...selectedDaysToDisable
]}
/>