Skip to content

Commit 0e58742

Browse files
authored
fix: prevent duplicate validation on blur (#6192)
1 parent 90a7b10 commit 0e58742

2 files changed

Lines changed: 36 additions & 26 deletions

File tree

‎packages/time-picker/src/vaadin-time-picker.js‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
125125
position-target="[[_inputContainer]]"
126126
theme$="[[_theme]]"
127127
on-change="__onComboBoxChange"
128+
on-validated="__onComboBoxValidated"
128129
on-has-input-value-changed="__onComboBoxHasInputValueChanged"
129130
>
130131
<vaadin-input-container
@@ -417,19 +418,6 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
417418
);
418419
}
419420

420-
/**
421-
* Override method inherited from `FocusMixin` to validate on blur.
422-
* @param {boolean} focused
423-
* @protected
424-
*/
425-
_setFocused(focused) {
426-
super._setFocused(focused);
427-
428-
if (!focused) {
429-
this.validate();
430-
}
431-
}
432-
433421
/** @private */
434422
__validDayDivisor(step) {
435423
// Valid if undefined, or exact divides a day, or has millisecond resolution
@@ -640,9 +628,6 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
640628
/** @private */
641629
__onComboBoxChange(event) {
642630
event.stopPropagation();
643-
644-
this.validate();
645-
646631
this.__dispatchChange();
647632
}
648633

@@ -655,6 +640,11 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
655640
this._hasInputValue = this.$.comboBox._hasInputValue;
656641
}
657642

643+
/** @private */
644+
__onComboBoxValidated() {
645+
this.validate();
646+
}
647+
658648
/** @private */
659649
__updateValue(obj) {
660650
const timeString = this.__formatISO(this.__validateTime(obj)) || '';

‎packages/time-picker/test/validation.test.js‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,37 +68,57 @@ describe('validation', () => {
6868
});
6969

7070
describe('basic', () => {
71-
let validateSpy;
71+
let validateSpy, changeSpy, input;
7272

7373
beforeEach(() => {
7474
timePicker = fixtureSync(`<vaadin-time-picker></vaadin-time-picker>`);
7575
validateSpy = sinon.spy(timePicker, 'validate');
76+
changeSpy = sinon.spy();
77+
timePicker.addEventListener('change', changeSpy);
78+
input = timePicker.inputElement;
7679
});
7780

7881
it('should pass validation by default', () => {
7982
expect(timePicker.checkValidity()).to.be.true;
8083
expect(timePicker.validate()).to.be.true;
8184
});
8285

83-
it('should not validate on value input', () => {
86+
it('should not validate on user input', () => {
8487
setInputValue(timePicker, '12:00');
85-
expect(validateSpy.calledOnce).to.be.false;
88+
expect(validateSpy.called).to.be.false;
89+
});
90+
91+
it('should validate on blur', () => {
92+
input.focus();
93+
input.blur();
94+
expect(validateSpy.calledOnce).to.be.true;
8695
});
8796

88-
it('should validate on value commit', () => {
97+
it('should validate before change event on blur', () => {
98+
input.focus();
8999
setInputValue(timePicker, '12:00');
90-
enter(timePicker.inputElement);
100+
input.blur();
101+
expect(changeSpy.calledOnce).to.be.true;
91102
expect(validateSpy.calledOnce).to.be.true;
103+
expect(validateSpy.calledBefore(changeSpy)).to.be.true;
92104
});
93105

94-
it('should validate on input focusout', () => {
95-
focusout(timePicker.inputElement);
106+
it('should validate before change event on Enter', () => {
107+
setInputValue(timePicker, '12:00');
108+
enter(timePicker.inputElement);
109+
expect(changeSpy.calledOnce).to.be.true;
96110
expect(validateSpy.calledOnce).to.be.true;
111+
expect(validateSpy.calledBefore(changeSpy)).to.be.true;
97112
});
98113

99-
it('should be possible to force invalid status', () => {
100-
timePicker.invalid = true;
101-
expect(timePicker.inputElement.hasAttribute('invalid')).to.be.true;
114+
it('should validate before change event on clear button click', () => {
115+
timePicker.clearButtonVisible = true;
116+
timePicker.value = '12:00';
117+
validateSpy.resetHistory();
118+
timePicker.$.clearButton.click();
119+
expect(changeSpy.calledOnce).to.be.true;
120+
expect(validateSpy.calledOnce).to.be.true;
121+
expect(validateSpy.calledBefore(changeSpy)).to.be.true;
102122
});
103123

104124
it('should not validate on min change without value', () => {

0 commit comments

Comments
 (0)