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

fix: prevent possible errors when inputs property is not set #8012

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions packages/custom-field/src/vaadin-custom-field-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const CustomFieldMixin = (superClass) =>
*/
_shouldRemoveFocus(event) {
const { relatedTarget } = event;
return !this.inputs.some((el) => relatedTarget === (el.focusElement || el));
return this.inputs && !this.inputs.some((el) => relatedTarget === (el.focusElement || el));
web-padawan marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -181,9 +181,10 @@ export const CustomFieldMixin = (superClass) =>
* @return {boolean}
*/
checkValidity() {
const invalidFields = this.inputs.filter((input) => !(input.validate || input.checkValidity).call(input));
const hasInvalidFields =
this.inputs && this.inputs.some((input) => !(input.validate || input.checkValidity).call(input));

if (invalidFields.length || (this.required && !(this.value && this.value.trim()))) {
if (hasInvalidFields || (this.required && !(this.value && this.value.trim()))) {
// Either 1. one of the input fields is invalid or
// 2. the custom field itself is required but doesn't have a value
return false;
Expand Down Expand Up @@ -213,9 +214,10 @@ export const CustomFieldMixin = (superClass) =>
*/
_onKeyDown(e) {
if (e.key === 'Tab') {
const inputs = this.inputs || [];
if (
(this.inputs.indexOf(e.target) < this.inputs.length - 1 && !e.shiftKey) ||
(this.inputs.indexOf(e.target) > 0 && e.shiftKey)
(inputs.indexOf(e.target) < inputs.length - 1 && !e.shiftKey) ||
(inputs.indexOf(e.target) > 0 && e.shiftKey)
) {
this.dispatchEvent(new CustomEvent('internal-tab'));
} else {
Expand Down
6 changes: 6 additions & 0 deletions packages/custom-field/test/validation.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('validation', () => {
const event = validatedSpy.firstCall.args[0];
expect(event.detail.valid).to.be.false;
});

it('should not throw when checkValidity() called without inputs', () => {
expect(() => {
document.createElement('vaadin-custom-field').checkValidity();
}).to.not.throw(Error);
});
});

describe('required', () => {
Expand Down