-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add selectionType prop to Calendar component
- Loading branch information
Showing
9 changed files
with
531 additions
and
381 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
24 changes: 24 additions & 0 deletions
24
src/components/Calendar/helpers/__test__/buildNewRangeFromValue.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/components/Calendar/helpers/__test__/isDateWithinRange.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.