Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit 6ad663d

Browse files
committed
better handling of invalid ranges
1 parent abe904b commit 6ad663d

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

demo/src/components/Main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export default class Main extends Component {
345345
moveRangeOnFirstSelection={false}
346346
ranges={[this.state.dateRangeWithDisabled.selection]}
347347
className={'PreviewArea'}
348-
disabledDates={[Date()]}
348+
disabledDates={[new Date()]}
349349
minDate={new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000)}
350350
/>
351351
</Section>

src/components/DateRange.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DateRange extends Component {
2222
}
2323
calcNewSelection(value, isSingleValue = true) {
2424
const focusedRange = this.props.focusedRange || this.state.focusedRange;
25-
const { ranges, onChange, maxDate, moveRangeOnFirstSelection } = this.props;
25+
const { ranges, onChange, maxDate, moveRangeOnFirstSelection, disabledDates } = this.props;
2626
const focusedRangeIndex = focusedRange[0];
2727
const selectedRange = ranges[focusedRangeIndex];
2828
if (!selectedRange || !onChange) return {};
@@ -43,37 +43,48 @@ class DateRange extends Component {
4343
} else {
4444
endDate = value;
4545
}
46+
4647
// reverse dates if startDate before endDate
48+
let hasBeenReversed = false;
4749
if (isBefore(endDate, startDate)) {
50+
hasBeenReversed = true;
4851
[startDate, endDate] = [endDate, startDate];
4952
}
5053

54+
const inValidDatesWithinRange = disabledDates.filter(disabledDate =>
55+
isWithinInterval(disabledDate, {
56+
start: startDate,
57+
end: endDate,
58+
})
59+
);
60+
61+
if (inValidDatesWithinRange.length > 0) {
62+
if (hasBeenReversed) {
63+
const minDate = new Date(Math.min.apply(null, inValidDatesWithinRange));
64+
startDate = new Date(minDate.getTime() + 24 * 60 * 60 * 1000);
65+
} else {
66+
const maxDate = new Date(Math.max.apply(null, inValidDatesWithinRange));
67+
endDate = new Date(maxDate.getTime() - 24 * 60 * 60 * 1000);
68+
}
69+
}
70+
5171
if (!nextFocusRange) {
5272
const nextFocusRangeIndex = findNextRangeIndex(this.props.ranges, focusedRange[0]);
5373
nextFocusRange = [nextFocusRangeIndex, 0];
5474
}
5575
return {
76+
wasValid: !(inValidDatesWithinRange.length > 0),
5677
range: { startDate, endDate },
5778
nextFocusRange: nextFocusRange,
5879
};
5980
}
6081
setSelection(value, isSingleValue) {
61-
const { onChange, ranges, onRangeFocusChange, disabledDates } = this.props;
82+
const { onChange, ranges, onRangeFocusChange } = this.props;
6283
const focusedRange = this.props.focusedRange || this.state.focusedRange;
6384
const focusedRangeIndex = focusedRange[0];
6485
const selectedRange = ranges[focusedRangeIndex];
6586
if (!selectedRange) return;
6687
const newSelection = this.calcNewSelection(value, isSingleValue);
67-
if (
68-
disabledDates.some(disabledDate =>
69-
isWithinInterval(disabledDate, {
70-
start: newSelection.range.startDate,
71-
end: newSelection.range.endDate,
72-
})
73-
)
74-
) {
75-
return;
76-
}
7788
onChange({
7889
[selectedRange.key || `range${focusedRangeIndex + 1}`]: {
7990
...selectedRange,
@@ -91,14 +102,14 @@ class DateRange extends Component {
91102
this.props.onRangeFocusChange && this.props.onRangeFocusChange(focusedRange);
92103
}
93104
updatePreview(val) {
94-
if (!val) {
105+
if (!val || !val.wasValid) {
95106
this.setState({ preview: null });
96107
return;
97108
}
98109
const { rangeColors, ranges } = this.props;
99110
const focusedRange = this.props.focusedRange || this.state.focusedRange;
100111
const color = ranges[focusedRange[0]].color || rangeColors[focusedRange[0]] || color;
101-
this.setState({ preview: { ...val, color } });
112+
this.setState({ preview: { ...val.range, color } });
102113
}
103114
render() {
104115
return (
@@ -107,7 +118,7 @@ class DateRange extends Component {
107118
onRangeFocusChange={this.handleRangeFocusChange}
108119
preview={this.state.preview}
109120
onPreviewChange={value => {
110-
this.updatePreview(value ? this.calcNewSelection(value).range : null);
121+
this.updatePreview(value ? this.calcNewSelection(value) : null);
111122
}}
112123
{...this.props}
113124
displayMode="dateRange"
@@ -127,6 +138,7 @@ DateRange.defaultProps = {
127138
ranges: [],
128139
moveRangeOnFirstSelection: false,
129140
rangeColors: ['#3d91ff', '#3ecf8e', '#fed14c'],
141+
disabledDates: [],
130142
};
131143

132144
DateRange.propTypes = {

0 commit comments

Comments
 (0)