diff --git a/src/Calendar/index.js b/src/Calendar/index.js index 1c01075bd..1bf369d46 100644 --- a/src/Calendar/index.js +++ b/src/Calendar/index.js @@ -134,6 +134,17 @@ export default class Calendar extends Component { this._minDate = parse(props.minDate); this._maxDate = parse(props.maxDate); + //validate min/max inputs + if(isNaN(this._min.getTime())) this._min = new Date(1980, 0, 1); + if(isNaN(this._minDate.getTime())) this._minDate = new Date(1980, 0, 1); + if(isNaN(this._max.getTime())) this._max = new Date(2050, 11, 31); + if(isNaN(this._maxDate.getTime())) this._max = new Date(2050, 11, 31); + if(this._min>this._max) { + var temp = this._min; + this._min = this._max; + this._max = temp; + } + const min = this._min.getFullYear(); const minMonth = this._min.getMonth(); const max = this._max.getFullYear(); diff --git a/src/Calendar/withDateSelection.js b/src/Calendar/withDateSelection.js index c93665165..4abd88142 100644 --- a/src/Calendar/withDateSelection.js +++ b/src/Calendar/withDateSelection.js @@ -14,7 +14,7 @@ export const enhanceDay = withPropsOnChange(['selected'], props => ({ })); const enhanceYear = withPropsOnChange(['selected'], ({selected}) => ({ - selected: parse(selected), + selected: isNaN(parse(selected).getTime()) ? null : parse(selected), })); // Enhancer to handle selecting and displaying a single date @@ -29,7 +29,7 @@ export const withDateSelection = compose( DayComponent: enhanceDay(DayComponent), YearsComponent: enhanceYear(YearsComponent), })), - withState('scrollDate', 'setScrollDate', props => props.selected || new Date()), + withState('scrollDate', 'setScrollDate', props => sanitizeDate(props.selected,props) || new Date()), withProps(({onSelect, setScrollDate, ...props}) => { const selected = sanitizeDate(props.selected, props); diff --git a/src/Calendar/withMultipleDates.js b/src/Calendar/withMultipleDates.js index 21ce5d089..14ce50ccb 100644 --- a/src/Calendar/withMultipleDates.js +++ b/src/Calendar/withMultipleDates.js @@ -59,6 +59,7 @@ function handleYearSelect(date, callback) { } function getInitialDate({selected}) { + if(!selected || !selected[0] || isNaN(selected[0].getTime())) return new Date(); return selected.length ? selected[0] : new Date(); } diff --git a/src/Calendar/withRange.js b/src/Calendar/withRange.js index 83d7766b6..5fdf10cd7 100644 --- a/src/Calendar/withRange.js +++ b/src/Calendar/withRange.js @@ -1,7 +1,7 @@ import {compose, withProps, withPropsOnChange, withState} from 'recompose'; import classNames from 'classnames'; import {withDefaultProps} from './'; -import {withImmutableProps} from '../utils'; +import {sanitizeDate, withImmutableProps} from '../utils'; import isBefore from 'date-fns/is_before'; import enhanceHeader from '../Header/withRange'; import format from 'date-fns/format'; @@ -73,8 +73,8 @@ export const withRange = compose( }, }, selected: { - start: format(selected.start, 'YYYY-MM-DD'), - end: format(selected.end, 'YYYY-MM-DD'), + start: format((sanitizeDate(selected.start,props) ? selected.start : new Date()), 'YYYY-MM-DD'), + end: format((sanitizeDate(selected.end,props) ? selected.end : new Date()), 'YYYY-MM-DD'), }, })), ); @@ -125,6 +125,7 @@ function handleYearSelect(date, {displayKey, onSelect, selected, setScrollDate}) } function getInitialDate({selected}) { + if(!selected || !selected.start || isNaN(selected.start.getTime())) return new Date(); return selected.start || new Date(); } diff --git a/src/utils/index.js b/src/utils/index.js index e12dca456..e36e7d546 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -141,6 +141,7 @@ export function sanitizeDate(date, { // Selected date should not be disabled or outside the selectable range if ( !date || + isNaN(date.getTime()) || disabledDates.some(disabledDate => isSameDay(disabledDate, date)) || disabledDays && disabledDays.indexOf(getDay(date)) !== -1 || minDate && isBefore(date, startOfDay(minDate)) ||