Skip to content

Commit

Permalink
feat: add methods to make setting invalid overridable (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
mukherjeesudebi authored Apr 19, 2023
1 parent f0e4f9c commit 1c11516
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/vaadin-combo-box-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,27 @@
this._clear();
}

/**
* @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.
*
Expand All @@ -1102,7 +1123,7 @@
*/
validate() {
const isValid = this.checkValidity();
this.invalid = !isValid;
this._setInvalid(!isValid);
this.dispatchEvent(new CustomEvent('validated', {detail: {valid: isValid}}));
return isValid;
}
Expand Down
18 changes: 18 additions & 0 deletions test/validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let comboBox;

beforeEach(async() => {
comboBox = fixture('combo-box');
comboBox._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

it('should set invalid only when it is true', async() => {
comboBox.required = true;
comboBox.validate();
expect(comboBox.invalid).to.be.true;
comboBox.value = 'foo';
comboBox.validate();
expect(comboBox.invalid).to.be.true;
});
});
</script>

</body>
Expand Down

0 comments on commit 1c11516

Please sign in to comment.