Skip to content

Commit

Permalink
feat: add methods to make setting invalid overridable (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
mukherjeesudebi committed Apr 18, 2023
1 parent a14045e commit 0f0fb78
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/vaadin-time-picker.html
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,35 @@
return this.__inputElement;
}

/**
* @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.
*
* The method fires a `validated` event with the result of the validation.
*/
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 @@ -78,5 +78,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let timePicker;

beforeEach(async() => {
timePicker = fixture('time-picker');
timePicker._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

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

0 comments on commit 0f0fb78

Please sign in to comment.