Skip to content

Commit

Permalink
feat: add selectionType prop to Calendar component
Browse files Browse the repository at this point in the history
  • Loading branch information
wildergd committed May 19, 2020
1 parent e1606ba commit c4004e6
Show file tree
Hide file tree
Showing 9 changed files with 531 additions and 381 deletions.
3 changes: 2 additions & 1 deletion src/components/Calendar/context.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';

export const { Provider, Consumer } = React.createContext();
export const CalendarContext = React.createContext();
export const { Provider, Consumer } = CalendarContext;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import buildNewRangeFromValue from '../buildNewRangeFromValue';

describe('buildNewRangeFromValue', () => {
it('should return array with single date', () => {
const value = new Date();
const ranges = [undefined, null, []];
ranges.forEach(range => {
expect(buildNewRangeFromValue(value, range)).toEqual([value]);
});
});
it('should return array with two dates', () => {
const date1 = new Date(2019, 2, 1);
const date2 = new Date(2019, 21, 1);
const range = [date1];
expect(buildNewRangeFromValue(date2, range)).toEqual([date1, date2]);
});
it('should return array with two dates and date3 as first date', () => {
const date1 = new Date(2019, 0, 2);
const date2 = new Date(2019, 0, 21);
const date3 = new Date(2019, 0, 15);
const range = [date1, date2];
expect(buildNewRangeFromValue(date3, range)).toEqual([date3, date2]);
});
});
17 changes: 17 additions & 0 deletions src/components/Calendar/helpers/__test__/isDateWithinRange.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import isDateWithinRange from '../isDateWithinRange';

describe('isDateWithinRange', () => {
it('should return false', () => {
const range = [new Date(2019, 2, 1, 0, 0, 0, 600), new Date(2019, 15, 1, 23, 12, 34, 600)];
expect(isDateWithinRange(null, range)).toBe(false);
expect(isDateWithinRange(undefined, range)).toBe(false);
expect(isDateWithinRange(new Date(2019, 1, 1), range)).toBe(false);
expect(isDateWithinRange(new Date(2019, 18, 1), range)).toBe(false);
});
it('should return true', () => {
const range = [new Date(2019, 2, 1), new Date(2019, 15, 1)];
expect(isDateWithinRange(new Date(2019, 2, 1), range)).toBe(true);
expect(isDateWithinRange(new Date(2019, 13, 1), range)).toBe(true);
expect(isDateWithinRange(new Date(2019, 15, 1), range)).toBe(true);
});
});
27 changes: 27 additions & 0 deletions src/components/Calendar/helpers/buildNewRangeFromValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import isDateWithinRange from './isDateWithinRange';
import compareDates from './compareDates';

let rangeIndex = 0;

export default function buildNewRangeFromValue(value, currentRange) {
if (!currentRange || currentRange.length === 0) return [value];

const [rangeStart, rangeEnd] = currentRange;

if (!rangeEnd) {
if (compareDates(value, rangeStart) >= 0) return [rangeStart, value];
return [value, rangeStart];
}

if (isDateWithinRange(value, currentRange)) {
if (rangeIndex === 0) {
rangeIndex = 1;
return [value, rangeEnd];
}

rangeIndex = 0;
return [rangeStart, value];
}

return [value];
}
59 changes: 20 additions & 39 deletions src/components/Calendar/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
import addDays from './addDays';
import addMonths from './addMonths';
import formatDate from './formatDate';
import getFirstDayMonth from './getFirstDayMonth';
import getFormattedMonth from './getFormattedMonth';
import getLastDayMonth from './getLastDayMonth';
import getYearsRange from './getYearsRange';
import isSameDay from './isSameDay';
import normalizeDate from './normalizeDate';
import getFormattedDayName from './getFormattedDayName';
import compareDates from './compareDates';
import isSameMonth from './isSameMonth';
import isSameYear from './isSameYear';
import getSign from './getSign';
import getCalendarBounds from './getCalendarBounds';
import isDateBelowLimit from './isDateBelowLimit';
import isDateBeyondLimit from './isDateBeyondLimit';
import getNextFocusedDate from './getNextFocusedDate';

export {
addDays,
addMonths,
formatDate,
getFirstDayMonth,
getFormattedMonth,
getLastDayMonth,
getYearsRange,
isSameDay,
normalizeDate,
getFormattedDayName,
compareDates,
isSameMonth,
isSameYear,
getSign,
getCalendarBounds,
isDateBelowLimit,
isDateBeyondLimit,
getNextFocusedDate,
};
export { default as addDays } from './addDays';
export { default as addMonths } from './addMonths';
export { default as formatDate } from './formatDate';
export { default as getFirstDayMonth } from './getFirstDayMonth';
export { default as getFormattedMonth } from './getFormattedMonth';
export { default as getLastDayMonth } from './getLastDayMonth';
export { default as getYearsRange } from './getYearsRange';
export { default as isSameDay } from './isSameDay';
export { default as normalizeDate } from './normalizeDate';
export { default as getFormattedDayName } from './getFormattedDayName';
export { default as compareDates } from './compareDates';
export { default as isSameMonth } from './isSameMonth';
export { default as isSameYear } from './isSameYear';
export { default as getSign } from './getSign';
export { default as getCalendarBounds } from './getCalendarBounds';
export { default as isDateBelowLimit } from './isDateBelowLimit';
export { default as isDateBeyondLimit } from './isDateBeyondLimit';
export { default as getNextFocusedDate } from './getNextFocusedDate';
export { default as isDateWithinRange } from './isDateWithinRange';
export { default as buildNewRangeFromValue } from './buildNewRangeFromValue';
8 changes: 8 additions & 0 deletions src/components/Calendar/helpers/isDateWithinRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import compareDates from './compareDates';

export default function isDateWithinRange(date, range) {
if (!date) return false;
const [rangeStart, rangeEnd] = range;

return compareDates(date, rangeStart) >= 0 && compareDates(date, rangeEnd) <= 0;
}
Loading

0 comments on commit c4004e6

Please sign in to comment.