Skip to content

Commit

Permalink
🎨 [open-formulieren/open-forms#4716] Replaced DOM query with ref
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmolen authored and sergei-maertens committed Oct 24, 2024
1 parent 724958c commit fec2d94
Show file tree
Hide file tree
Showing 17 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/formio/components/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Checkbox extends Formio.Components.components.checkbox {
}

setErrorClasses(elements, dirty, hasErrors, hasMessages) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/Currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Currency extends Formio.Components.components.currency {
}

setErrorClasses(elements, dirty, hasErrors, hasMessages) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}

Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/DateField.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DateField extends DateTimeField {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(targetElements, hasErrors, hasMessages, this.element);
setErrorAttributes(targetElements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(targetElements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/DateTimeField.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DateTimeField extends DateTimeFormio {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(targetElements, hasErrors, hasMessages, this.element);
setErrorAttributes(targetElements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(targetElements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/Email.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Email extends Formio.Components.components.email {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/FileField.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class FileField extends Formio.Components.components.file {
setErrorClasses(elements, dirty, hasErrors, hasMessages) {
const input = this.refs.fileBrowse;
const targetElements = input ? [input] : [];
setErrorAttributes(targetElements, hasErrors, hasMessages, this.element);
setErrorAttributes(targetElements, hasErrors, hasMessages, this.refs.messageContainer.id);
return super.setErrorClasses(targetElements, dirty, hasErrors, hasMessages);
}

Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/IBANField.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class IBANField extends TextField {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Number extends Formio.Components.components.number {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/Password.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Password extends Formio.Components.components.password {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/PhoneNumberField.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PhoneNumberField extends PhoneNumber {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Radio extends Formio.Components.components.radio {
}

setErrorClasses(elements, dirty, hasErrors, hasMessages) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Select extends Formio.Components.components.select {
setErrorClasses(elements, dirty, hasErrors, hasMessages) {
const inputClone = this.element.querySelector('input:not([type="hidden"])');
const targetElements = inputClone ? [inputClone] : [];
setErrorAttributes(targetElements, hasErrors, hasMessages, this.element);
setErrorAttributes(targetElements, hasErrors, hasMessages, this.refs.messageContainer.id);
return super.setErrorClasses(targetElements, dirty, hasErrors, hasMessages);
}

Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/Selectboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Selectboxes extends Formio.Components.components.selectboxes {
}

setErrorClasses(elements, dirty, hasErrors, hasMessages) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TextArea extends Formio.Components.components.textarea {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TextField extends Formio.Components.components.textfield {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/formio/components/TimeField.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TimeField extends Time {
// setErrorAttributes cannot be done for a `multiple` component
// https://github.com/open-formulieren/open-forms-sdk/pull/717#issuecomment-2405060364
if (!this.component.multiple) {
setErrorAttributes(elements, hasErrors, hasMessages, this.element);
setErrorAttributes(elements, hasErrors, hasMessages, this.refs.messageContainer.id);
}
return super.setErrorClasses(elements, dirty, hasErrors, hasMessages);
}
Expand Down
13 changes: 5 additions & 8 deletions src/formio/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,21 @@ const escapeHtml = source => {
return pre.innerHTML.replace(/"/g, '"').replace(/'/g, ''').replace(/&/g, '&');
};

const setErrorAttributes = (elements, hasErrors, hasMessages, parentElement) => {
const setErrorAttributes = (elements, hasErrors, hasMessages, messageContainerId) => {
// Update the attributes 'aria-invalid' and 'aria-describedby' using hasErrors
elements.forEach(element => {
const errorMessageElementId = parentElement.querySelector('[ref="messageContainer"]')?.id;
let ariaDescriptions = (element.getAttribute('aria-describedby') || '')
.split(' ')
.filter(description => !!description);

if (hasErrors && hasMessages && !ariaDescriptions.includes(errorMessageElementId)) {
if (hasErrors && hasMessages && !ariaDescriptions.includes(messageContainerId)) {
// The input has an error, but the error message isn't yet part of the ariaDescriptions
ariaDescriptions.push(errorMessageElementId);
ariaDescriptions.push(messageContainerId);
}

if (!hasErrors && ariaDescriptions.includes(errorMessageElementId)) {
if (!hasErrors && ariaDescriptions.includes(messageContainerId)) {
// The input doesn't have an error, but the error message is still a part of the ariaDescriptions
ariaDescriptions = ariaDescriptions.filter(
description => description !== errorMessageElementId
);
ariaDescriptions = ariaDescriptions.filter(description => description !== messageContainerId);
}

if (ariaDescriptions.length > 0) {
Expand Down

0 comments on commit fec2d94

Please sign in to comment.