From a17b8aba8f6f9bfabc1d857701f06023b71f0a65 Mon Sep 17 00:00:00 2001 From: Sergey Vinogradov Date: Thu, 21 Sep 2023 15:29:08 +0300 Subject: [PATCH] fix: disable validation of internal text-field (#1052) --- src/vaadin-combo-box-light.html | 11 ++++ src/vaadin-combo-box-mixin.html | 6 +- src/vaadin-combo-box.html | 7 +++ test/vaadin-combo-box-light.html | 20 +++---- test/validation.html | 100 +++++++++++++++++++++++++++++-- 5 files changed, 127 insertions(+), 17 deletions(-) diff --git a/src/vaadin-combo-box-light.html b/src/vaadin-combo-box-light.html index b5dbf0fd..bc463d0f 100644 --- a/src/vaadin-combo-box-light.html +++ b/src/vaadin-combo-box-light.html @@ -142,6 +142,17 @@ return this.getRootNode().activeElement === this.inputElement; } + /** + * Returns true if the current input value satisfies all constraints (if any). + * @return {boolean} + */ + checkValidity() { + if (this.inputElement && this.inputElement.validate) { + return this.inputElement.validate(); + } + return super.checkValidity(); + } + /** @protected */ connectedCallback() { super.connectedCallback(); diff --git a/src/vaadin-combo-box-mixin.html b/src/vaadin-combo-box-mixin.html index 981e164b..8909ea0f 100644 --- a/src/vaadin-combo-box-mixin.html +++ b/src/vaadin-combo-box-mixin.html @@ -934,6 +934,7 @@ /** @private */ _detectAndDispatchChange() { if (this.value !== this._lastCommittedValue) { + this.validate(); this.dispatchEvent(new CustomEvent('change', {bubbles: true})); this._lastCommittedValue = this.value; } @@ -1098,6 +1099,7 @@ } if (!this.readonly && !this._closeOnBlurIsPrevented) { this._closeOrCommit(); + this.validate(); } } @@ -1164,8 +1166,8 @@ * @return {boolean | undefined} */ checkValidity() { - if (this.inputElement.validate) { - return this.inputElement.validate(); + if (this.inputElement.checkValidity) { + return this.inputElement.checkValidity(); } } diff --git a/src/vaadin-combo-box.html b/src/vaadin-combo-box.html index a375f61a..4d26d6ce 100644 --- a/src/vaadin-combo-box.html +++ b/src/vaadin-combo-box.html @@ -387,6 +387,13 @@ ready() { super.ready(); + // Disable the internal text-field's validation to prevent it from overriding + // the invalid state that was propagated to the text-field from the combo-box. + this.inputElement.validate = () => {}; + // Restore the internal text-field's invalid state in case + // it got overridden before the validation was disabled above. + this.inputElement.invalid = this.invalid; + this._nativeInput = this.inputElement.focusElement; this._toggleElement = this.$.toggleButton; this._clearElement = this.inputElement.shadowRoot.querySelector('[part="clear-button"]'); diff --git a/test/vaadin-combo-box-light.html b/test/vaadin-combo-box-light.html index 0c4c70a9..7657920d 100644 --- a/test/vaadin-combo-box-light.html +++ b/test/vaadin-combo-box-light.html @@ -309,33 +309,33 @@ it('should clear the selection when clicking on the clear button', () => { comboBox.open(); - + fire('click', clearButton); - + expect(comboBox.value).to.eql(''); expect(comboBox.$.overlay._selectedItem).to.be.null; expect(comboBox.selectedItem).to.be.null; }); - + it('should not close the dropdown after clearing a selection', () => { comboBox.open(); - + fire('click', clearButton); - + expect(comboBox.opened).to.eql(true); }); - + it('should not open the dropdown after clearing a selection', () => { fire('click', clearButton); - + expect(comboBox.opened).to.eql(false); }); - + it('should cancel click event to avoid input blur', () => { comboBox.open(); - + const event = fire('click', clearButton); - + expect(event.defaultPrevented).to.eql(true); }); diff --git a/test/validation.html b/test/validation.html index 6cbd9bfa..54bdaf4b 100644 --- a/test/validation.html +++ b/test/validation.html @@ -7,6 +7,7 @@ + @@ -15,10 +16,32 @@ + + + + + + + + +