Skip to content

Commit

Permalink
Added a specific css class to excluded dates (#1849)
Browse files Browse the repository at this point in the history
Prior to this change, dates that were passed in via the excludedDates array prop had no specific identifier class on the day elements in the datepicker - they used the same disabled class that was used for dates before the minDate/after the maxDate, which makes it difficult to apply custom styling directly to excluded dates
  • Loading branch information
Sustenance authored and martijnrusschen committed Aug 16, 2019
1 parent 6a3280e commit 988ec69
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/date_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ export function isDayDisabled(
);
}

export function isDayExcluded(day, { excludeDates } = {}) {
return (
(excludeDates &&
excludeDates.some(excludeDate => isSameDay(day, excludeDate))) ||
false
);
}

export function isMonthDisabled(
month,
{ minDate, maxDate, excludeDates, includeDates, filterDate } = {}
Expand Down
4 changes: 4 additions & 0 deletions src/day.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
newDate,
isSameDay,
isDayDisabled,
isDayExcluded,
isDayInRange,
isEqual,
isBefore,
Expand Down Expand Up @@ -58,6 +59,8 @@ export default class Day extends React.Component {

isDisabled = () => isDayDisabled(this.props.day, this.props);

isExcluded = () => isDayExcluded(this.props.day, this.props);

getHighLightedClass = defaultClassName => {
const { day, highlightDates } = this.props;

Expand Down Expand Up @@ -177,6 +180,7 @@ export default class Day extends React.Component {
"react-datepicker__day--" + getDayOfWeekCode(this.props.day),
{
"react-datepicker__day--disabled": this.isDisabled(),
"react-datepicker__day--excluded": this.isExcluded(),
"react-datepicker__day--selected": this.isSameDay(this.props.selected),
"react-datepicker__day--keyboard-selected": this.isKeyboardSelected(),
"react-datepicker__day--range-start": this.isRangeStart(),
Expand Down
21 changes: 21 additions & 0 deletions test/date_utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isSameMonth,
isSameYear,
isDayDisabled,
isDayExcluded,
isMonthDisabled,
monthDisabledBefore,
monthDisabledAfter,
Expand Down Expand Up @@ -197,6 +198,26 @@ describe("date_utils", function() {
});
});

describe("isDayExcluded", function() {
it("should not be excluded by default", () => {
const day = newDate();
expect(isDayExcluded(day)).to.be.false;
});

it("should be excluded if in excluded dates", () => {
const day = newDate();
expect(isDayExcluded(day, { excludeDates: [day] })).to.be.true;
});

it("should not be excluded if not in excluded dates", () => {
const day = newDate();
const excludedDay = newDate();
const currentMonth = excludedDay.getMonth();
excludedDay.setMonth(currentMonth === 11 ? 0 : currentMonth + 1);
expect(isDayExcluded(day, { excludeDates: [] }));
});
});

describe("isMonthDisabled", function() {
it("should be enabled by default", () => {
const day = newDate();
Expand Down

0 comments on commit 988ec69

Please sign in to comment.