Skip to content

Commit 1bca35b

Browse files
committed
fix: value entered manually in disabled range should be invalid (#508)
1 parent 0dc1167 commit 1bca35b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

__test__/date-picker.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,24 @@ describe('DatePicker', () => {
418418
});
419419
expect(vm.validMultipleType).toBe(false);
420420
});
421+
422+
it('If the value entered manually is in the disabled range should be invalid', () => {
423+
const someday = new Date(2020, 6, 1);
424+
wrapper = shallowMount(DatePicker, {
425+
format: 'YYYY-MM-DD',
426+
propsData: {
427+
disabledDate: date => {
428+
return date < someday;
429+
},
430+
},
431+
});
432+
const textInput = wrapper.find('input');
433+
textInput.setValue('2020-08-01');
434+
textInput.trigger('change');
435+
expect(wrapper.emitted().input[0][0]).toEqual(new Date(2020, 7, 1));
436+
textInput.setValue('2020-05-01');
437+
textInput.trigger('change');
438+
expect(wrapper.emitted().input[1]).toBe(undefined);
439+
expect(wrapper.emitted()['input-error'][0][0]).toBe('2020-05-01');
440+
});
421441
});

src/date-picker.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,19 @@ export default {
396396
}
397397
return isValidDate(value);
398398
},
399+
isValidValueAndNotDisabled(value) {
400+
if (!this.isValidValue(value)) {
401+
return false;
402+
}
403+
const disabledDate =
404+
typeof this.disabledDate === 'function' ? this.disabledDate : () => false;
405+
const disabledTime =
406+
typeof this.disabledTime === 'function' ? this.disabledTime : () => false;
407+
if (!Array.isArray(value)) {
408+
value = [value];
409+
}
410+
return value.every(v => !disabledDate(v) && !disabledTime(v));
411+
},
399412
handleMultipleDates(date, dates) {
400413
if (this.validMultipleType && dates) {
401414
const nextDates = dates.filter(v => v.getTime() !== date.getTime());
@@ -470,7 +483,7 @@ export default {
470483
} else {
471484
date = this.parseDate(text, this.format);
472485
}
473-
if (this.isValidValue(date)) {
486+
if (this.isValidValueAndNotDisabled(date)) {
474487
this.emitValue(date);
475488
this.blur();
476489
} else {

0 commit comments

Comments
 (0)