下面的代码将有助于禁用,
选定的未来日子
禁用当天之前的所有天
周末(周六和周日)
方式一:
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
]}
/>