Skip to content

Commit 2ed1bf8

Browse files
author
Jorge Giraldez Gonzalez
committed
📦 Add date range validator to avoid invalid ranges
1 parent 9297914 commit 2ed1bf8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Calendar.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Text,
99
Modal,
1010
Image,
11+
Alert,
1112
StyleSheet,
1213
ScrollView,
1314
Dimensions,
@@ -25,6 +26,7 @@ export default class Calendar extends Component {
2526
format: PropTypes.string,
2627
customI18n: PropTypes.object,
2728
customStyles: PropTypes.object,
29+
dateRangeValidator: PropTypes.object,
2830
color: PropTypes.object,
2931
minDate: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
3032
maxDate: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)])
@@ -33,6 +35,7 @@ export default class Calendar extends Component {
3335
format: 'YYYY-MM-DD',
3436
customI18n: {},
3537
customStyles: {},
38+
dateRangeValidator: null,
3639
color: {}
3740
}
3841
static DEFAULT_I18N_MAP = {
@@ -200,6 +203,16 @@ export default class Calendar extends Component {
200203
} = this.state;
201204
let startMoment = startDate ? startDate.clone() : null;
202205
let endMoment = endDate ? endDate.clone() : null;
206+
if (startMoment != null && endMoment != null && this.props.dateRangeValidator != null) {
207+
const daysBetween = endMoment.diff(startMoment, 'days');
208+
if (daysBetween < (this.props.dateRangeValidator.minDaysBetween || 1)
209+
|| daysBetween > (this.props.dateRangeValidator.maxDaysBetween || 30)) {
210+
Alert.alert(
211+
this.props.dateRangeValidator.msg || 'Invalid range'
212+
);
213+
return false;
214+
}
215+
}
203216
this.props.onConfirm && this.props.onConfirm({
204217
startDate: startMoment ? startMoment.toDate() : null,
205218
endDate: endMoment ? endMoment.toDate() : null,

index.ios.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ export default class calendar extends Component {
9292
fontWeight: 'normal'
9393
}
9494
};
95+
let dateRangeValidator = {
96+
minDaysBetween : 1,
97+
maxDaysBetween : 30,
98+
msg : 'Invalid custom range'
99+
};
95100
const {
96101
startDate,
97102
endDate
@@ -115,6 +120,7 @@ export default class calendar extends Component {
115120
ref={(calendar) => {this.calendar = calendar;}}
116121
customI18n={customI18n}
117122
customStyles={customStyles}
123+
dateRangeValidator={dateRangeValidator}
118124
format="YYYYMMDD"
119125
minDate="20170510"
120126
maxDate="20180412"

0 commit comments

Comments
 (0)