diff --git a/src/vaadin-custom-field.html b/src/vaadin-custom-field.html index 82fa7a6..efa5fcf 100644 --- a/src/vaadin-custom-field.html +++ b/src/vaadin-custom-field.html @@ -225,6 +225,27 @@ } } + /** + * @param {boolean} invalid + * @protected + */ + _setInvalid(invalid) { + if (this._shouldSetInvalid(invalid)) { + this.invalid = invalid; + } + } + + /** + * Override this method to define whether the given `invalid` state should be set. + * + * @param {boolean} _invalid + * @return {boolean} + * @protected + */ + _shouldSetInvalid(_invalid) { + return true; + } + /** * Validates the field and sets the `invalid` property based on the result. * @@ -234,7 +255,7 @@ */ validate() { const isValid = this.checkValidity(); - this.invalid = !isValid; + this._setInvalid(!isValid); this.dispatchEvent(new CustomEvent('validated', {detail: {valid: isValid}})); return isValid; } diff --git a/test/.eslintrc.json b/test/.eslintrc.json index 2b2df00..ea86df8 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -1,4 +1,7 @@ { + "parserOptions": { + "ecmaVersion": 8 + }, "globals": { "WCT": false, "describe": false, @@ -10,6 +13,7 @@ "sinon": false, "dispatchChange": false, "dispatchSlotChange": false, - "MockInteractions": false + "MockInteractions": false, + "nextRender": false } } diff --git a/test/helpers.html b/test/helpers.html index e9520f4..3ae520a 100644 --- a/test/helpers.html +++ b/test/helpers.html @@ -1,6 +1,19 @@ + + + + + + + diff --git a/test/validation.html b/test/validation.html index 0c293f0..70250b2 100644 --- a/test/validation.html +++ b/test/validation.html @@ -7,6 +7,7 @@ + @@ -14,7 +15,6 @@ @@ -47,5 +47,23 @@ expect(event.detail.valid).to.be.false; }); }); + describe('invalid cannot be set to false', () => { + let customField; + + beforeEach(async() => { + customField = fixture('custom-field'); + customField._shouldSetInvalid = (invalid) => invalid; + await nextRender(); + }); + + it('should set invalid only when it is true', async() => { + customField.required = true; + customField.validate(); + expect(customField.invalid).to.be.true; + customField.value = 'value'; + customField.validate(); + expect(customField.invalid).to.be.true; + }); + });