Skip to content

Commit

Permalink
fix: prevent possible errors when inputs property is not set (#8012)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Oct 24, 2024
1 parent 81408db commit 82bfd11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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));
}

/**
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

0 comments on commit 82bfd11

Please sign in to comment.