Skip to content

Commit

Permalink
feat: add methods to make setting invalid overridable (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
mukherjeesudebi authored Apr 19, 2023
1 parent 31fb6ff commit c68d385
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
28 changes: 25 additions & 3 deletions src/vaadin-checkbox-group.html
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,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.
*
Expand All @@ -258,9 +279,10 @@
* @return {boolean} True if the value is valid.
*/
validate() {
this.invalid = this.required && this.value.length === 0;
this.dispatchEvent(new CustomEvent('validated', {detail: {valid: !this.invalid}}));
return !this.invalid;
const isValid = !(this.required && this.value.length === 0);
this._setInvalid(!isValid);
this.dispatchEvent(new CustomEvent('validated', {detail: {valid: isValid}}));
return isValid;
}

/** @private */
Expand Down
6 changes: 5 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"parserOptions": {
"ecmaVersion": 8
},
"globals": {
"WCT": false,
"describe": false,
Expand All @@ -9,6 +12,7 @@
"expect": false,
"gemini": false,
"MockInteractions": false,
"animationFrameFlush": false
"animationFrameFlush": false,
"nextRender": false
}
}
14 changes: 14 additions & 0 deletions test/helpers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>

<head>
<link rel="import" href="../../polymer/polymer.html">
</head>
<body>
<script>
window.nextRender = (element) => {
return new Promise(resolve => {
Polymer.RenderStatus.afterNextRender(element, resolve);
});
};
</script>
</body>
21 changes: 20 additions & 1 deletion test/validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="../vaadin-checkbox.html">
<link rel="import" href="../vaadin-checkbox-group.html">
<link rel="import" href="helpers.html">
</head>

<body>
<test-fixture id="checkbox-group">
<template>
<vaadin-checkbox-group>
<vaadin-checkbox-group>
<vaadin-checkbox value="1">Checkbox <b>1</b></vaadin-checkbox>
<vaadin-checkbox value="2">Checkbox <b>2</b></vaadin-checkbox>
<vaadin-checkbox value="3">Checkbox <b>3</b></vaadin-checkbox>
Expand Down Expand Up @@ -49,5 +50,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let group;

beforeEach(async() => {
group = fixture('checkbox-group');
group._shouldSetInvalid = (invalid) => invalid;
await nextRender();
});

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

0 comments on commit c68d385

Please sign in to comment.