Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add methods to make setting invalid overridable (CP: 14) #128

Merged
merged 3 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/vaadin-custom-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"parserOptions": {
"ecmaVersion": 8
},
"globals": {
"WCT": false,
"describe": false,
Expand All @@ -10,6 +13,7 @@
"sinon": false,
"dispatchChange": false,
"dispatchSlotChange": false,
"MockInteractions": false
"MockInteractions": false,
"nextRender": false
}
}
13 changes: 13 additions & 0 deletions test/helpers.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
<!doctype html>

<head>
<link rel="import" href="../../polymer/polymer.html">
</head>
<body>
<script>
window.dispatchChange = function(el) {
const evt = new CustomEvent('change', {bubbles: true});
el.dispatchEvent(evt);
};

window.nextRender = (element) => {
return new Promise(resolve => {
Polymer.RenderStatus.afterNextRender(element, resolve);
});
};
</script>
</body>
20 changes: 19 additions & 1 deletion test/validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="../vaadin-custom-field.html">
<link rel="import" href="helpers.html">
</head>

<body>
<test-fixture id="custom-field">
<template>
<vaadin-custom-field>
<input type="text">
<input type="number">
</vaadin-custom-field>
</template>
</test-fixture>
Expand Down Expand Up @@ -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';
vursen marked this conversation as resolved.
Show resolved Hide resolved
customField.validate();
expect(customField.invalid).to.be.true;
});
});
</script>
</body>