Skip to content

Commit

Permalink
fix: #59 prevent selecting disabled values
Browse files Browse the repository at this point in the history
  • Loading branch information
arfedulov committed Dec 30, 2018
1 parent 1e68e30 commit bab7718
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/pickers/MonthPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
9 changes: 8 additions & 1 deletion src/pickers/YearPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ class YearPicker extends SingleSelectionPicker<YearPickerProps> {
}

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 {
Expand Down
5 changes: 4 additions & 1 deletion src/pickers/dayPicker/DatesRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
buildDays,
getDefaultEnabledDayPositions,
getDisabledDays,
getInitialDatePosition,
isNextPageAvailable,
isPrevPageAvailable,
} from './sharedFunctions';
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/pickers/dayPicker/DayPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import {
buildDays,
getDisabledDays,
getInitialDatePosition,
isNextPageAvailable,
isPrevPageAvailable,
} from './sharedFunctions';
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions src/pickers/dayPicker/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 6 additions & 1 deletion src/pickers/timePicker/HourPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/pickers/timePicker/MinutePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down

0 comments on commit bab7718

Please sign in to comment.