-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement square selection and expose prop to select selection scheme
- Loading branch information
Showing
10 changed files
with
193 additions
and
53 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ test | |
[include] | ||
|
||
[libs] | ||
declarations | ||
|
||
[options] | ||
suppress_comment= \\(.\\|\n\\)*\\flow-disable-next-line |
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
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,6 @@ | ||
// @flow | ||
/* eslint-disable */ | ||
|
||
type SelectionType = 'add' | 'remove' | ||
|
||
type SelectionSchemeType = 'linear' | 'square' |
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
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,23 @@ | ||
// @flow | ||
|
||
import differenceInHours from 'date-fns/difference_in_hours' | ||
import startOfDay from 'date-fns/start_of_day' | ||
import isAfter from 'date-fns/is_after' | ||
|
||
// Helper function that uses date-fns methods to determine if a date is between two other dates | ||
export const dateHourIsBetween = (start: Date, candidate: Date, end: Date) => | ||
differenceInHours(candidate, start) >= 0 && differenceInHours(end, candidate) >= 0 | ||
|
||
export const dateIsBetween = (start: Date, candidate: Date, end: Date): boolean => { | ||
const startOfCandidate = startOfDay(candidate) | ||
const startOfStart = startOfDay(start) | ||
const startOfEnd = startOfDay(end) | ||
|
||
return ( | ||
(startOfCandidate.getTime() === startOfStart.getTime() || isAfter(startOfCandidate, startOfStart)) && | ||
(startOfCandidate.getTime() === startOfEnd.getTime() || isAfter(startOfEnd, startOfCandidate)) | ||
) | ||
} | ||
|
||
export const timeIsBetween = (start: Date, candidate: Date, end: Date) => | ||
candidate.getHours() >= start.getHours() && candidate.getHours() <= end.getHours() |
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,7 @@ | ||
import linear from './linear' | ||
import square from './square' | ||
|
||
export default { | ||
linear, | ||
square | ||
} |
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,37 @@ | ||
// @flow | ||
|
||
import isBefore from 'date-fns/is_before' | ||
|
||
import * as dateUtils from '../date-utils' | ||
|
||
const linear = (selectionStart: ?Date, selectionEnd: ?Date, dateList: Array<Array<Date>>): Array<Date> => { | ||
let selected: Array<Date> = [] | ||
if (selectionEnd == null) { | ||
// This function is called with a null selectionEnd on `mouseup`. This is useful for catching cases | ||
// where the user just clicks on a single cell | ||
if (selectionStart) selected = [selectionStart] | ||
} else if (selectionStart) { | ||
const reverseSelection = isBefore(selectionEnd, selectionStart) | ||
// Generate a list of Dates between the start of the selection and the end of the selection | ||
// The Dates to choose from for this list are sourced from this.dates | ||
selected = dateList.reduce( | ||
(acc, dayOfTimes) => | ||
acc.concat( | ||
dayOfTimes.filter( | ||
t => | ||
selectionStart && | ||
selectionEnd && | ||
dateUtils.dateHourIsBetween( | ||
reverseSelection ? selectionEnd : selectionStart, | ||
t, | ||
reverseSelection ? selectionStart : selectionEnd | ||
) | ||
) | ||
), | ||
[] | ||
) | ||
} | ||
return selected | ||
} | ||
|
||
export default linear |
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,42 @@ | ||
// @flow | ||
|
||
import isBefore from 'date-fns/is_before' | ||
|
||
import * as dateUtils from '../date-utils' | ||
|
||
const square = (selectionStart: ?Date, selectionEnd: ?Date, dateList: Array<Array<Date>>): Array<Date> => { | ||
let selected: Array<Date> = [] | ||
if (selectionEnd == null) { | ||
// This function is called with a null selectionEnd on `mouseup`. This is useful for catching cases | ||
// where the user just clicks on a single cell | ||
if (selectionStart) selected = [selectionStart] | ||
} else if (selectionStart) { | ||
const reverseSelection = isBefore(selectionEnd, selectionStart) | ||
|
||
selected = dateList.reduce( | ||
(acc, dayOfTimes) => | ||
acc.concat( | ||
dayOfTimes.filter( | ||
t => | ||
selectionStart && | ||
selectionEnd && | ||
dateUtils.dateIsBetween( | ||
reverseSelection ? selectionEnd : selectionStart, | ||
t, | ||
reverseSelection ? selectionStart : selectionEnd | ||
) && | ||
dateUtils.timeIsBetween( | ||
reverseSelection ? selectionEnd : selectionStart, | ||
t, | ||
reverseSelection ? selectionStart : selectionEnd | ||
) | ||
) | ||
), | ||
[] | ||
) | ||
} | ||
|
||
return selected | ||
} | ||
|
||
export default square |
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
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,44 @@ | ||
import moment from 'moment' | ||
|
||
import { dateIsBetween, timeIsBetween } from '../../src/lib/date-utils' | ||
|
||
describe('dateIsBetween', () => { | ||
const now = moment().toDate() | ||
const tomorrow = moment(now) | ||
.add(1, 'day') | ||
.toDate() | ||
const yesterday = moment(now) | ||
.subtract(1, 'day') | ||
.toDate() | ||
|
||
test.each([ | ||
[[yesterday, now, tomorrow], true], | ||
[[now, yesterday, tomorrow], false], | ||
[[yesterday, tomorrow, now], false], | ||
[[now, now, now], true] | ||
])('it returns correctly', (args, expectation) => { | ||
const expectMethod = expectation ? 'toBeTruthy' : 'toBeFalsy' | ||
expect(dateIsBetween(...args))[expectMethod]() | ||
}) | ||
}) | ||
|
||
describe('timeIsBetween', () => { | ||
const hours = {} | ||
for (let i = 0; i < 23; i += 1) { | ||
hours[i] = new Date() | ||
hours[i].setHours(i) | ||
} | ||
|
||
test.each([ | ||
[[hours[1], hours[2], hours[3]], true], | ||
[[hours[5], hours[7], hours[20]], true], | ||
[[hours[5], hours[4], hours[7]], false], | ||
[[hours[5], hours[5], hours[5]], true], | ||
[[hours[5], hours[5], hours[6]], true], | ||
[[hours[5], hours[6], hours[6]], true], | ||
[[hours[5], hours[10], hours[4]], false] | ||
])('it returns correctly', (args, expectation) => { | ||
const expectMethod = expectation ? 'toBeTruthy' : 'toBeFalsy' | ||
expect(timeIsBetween(...args))[expectMethod]() | ||
}) | ||
}) |