Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excluded dates message #4437

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs-site/src/components/Examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Locale from "../../examples/locale";
import LocaleWithTime from "../../examples/localeWithTime";
import LocaleWithoutGlobalVariable from "../../examples/localeWithoutGlobalVariable";
import ExcludeDates from "../../examples/excludeDates";
import ExcludedWithMessage from "../../examples/excludeDatesWithMessage";
import ExcludeDateIntervals from "../../examples/excludeDateIntervals";
import ExcludeDatesMonthPicker from "../../examples/excludeDatesMonthPicker";
import HighlightDates from "../../examples/highlightDates";
Expand Down Expand Up @@ -271,6 +272,10 @@ export default class exampleComponents extends React.Component {
title: "Exclude dates",
component: ExcludeDates,
},
{
title: "Exclude dates with message",
component: ExcludedWithMessage,
},
{
title: "Exclude date intervals",
component: ExcludeDateIntervals,
Expand Down
14 changes: 14 additions & 0 deletions docs-site/src/examples/excludeDatesWithMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
() => {
const [startDate, setStartDate] = useState(new Date());
return (
<DatePicker
selected={startDate}
onChange={(date) => setStartDate(date)}
excludeDates={[
{ date: new Date(), message: "Today is excluded" },
{ date: subDays(new Date(), 1), message: "This day is excluded" },
]}
placeholderText="Select a date other than today or yesterday"
/>
);
};
8 changes: 6 additions & 2 deletions src/date_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ export function isDayDisabled(
return (
isOutOfBounds(day, { minDate, maxDate }) ||
(excludeDates &&
excludeDates.some((excludeDate) => isSameDay(day, excludeDate))) ||
excludeDates.some((excludeDate) =>
martijnrusschen marked this conversation as resolved.
Show resolved Hide resolved
isSameDay(day, excludeDate.date ? excludeDate.date : excludeDate),
)) ||
(excludeDateIntervals &&
excludeDateIntervals.some(({ start, end }) =>
isWithinInterval(day, { start, end }),
Expand All @@ -436,7 +438,9 @@ export function isDayExcluded(
}
return (
(excludeDates &&
excludeDates.some((excludeDate) => isSameDay(day, excludeDate))) ||
excludeDates.some((excludeDate) =>
isSameDay(day, excludeDate.date ? excludeDate.date : excludeDate),
)) ||
false
);
}
Expand Down
21 changes: 15 additions & 6 deletions src/day.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default class Day extends React.Component {
PropTypes.shape({ locale: PropTypes.object }),
]),
calendarStartDay: PropTypes.number,
excludeDates: PropTypes.arrayOf(PropTypes.instanceOf(Date)),
};

componentDidMount() {
Expand Down Expand Up @@ -326,14 +327,22 @@ export default class Day extends React.Component {

// A function to return the holiday's name as title's content
getTitle = () => {
const { day, holidays = new Map() } = this.props;
const { day, holidays = new Map(), excludeDates } = this.props;
const compareDt = formatDate(day, "MM.dd.yyyy");
const titles = [];
if (holidays.has(compareDt)) {
return holidays.get(compareDt).holidayNames.length > 0
? holidays.get(compareDt).holidayNames.join(", ")
: "";
titles.push(...holidays.get(compareDt).holidayNames);
}
return "";
if (this.isExcluded()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code could be made a lot simpler if you just appended all the titles to the output if they can be fetched and have a single flow.

Eg:

titles = []
titles.append(holidays.get(compareDt).holidayNames)
titles.append(excludeDates?.filter(filterFnHere))
return titles.join(", ")

◽ Simplify Code

Image of John G John G

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Thanks for your suggestion

titles.push(
excludeDates
?.filter((excludeDate) =>
isSameDay(excludeDate.date ? excludeDate.date : excludeDate, day),
)
.map((excludeDate) => excludeDate.message),
);
}
return titles.join(", ");
};

getTabIndex = (selected, preSelection) => {
Expand Down Expand Up @@ -423,7 +432,7 @@ export default class Day extends React.Component {
>
{this.renderDayContents()}
{this.getTitle() !== "" && (
<span className="holiday-overlay">{this.getTitle()}</span>
<span className="overlay">{this.getTitle()}</span>
)}
</div>
);
Expand Down
21 changes: 19 additions & 2 deletions src/stylesheets/datepicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@
background-color: $datepicker__holidays-color;
color: #fff;

.holiday-overlay {
.overlay {
position: absolute;
bottom: 100%;
left: 50%;
Expand All @@ -445,7 +445,7 @@
background-color: darken($datepicker__holidays-color, 10%);
}

&:hover .holiday-overlay {
&:hover .overlay {
visibility: visible;
opacity: 1;
}
Expand Down Expand Up @@ -492,6 +492,23 @@
&:hover {
background-color: transparent;
}

.overlay {
position: absolute;
bottom: 70%;
left: 50%;
transform: translateX(-50%);
background-color: #333;
color: #fff;
padding: 4px;
border-radius: 4px;
white-space: nowrap;
visibility: hidden;
opacity: 0;
transition:
visibility 0s,
opacity 0.3s ease-in-out;
}
}
}

Expand Down
49 changes: 49 additions & 0 deletions test/exclude_dates.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from "react";
import { render } from "@testing-library/react";
import DatePicker from "../src/index.jsx";
import { subDays } from "date-fns";

describe("DatePicker", () => {
const excludeDates = [new Date(), subDays(new Date(), 1)];
const excludeDatesWithMessages = [
{ date: subDays(new Date(), 1), message: "This day is excluded" },
{ date: new Date(), message: "Today is excluded" },
];

it("should disable dates specified in excludeDates props", () => {
const { container: datePicker } = render(
<DatePicker
open
excludeDates={excludeDates}
placeholderText="Select a date other than today or yesterday"
/>,
);

const disabledTimeItems = datePicker.querySelectorAll(
".react-datepicker__day--excluded",
);
expect(disabledTimeItems.length).toBe(excludeDates.length);
});

it("should disable dates specified in excludeDates props and should show the reason", () => {
const { container: datePicker } = render(
<DatePicker
open
excludeDates={excludeDatesWithMessages}
placeholderText="Select a date other than today or yesterday"
/>,
);

const disabledTimeItems = datePicker.querySelectorAll(
".react-datepicker__day--excluded",
);

expect(disabledTimeItems.length).toBe(excludeDatesWithMessages.length);
expect(disabledTimeItems[0].getAttribute("title")).toBe(
"This day is excluded",
);
expect(disabledTimeItems[1].getAttribute("title")).toBe(
"Today is excluded",
);
});
});
Loading