Skip to content

Commit 3078f07

Browse files
FredBonuxdaniloff200
authored andcommitted
fix(datepicker): fix manual input accepts invalid date (#5532)
* 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
1 parent 0f4d879 commit 3078f07

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/datepicker/bs-datepicker-input.directive.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,14 @@ export class BsDatepickerInputDirective
113113
}
114114

115115
if (this._picker && this._picker.minDate && isBefore(_value, this._picker.minDate, 'date')) {
116+
this.writeValue(this._picker.minDate);
117+
116118
return { bsDate: { minDate: this._picker.minDate } };
117119
}
118120

119121
if (this._picker && this._picker.maxDate && isAfter(_value, this._picker.maxDate, 'date')) {
122+
this.writeValue(this._picker.maxDate);
123+
120124
return { bsDate: { maxDate: this._picker.maxDate } };
121125
}
122126
}

src/datepicker/bs-daterangepicker-input.directive.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,15 @@ export class BsDaterangepickerInputDirective
113113

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

117118
if (_value === null || _value === undefined || !isArray(_value)) {
118119
return null;
119120
}
120121

122+
// @ts-ignore
123+
_value.sort((a, b) => a - b);
124+
121125
const _isFirstDateValid = isDateValid(_value[0]);
122126
const _isSecondDateValid = isDateValid(_value[1]);
123127

@@ -130,11 +134,18 @@ export class BsDaterangepickerInputDirective
130134
}
131135

132136
if (this._picker && this._picker.minDate && isBefore(_value[0], this._picker.minDate, 'date')) {
133-
return { bsDate: { minDate: this._picker.minDate } };
137+
_value[0] = this._picker.minDate;
138+
errors.push({ bsDate: { minDate: this._picker.minDate } });
134139
}
135140

136141
if (this._picker && this._picker.maxDate && isAfter(_value[1], this._picker.maxDate, 'date')) {
137-
return { bsDate: { maxDate: this._picker.maxDate } };
142+
_value[1] = this._picker.maxDate;
143+
errors.push({ bsDate: { maxDate: this._picker.maxDate } });
144+
}
145+
if (errors.length > 0) {
146+
this.writeValue(_value);
147+
148+
return errors;
138149
}
139150
}
140151

0 commit comments

Comments
 (0)