2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-10 00:42:26 +03:00

fix: value entered manually in disabled range should be invalid (#508)

This commit is contained in:
mengxiong10
2020-08-07 16:42:38 +08:00
parent 0dc1167d60
commit 1bca35bd74
2 changed files with 34 additions and 1 deletions
+20
View File
@@ -418,4 +418,24 @@ describe('DatePicker', () => {
});
expect(vm.validMultipleType).toBe(false);
});
it('If the value entered manually is in the disabled range should be invalid', () => {
const someday = new Date(2020, 6, 1);
wrapper = shallowMount(DatePicker, {
format: 'YYYY-MM-DD',
propsData: {
disabledDate: date => {
return date < someday;
},
},
});
const textInput = wrapper.find('input');
textInput.setValue('2020-08-01');
textInput.trigger('change');
expect(wrapper.emitted().input[0][0]).toEqual(new Date(2020, 7, 1));
textInput.setValue('2020-05-01');
textInput.trigger('change');
expect(wrapper.emitted().input[1]).toBe(undefined);
expect(wrapper.emitted()['input-error'][0][0]).toBe('2020-05-01');
});
});
+14 -1
View File
@@ -396,6 +396,19 @@ export default {
}
return isValidDate(value);
},
isValidValueAndNotDisabled(value) {
if (!this.isValidValue(value)) {
return false;
}
const disabledDate =
typeof this.disabledDate === 'function' ? this.disabledDate : () => false;
const disabledTime =
typeof this.disabledTime === 'function' ? this.disabledTime : () => false;
if (!Array.isArray(value)) {
value = [value];
}
return value.every(v => !disabledDate(v) && !disabledTime(v));
},
handleMultipleDates(date, dates) {
if (this.validMultipleType && dates) {
const nextDates = dates.filter(v => v.getTime() !== date.getTime());
@@ -470,7 +483,7 @@ export default {
} else {
date = this.parseDate(text, this.format);
}
if (this.isValidValue(date)) {
if (this.isValidValueAndNotDisabled(date)) {
this.emitValue(date);
this.blur();
} else {