Topic: [A11y] Auto-Complete Accessibility Issues in .cshtml file

foundant priority asked 1 day ago


I was tasked to address some A11Y accessibility issues flagged involving the MDB AutoComplete. I am using the Axe DevTools extension on the Edge browser with Best Practices set to ON and set to WCAG 2.2 AA for our testing.

We are calling your mdb.Autocomplete() function like so inside of our .cshtml file in the <script> section of the file:

new mdb.Autocomplete(organizationAutocomplete, {
    filter: nameFilter,
    displayValue: (value) => value.Name,
    listHeight: 235,
    itemContent: (result) => {
        return `
        <div class="w-100">
            <div>${result.Name}</div>
            <div class="ft-6 small">${result.TaxId}</div>
        </div>
        `;
    },
    threshold: 1
});

With this, we get several A11Y violations in several states.


No Results Found

If there are no results found, we have something like this:No results foundAnd it reports the following A11y violations, via Axe DevTools:No results A11y violations


With Suggested Results

If there are some results based on the search input, we get the following:enter image description herewhich reports the following A11y violations:With results A11y violations


Work Around / Fix

Because the Auto Complete is added to the DOM dynamically, I had to be a bit clever in order to target the Auto Complete markup in order to address the A11y violations. The code to handle the dynamic DOM situation looks like this:

const observer = new MutationObserver((mutations) => {
    const dropdownContainer = document.querySelector(".autocomplete-dropdown-container");
    if (dropdownContainer && !dropdownContainer.hasAttribute("role")) {
        dropdownContainer.setAttribute("role", "region");
    }

    const autocompleteList = document.querySelector('ul.autocomplete-items-list');
    if (autocompleteList && !autocompleteList.hasAttribute('aria-label')) {
        autocompleteList.setAttribute('aria-label', 'Organization suggestions');

        const listItems = autocompleteList.querySelectorAll("li");
        if (listItems) {
            listItems.forEach((item) => {
                item.setAttribute("role", "option");
            })
        }

        autocompleteList.setAttribute("tabindex", 0);
        observer.disconnect(); // Stop observing once we've set it
    }
});

observer.observe(document.body, {
    childList: true,
    subtree: true
});

The Ask/Question

Is there a simpler way to add accessibility attributes to the dynamically generated markup? I needed to add role="region", role="option", aria-label, and tabindex="0" to address A11Y violations. Our team prefers to avoid using MutationObserver if possible. Are there plans to address this in future versions, or is there a configuration option we can use to make the generated HTML elements A11Y compliant by default?

Thanks in advance.

P.S.: the images don't appear to be rendering in the preview below. It looks like the image has uploaded to your server as the markup suggests the image is located at: /mdb-images/support/<guid>


Resources (screenshots, code snippets etc.)


Bartosz Cylwik staff answered 22 hours ago


Hi! Unfortunately I dont think there is a way to add / customize the accessibility attributes you need right now. Im adding checking those issues to our to check / to do list for future releases but I cannot give you an ETA for now.

As of your workaround, maybe using our events instead of observers could work here? I. e. when the dropdown is opened?

https://mdbootstrap.com/docs/standard/forms/autocomplete/#api-section-events

Other thing you could try is to customize the component yourself (from the source) and use this one instead.

Best Regards!



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB Standard
  • MDB Version: MDB5 9.0.0
  • Device: N/A
  • Browser: Edge
  • OS: Windows 11
  • Provided sample code: Yes
  • Provided link: Yes