Skip to content

Commit 2754740

Browse files
committed
use role=combobox for dropdown with input
1 parent 02c65fe commit 2754740

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

web_src/js/features/aria.js

+52-40
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,79 @@ function generateAriaId() {
66
return `_aria_auto_id_${ariaIdCounter++}`;
77
}
88

9-
// make the item has role=menuitem/option, and add an id if there wasn't one yet.
10-
function prepareMenuItem($item) {
11-
if (!$item.attr('id')) $item.attr('id', generateAriaId());
12-
$item.attr({'role': 'menuitem', 'tabindex': '-1'});
13-
$item.find('a').attr('tabindex', '-1'); // as above, the elements inside the dropdown menu item should not be focusable, the focus should always be on the dropdown primary element.
14-
}
15-
16-
// when the dropdown menu items are loaded from AJAX requests, the items are created dynamically
17-
const defaultCreateDynamicMenu = $.fn.dropdown.settings.templates.menu;
18-
$.fn.dropdown.settings.templates.menu = function(response, fields, preserveHTML, className) {
19-
const ret = defaultCreateDynamicMenu(response, fields, preserveHTML, className);
20-
const $wrapper = $('<div>').append(ret);
21-
const $items = $wrapper.find('> .item');
22-
$items.each((_, item) => {
23-
prepareMenuItem($(item));
24-
});
25-
return $wrapper.html();
26-
};
27-
289
function attachOneDropdownAria($dropdown) {
2910
if ($dropdown.attr('data-aria-attached')) return;
3011
$dropdown.attr('data-aria-attached', 1);
3112

32-
const $textSearch = $dropdown.find('input.search').eq(0);
33-
const $focusable = $textSearch.length ? $textSearch : $dropdown; // see comment below
34-
if (!$focusable.length) return;
35-
36-
// prepare dropdown menu list
37-
const $menu = $dropdown.find('> .menu');
38-
if (!$menu.attr('id')) $menu.attr('id', generateAriaId());
39-
40-
// dropdown has 2 different focusing behaviors
41-
// * with search input: the input is focused, and it works perfectly with aria-activedescendant pointing another sibling element.
13+
// Dropdown has 2 different focusing behaviors
14+
// * with search input: the input is focused, and it works with aria-activedescendant pointing another sibling element.
4215
// * without search input (but the readonly text), the dropdown itself is focused. then the aria-activedescendant points to the element inside dropdown
16+
// Some desktop screen readers may change the focus, but dropdown requires that the focus must be on its primary element, then they don't work well.
4317

44-
// expected user interactions for dropdown with aria support:
18+
// Expected user interactions for dropdown with aria support:
4519
// * user can use Tab to focus in the dropdown, then the dropdown menu (list) will be shown
4620
// * user presses Tab on the focused dropdown to move focus to next sibling focusable element (but not the menu item)
4721
// * user can use arrow key Up/Down to navigate between menu items
4822
// * when user presses Enter:
4923
// - if the menu item is clickable (eg: <a>), then trigger the click event
5024
// - otherwise, the dropdown control (low-level code) handles the Enter event, hides the dropdown menu
5125

52-
// TODO: multiple selection is not supported yet.
53-
// TODO: use combobox for dropdown with search input in the future
26+
const $textSearch = $dropdown.find('input.search').eq(0);
27+
const $focusable = $textSearch.length ? $textSearch : $dropdown; // the primary element for focus, see comment above
28+
if (!$focusable.length) return;
5429

55-
$focusable.attr({
56-
'role': 'menu',
57-
'aria-haspopup': 'menu',
58-
'aria-controls': $menu.attr('id'),
59-
'aria-expanded': 'false',
60-
});
30+
// detect if the dropdown has an input, if yes, it works like a combobox, otherwise it works like a menu
31+
// or use a special class to indicate it's a combobox/menu in the future
32+
const isComboBox = $dropdown.find('input').length > 0;
33+
34+
const focusableRole = isComboBox ? 'combobox' : 'button';
35+
const listPopupRole = isComboBox ? 'listbox' : 'menu';
36+
const listItemRole = isComboBox ? 'option' : 'menuitem';
37+
38+
// make the item has role=option/menuitem, and add an id if there wasn't one yet.
39+
function prepareMenuItem($item) {
40+
if (!$item.attr('id')) $item.attr('id', generateAriaId());
41+
$item.attr({'role': listItemRole, 'tabindex': '-1'});
42+
$item.find('a').attr('tabindex', '-1'); // as above, the elements inside the dropdown menu item should not be focusable, the focus should always be on the dropdown primary element.
43+
}
44+
45+
const dropdownTemplates = $dropdown.dropdown('setting', 'templates');
46+
const dropdownTemplatesMenuOld = dropdownTemplates.menu;
47+
dropdownTemplates.menu = function(response, fields, preserveHTML, className) {
48+
// when the dropdown menu items are loaded from AJAX requests, the items are created dynamically
49+
const ret = dropdownTemplatesMenuOld(response, fields, preserveHTML, className);
50+
const $wrapper = $('<div>').append(ret);
51+
const $items = $wrapper.find('> .item');
52+
$items.each((_, item) => {
53+
prepareMenuItem($(item));
54+
});
55+
return $wrapper.html();
56+
};
57+
$dropdown.dropdown('setting', ['templates', dropdownTemplates]);
58+
59+
60+
// TODO: multiple selection is not supported yet.
6161

62-
if ($dropdown.attr('data-content') && !$dropdown.attr('aria-label')) {
62+
// use tooltip's content as aria-label if there is no aria-label
63+
if ($dropdown.hasClass('tooltip') && $dropdown.attr('data-content') && !$dropdown.attr('aria-label')) {
6364
$dropdown.attr('aria-label', $dropdown.attr('data-content'));
6465
}
6566

67+
// prepare dropdown menu list popup
68+
const $menu = $dropdown.find('> .menu');
69+
if (!$menu.attr('id')) $menu.attr('id', generateAriaId());
70+
$menu.attr('role', listPopupRole);
6671
$menu.find('> .item').each((_, item) => {
6772
prepareMenuItem($(item));
6873
});
6974

75+
$focusable.attr({
76+
'role': focusableRole,
77+
'aria-haspopup': listPopupRole,
78+
'aria-controls': $menu.attr('id'),
79+
'aria-expanded': 'false',
80+
});
81+
7082
// update aria attributes according to current active/selected item
7183
const refreshAria = () => {
7284
const isMenuVisible = !$menu.is('.hidden') && !$menu.is('.animating.out');
@@ -86,7 +98,7 @@ function attachOneDropdownAria($dropdown) {
8698
if (!$item) $item = $menu.find('> .item.selected'); // when dropdown filters items by input, there is no "value", so query the "selected" item
8799
// if the selected item is clickable, then trigger the click event.
88100
// we can not click any item without check, because Fomantic code might also handle the Enter event. that would result in double click.
89-
if ($item && ($item.is('a') || $item.is('.js-aria-clickable'))) $item[0].click();
101+
if ($item && ($item.is('a') || $item.hasClass('js-aria-clickable'))) $item[0].click();
90102
}
91103
});
92104

0 commit comments

Comments
 (0)