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) #828

Merged
merged 1 commit into from
Apr 19, 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-date-picker-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,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 @@ -830,7 +851,7 @@
// to avoid breaking change on custom `checkValidity`.
// Can be removed with next major.
const isValid = this.checkValidity(this._inputValue);
this.invalid = !isValid;
this._setInvalid(!isValid);
this.dispatchEvent(new CustomEvent('validated', {detail: {valid: isValid}}));
return isValid;
}
Expand Down
3 changes: 3 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"parserOptions": {
"ecmaVersion": 8
},
"rules": {
"no-undef": 0,
"no-unused-vars": 0
Expand Down
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 @@ -3,10 +3,11 @@
<head>
<meta charset="UTF-8">
<title>vaadin-date-picker validation tests</title>
<script src="../../web-component-tester/browser.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="../vaadin-date-picker.html">
<link rel="import" href="helpers.html">
</head>

<body>
Expand Down Expand Up @@ -44,5 +45,23 @@
expect(event.detail.valid).to.be.false;
});
});
describe('invalid cannot be set to false', () => {
let datePicker;

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

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