diff --git a/src/pickers/MonthPicker.tsx b/src/pickers/MonthPicker.tsx index 8d7029d8..40ac7c83 100644 --- a/src/pickers/MonthPicker.tsx +++ b/src/pickers/MonthPicker.tsx @@ -101,6 +101,11 @@ class MonthPicker } protected getInitialDatePosition(): number { + const selectable = this.getSelectableCellPositions(); + if (selectable.indexOf(this.state.date.month()) < 0) { + return selectable[0]; + } + return this.state.date.month(); } diff --git a/src/pickers/YearPicker.tsx b/src/pickers/YearPicker.tsx index 718eda50..da35109b 100644 --- a/src/pickers/YearPicker.tsx +++ b/src/pickers/YearPicker.tsx @@ -90,7 +90,14 @@ class YearPicker extends SingleSelectionPicker { } protected getInitialDatePosition(): number { - return this.buildCalendarValues().indexOf(this.state.date.year().toString()); + const selectable = this.getSelectableCellPositions(); + const values = this.buildCalendarValues(); + const currentYearIndex = values.indexOf(this.state.date.year().toString()); + if (selectable.indexOf(currentYearIndex) < 0) { + return selectable[0]; + } + + return currentYearIndex; } protected getActiveCellPosition(): number { diff --git a/src/pickers/dayPicker/DatesRangePicker.tsx b/src/pickers/dayPicker/DatesRangePicker.tsx index ee36cb2e..59eb9545 100644 --- a/src/pickers/dayPicker/DatesRangePicker.tsx +++ b/src/pickers/dayPicker/DatesRangePicker.tsx @@ -17,6 +17,7 @@ import { buildDays, getDefaultEnabledDayPositions, getDisabledDays, + getInitialDatePosition, isNextPageAvailable, isPrevPageAvailable, } from './sharedFunctions'; @@ -102,7 +103,9 @@ class DatesRangePicker } protected getInitialDatePosition(): number { - return this.buildCalendarValues().indexOf(this.state.date.date().toString()); + return getInitialDatePosition(this.state.date.date().toString(), + this.buildCalendarValues(), + this.getSelectableCellPositions()); } // TODO: too complicated method diff --git a/src/pickers/dayPicker/DayPicker.tsx b/src/pickers/dayPicker/DayPicker.tsx index 344b81b2..b19cc4aa 100644 --- a/src/pickers/dayPicker/DayPicker.tsx +++ b/src/pickers/dayPicker/DayPicker.tsx @@ -15,6 +15,7 @@ import { import { buildDays, getDisabledDays, + getInitialDatePosition, isNextPageAvailable, isPrevPageAvailable, } from './sharedFunctions'; @@ -101,7 +102,9 @@ class DayPicker } protected getInitialDatePosition(): number { - return this.buildCalendarValues().indexOf(this.state.date.date().toString()); + return getInitialDatePosition(this.state.date.date().toString(), + this.buildCalendarValues(), + this.getSelectableCellPositions()); } protected getActiveCellPosition(): number { diff --git a/src/pickers/dayPicker/sharedFunctions.ts b/src/pickers/dayPicker/sharedFunctions.ts index 7e69a38d..daf76aed 100644 --- a/src/pickers/dayPicker/sharedFunctions.ts +++ b/src/pickers/dayPicker/sharedFunctions.ts @@ -149,3 +149,19 @@ export const testExport = { getDaysArray, getDefaultEnabledDayPositions, }; + +export function getInitialDatePosition(initDate: string, + values: string[], + selectablePositions: number[]): number { +const selectable = selectablePositions.reduce((acc, pos) => { +acc.push({ value: values[pos], position: pos }); + +return acc; +}, []); +const res = _.find(selectable, (item) => item.value === initDate); +if (res) { +return res.position; +} + +return selectable[0].position; +} diff --git a/src/pickers/timePicker/HourPicker.tsx b/src/pickers/timePicker/HourPicker.tsx index ef7ee2ae..1b2a8255 100644 --- a/src/pickers/timePicker/HourPicker.tsx +++ b/src/pickers/timePicker/HourPicker.tsx @@ -110,7 +110,12 @@ class HourPicker } protected getInitialDatePosition(): number { - return 0; + const selectable = this.getSelectableCellPositions(); + if (selectable.indexOf(this.state.date.hour()) < 0) { + return selectable[0]; + } + + return this.state.date.hour(); } protected getActiveCellPosition(): number { diff --git a/src/pickers/timePicker/MinutePicker.tsx b/src/pickers/timePicker/MinutePicker.tsx index dcaf0520..abb5d732 100644 --- a/src/pickers/timePicker/MinutePicker.tsx +++ b/src/pickers/timePicker/MinutePicker.tsx @@ -113,7 +113,10 @@ class MinutePicker } protected getInitialDatePosition(): number { - return 0; + const selectable = this.getSelectableCellPositions(); + if (selectable.indexOf(this.state.date.hour()) < 0) { + return selectable[0]; + } } protected getDisabledPositions(): number[] {