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

Month range feature #189

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/.stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import InfiniteCalendar, {
withKeyboardSupport,
withMultipleDates,
withRange,
withMonthRange,
} from '../';
import styles from './stories.scss';

Expand Down Expand Up @@ -65,6 +66,22 @@ storiesOf('Higher Order Components', module)
Component={withRange(withKeyboardSupport(Calendar))}
/>
))
.add('Month Range selection', () => (
<InfiniteCalendar
selected={{
start: subMonths(new Date(), 1),
end: addMonths(new Date(), 1),
}}
display={'years'}
displayOptions={{
showHeader: false,
hideYearsOnSelect: false,
}}
minDate={subMonths(new Date(), 10)}
maxDate={addMonths(new Date(), 10)}
Component={withMonthRange(Calendar)}
/>
))
.add('Multiple date selection', () => {
return (
<InfiniteCalendar
Expand Down
11 changes: 11 additions & 0 deletions src/Calendar/Range/Range.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@mixin circle($size) {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: $size - 4px;
height: $size - 4px;
margin-top: -0.5 * ($size - 4px);
margin-left: -0.5 * ($size - 4px);
border-radius: 50%;
}
15 changes: 15 additions & 0 deletions src/Calendar/Range/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {getSortedDate} from '../../utils'

export const EVENT_TYPE = {
END: 3,
HOVER: 2,
START: 1,
};

export function getSortedSelection({start, end}) {
return getSortedDate(start, end);
}

export function getInitialDate({selected}) {
return selected && selected.start || new Date();
}
110 changes: 110 additions & 0 deletions src/Calendar/withMonthRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import min from 'date-fns/min';
import max from 'date-fns/max';
import startOfMonth from 'date-fns/start_of_month';
import endOfMonth from 'date-fns/end_of_month';
import {compose, withProps, withState} from 'recompose';
import {withDefaultProps} from './';
import {withImmutableProps} from '../utils';
import {EVENT_TYPE, getInitialDate, getSortedSelection} from './Range';

let isTouchDevice = false;

export const withMonthRange = compose(
withDefaultProps,
withState('scrollDate', 'setScrollDate', getInitialDate),
withState('selectionStart', 'setSelectionStart', null),
withImmutableProps(({
YearsComponent,
}) => ({
YearsComponent: YearsComponent,
})),
withProps(({passThrough, selected, ...props}) => ({
/* eslint-disable sort-keys */
passThrough: {
...passThrough,
Years: {
onSelect: (date) => handleSelect(date, {selected, ...props}),
handlers: {
onMouseOver: !isTouchDevice && props.selectionStart
? (e) => handleMouseOver(e, {selected, ...props})
: null,
},
},
},
selected: {
start: selected && selected.start,
end: selected && selected.end,
},
})),
);

function handleSelect(date, {onSelect, selected, selectionStart, setSelectionStart, min, max, minDate, maxDate}) {
if (selectionStart) {
onSelect({
eventType: EVENT_TYPE.END,
...getMonthRangeDate({
start: selectionStart,
end: date,
minSelected: minDate,
maxSelected: maxDate,
minScrolled: min,
maxScrolled: max,
}),
});
setSelectionStart(null);
} else {
onSelect({
eventType: EVENT_TYPE.START,
...getMonthRangeDate({
start: date,
end: date,
minSelected: minDate,
maxSelected: maxDate,
minScrolled: min,
maxScrolled: max,
}),
});
setSelectionStart(date);
}
}

function handleMouseOver(e, {onSelect, selectionStart}) {
e.stopPropagation();
const month = e.target.getAttribute('data-month');
if (!month) { return; }
onSelect({
eventType: EVENT_TYPE.HOVER,
...getMonthRangeDate({
start: selectionStart,
end: month,
}),
});
}

function getMonthRangeDate({start, end, minSelected, maxSelected, minScrolled, maxScrolled}) {
const sortedDate = getSortedSelection({start, end});
const compareStartDate = [];
const compareEndDate = [];
if (sortedDate.start) {
compareStartDate.push(sortedDate.start, startOfMonth(sortedDate.start));
minScrolled && compareStartDate.push(minScrolled);
minSelected && compareStartDate.push(minSelected);
}
if (sortedDate.end) {
compareEndDate.push(endOfMonth(sortedDate.end));
maxScrolled && compareEndDate.push(maxScrolled);
maxSelected && compareEndDate.push(maxSelected);
}
return {
start: compareStartDate.length > 0 ? max(...compareStartDate) : sortedDate.start,
end: compareEndDate.length > 0 ? min(...compareEndDate) : sortedDate.end,
};
}

if (typeof window !== 'undefined') {
window.addEventListener('touchstart', function onTouch() {
isTouchDevice = true;

window.removeEventListener('touchstart', onTouch, false);
});
}
19 changes: 4 additions & 15 deletions src/Day/Day.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
@import "../variables";

@mixin circle() {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: $rowHeight - 4px;
height: $rowHeight - 4px;
margin-top: -0.5 * ($rowHeight - 4px);
margin-left: -0.5 * ($rowHeight - 4px);
border-radius: 50%;
}
@import "../Calendar/Range/Range";

.root {
display: inline-block;
Expand All @@ -31,7 +20,7 @@
z-index: 1;

&:before {
@include circle();
@include circle($rowHeight);

background-color: $cellHoverBg;
z-index: -1;
Expand Down Expand Up @@ -63,7 +52,7 @@
}

&:before {
@include circle();
@include circle($rowHeight);
box-shadow: inset 0 0 0 1px;
z-index: -1;
}
Expand All @@ -84,7 +73,7 @@
}

.selection {
@include circle();
@include circle($rowHeight);
line-height: $rowHeight;
z-index: 2;

Expand Down
82 changes: 81 additions & 1 deletion src/Years/Years.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@import "../variables";
@import "../Calendar/Range/Range";

$cellSize: 44px;

.root {
position: absolute;
Expand Down Expand Up @@ -41,7 +44,6 @@
}

.year {
$cellSize: 44px;

display: flex;
padding: 0 20px;
Expand Down Expand Up @@ -91,6 +93,7 @@
justify-content: center;
list-style: none;
border-radius: 50%;
margin-bottom: 2px;
box-sizing: border-box;

color: #444;
Expand All @@ -106,6 +109,12 @@
background-color: blue;
color: #FFF !important;
border: 0;

.selection {
@include circle($cellSize);
line-height: $cellSize;
z-index: 2;
}
}
&.disabled {
cursor: not-allowed;
Expand Down Expand Up @@ -159,3 +168,74 @@
padding-bottom: $spacing;
}
}

/*
* Range selection styles
*/
.range.selected {
&.start, &.end {
&:after {
content: '';
position: absolute;
top: 50%;
width: 50%;
height: $cellSize;
margin-top: -0.5 * $cellSize;
background-color: #559fff;
}
}

&.disabled {
background-color: #EEE !important;
.selection.selection {
background-color: #EEE !important;
color: #FFF;
}
&:after {
background-color: #EEE !important;
}
}

&.start {
.selection {
background-color: #448aff;
border-top-left-radius: 50%;
border-bottom-left-radius: 50%;
}

&:after {
right: 0;
}

&.end:after {
display: none;
}
}
&.betweenRange {
border-radius: 0;
.selection {
left: 0;
right: 0;
width: 100%;
margin-left: 0;
display: flex;
justify-content: center;
align-items: center;
border-radius: 0;
}
}
&.end {
&:after {
left: 0;
}

.selection {
border-top-right-radius: 50%;
border-bottom-right-radius: 50%;

color: #559fff;
background-color: #FFF;
box-sizing: border-box;
}
}
}
Loading