Skip to content

Commit

Permalink
fix(datepicker): fix manual input accepts invalid date (#5532)
Browse files Browse the repository at this point in the history
* force min or max date on keyboard input
* works fine on single date picker
* weird cases on range picker caused by different timezones
Closes #4477
  • Loading branch information
FredBonux authored and daniloff200 committed Jan 11, 2020
1 parent 0f4d879 commit 3078f07
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/datepicker/bs-datepicker-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ export class BsDatepickerInputDirective
}

if (this._picker && this._picker.minDate && isBefore(_value, this._picker.minDate, 'date')) {
this.writeValue(this._picker.minDate);

return { bsDate: { minDate: this._picker.minDate } };
}

if (this._picker && this._picker.maxDate && isAfter(_value, this._picker.maxDate, 'date')) {
this.writeValue(this._picker.maxDate);

return { bsDate: { maxDate: this._picker.maxDate } };
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/datepicker/bs-daterangepicker-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ export class BsDaterangepickerInputDirective

validate(c: AbstractControl): ValidationErrors | null {
const _value: [Date, Date] = c.value;
const errors: object[] = [];

if (_value === null || _value === undefined || !isArray(_value)) {
return null;
}

// @ts-ignore
_value.sort((a, b) => a - b);

const _isFirstDateValid = isDateValid(_value[0]);
const _isSecondDateValid = isDateValid(_value[1]);

Expand All @@ -130,11 +134,18 @@ export class BsDaterangepickerInputDirective
}

if (this._picker && this._picker.minDate && isBefore(_value[0], this._picker.minDate, 'date')) {
return { bsDate: { minDate: this._picker.minDate } };
_value[0] = this._picker.minDate;
errors.push({ bsDate: { minDate: this._picker.minDate } });
}

if (this._picker && this._picker.maxDate && isAfter(_value[1], this._picker.maxDate, 'date')) {
return { bsDate: { maxDate: this._picker.maxDate } };
_value[1] = this._picker.maxDate;
errors.push({ bsDate: { maxDate: this._picker.maxDate } });
}
if (errors.length > 0) {
this.writeValue(_value);

return errors;
}
}

Expand Down

0 comments on commit 3078f07

Please sign in to comment.