Skip to content

Commit

Permalink
fix: value entered manually in disabled range should be invalid (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxiong10 committed Aug 7, 2020
1 parent 0dc1167 commit 1bca35b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
20 changes: 20 additions & 0 deletions __test__/date-picker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
15 changes: 14 additions & 1 deletion src/date-picker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1bca35b

Please sign in to comment.