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

Fix function modifiers #302

Merged
merged 2 commits into from
Apr 20, 2017
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
3 changes: 2 additions & 1 deletion examples/src/examples/Restricted.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../../../src/style.css';

const start = new Date(2015, 3, 1, 0, 0);
const end = new Date(2015, 10, 30, 23, 59);
const sundays = day => day.getDay() === 0;

export default function Restricted() {
return (
Expand All @@ -14,7 +15,7 @@ export default function Restricted() {
initialMonth={start}
fromMonth={start}
toMonth={end}
disabledDays={[{ before: start }, { after: end }]}
disabledDays={[{ before: start }, { after: end }, sundays]}
onDayClick={(day, { disabled }) => {
if (!disabled) {
console.log(day.toLocaleDateString());
Expand Down
3 changes: 3 additions & 0 deletions src/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export function getModifiersForDay(d, modifiersObj = {}) {
if (day.before) {
return d < day.before;
}
if (typeof day === 'function' && day(d)) {
return true;
}
return false;
})
) {
Expand Down
21 changes: 21 additions & 0 deletions test/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ describe('Helpers', () => {
expect(modifiers).to.have.length(1);
expect(modifiers.indexOf('foo')).to.equal(0);
});
it('work with a mix of functions and days', () => {
const mixedModifiers = {
foo: [
{ before: new Date(2015, 8, 15) },
day => day.getTime() === new Date(2015, 8, 17).getTime(),
],
};
const modifiers = Helpers.getModifiersForDay(
new Date(2015, 8, 10),
mixedModifiers
);
expect(modifiers).to.have.length(1);
expect(modifiers.indexOf('foo')).to.equal(0);

const modifiers2 = Helpers.getModifiersForDay(
new Date(2015, 8, 17),
mixedModifiers
);
expect(modifiers2).to.have.length(1);
expect(modifiers2.indexOf('foo')).to.equal(0);
});
it('works even without modifiers', () => {
const modifiers = Helpers.getModifiersForDay(new Date(2015, 8, 19));
expect(modifiers).to.have.length(0);
Expand Down