Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit be070a4

Browse files
authored
fix(list): Accept array of index for selectedIndex API (#4124)
BREAKING CHANGE: Introduced new adapter `isFocusInsideList` for MDC List for improved accessibility.
1 parent 96f663e commit be070a4

File tree

7 files changed

+572
-198
lines changed

7 files changed

+572
-198
lines changed

packages/mdc-list/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ When rendering list with checkbox items all pre-selected list items should conta
359359
</ul>
360360
```
361361

362+
The `selectedIndex` (that proxies foundation's `setSelectedState()`) accepts list of indexes in array format for list with checkbox items to set the selection state. It overwrites the current state with new selected state.
363+
362364
## Style Customization
363365

364366
### CSS Classes
@@ -509,6 +511,7 @@ Method Signature | Description
509511
`hasCheckboxAtIndex(index: number) => boolean` | Returns true if checkbox is present at given list item index.
510512
`isCheckboxCheckedAtIndex(index: number) => boolean` | Returns true if checkbox inside a list item is checked.
511513
`setCheckedCheckboxOrRadioAtIndex(index: number, isChecked: boolean) => void` | Sets the checked status of checkbox or radio at given list item index.
514+
`isFocusInsideList() => boolean` | Returns true if the current focused element is inside list root.
512515

513516
### `MDCListFoundation`
514517

@@ -517,13 +520,14 @@ Method Signature | Description
517520
`setWrapFocus(value: Boolean) => void` | Sets the list to allow the up arrow on the first element to focus the last element of the list and vice versa.
518521
`setVerticalOrientation(value: Boolean) => void` | Sets the list to an orientation causing the keys used for navigation to change. `true` results in the Up/Down arrow keys being used. `false` results in the Left/Right arrow keys being used.
519522
`setSingleSelection(value: Boolean) => void` | Sets the list to be a selection list. Enables the `enter` and `space` keys for selecting/deselecting a list item.
520-
`setSelectedIndex(index: Number) => void` | Toggles the `selected` state of the list item at index `index`.
523+
`getSelectedIndex() => Index` | Gets the current selection state by returning selected index or list of indexes for checkbox based list. See [constants.js](./constants.js) for `Index` type definition.
524+
`setSelectedIndex(index: Index) => void` | Sets the selection state to given index or list of indexes if it is checkbox based list. See [constants.js](./constants.js) for `Index` type definition.
521525
`setUseActivated(useActivated: boolean) => void` | Sets the selection logic to apply/remove the `mdc-list-item--activated` class.
522526
`handleFocusIn(evt: Event) => void` | Handles the changing of `tabindex` to `0` for all button and anchor elements when a list item receives focus.
523527
`handleFocusOut(evt: Event) => void` | Handles the changing of `tabindex` to `-1` for all button and anchor elements when a list item loses focus.
524528
`handleKeydown(evt: Event) => void` | Handles determining if a focus action should occur when a key event is triggered.
525529
`handleClick(evt: Event) => void` | Handles toggling the selected/deselected state for a list item when clicked. This method is only used by the single selection list.
526-
`focusNextElement(index: Number) => void` | Handles focusing the next element using the current `index`.
527-
`focusPrevElement(index: Number) => void` | Handles focusing the previous element using the current `index`.
528-
`focusFirstElement() => void` | Handles focusing the first element in a list.
529-
`focusLastElement() => void` | Handles focusing the last element in a list.
530+
`focusNextElement(index: number) => number` | Handles focusing the next element using the current `index`. Returns focused element index.
531+
`focusPrevElement(index: number) => number` | Handles focusing the previous element using the current `index`. Returns focused element index.
532+
`focusFirstElement() => number` | Handles focusing the first element in a list. Returns focused element index.
533+
`focusLastElement() => number` | Handles focusing the last element in a list. Returns focused element index.

packages/mdc-list/adapter.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ class MDCListAdapter {
113113
* @param {boolean} isChecked
114114
*/
115115
setCheckedCheckboxOrRadioAtIndex(index, isChecked) {}
116+
117+
/**
118+
* @return {boolean} Returns true when the current focused element is inside list root.
119+
*/
120+
isFocusInsideList() {}
116121
}
117122

118123
export default MDCListAdapter;

packages/mdc-list/constants.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const strings = {
3636
ARIA_SELECTED: 'aria-selected',
3737
ARIA_CHECKED: 'aria-checked',
3838
ARIA_CHECKED_RADIO_SELECTOR: '[role="radio"][aria-checked="true"]',
39+
ARIA_ROLE_CHECKBOX_SELECTOR: '[role="checkbox"]',
40+
ARIA_CHECKED_CHECKBOX_SELECTOR: '[role="checkbox"][aria-checked="true"]',
3941
RADIO_SELECTOR: 'input[type="radio"]:not(:disabled)',
4042
CHECKBOX_SELECTOR: 'input[type="checkbox"]:not(:disabled)',
4143
CHECKBOX_RADIO_SELECTOR: 'input[type="checkbox"]:not(:disabled), input[type="radio"]:not(:disabled)',
@@ -47,4 +49,7 @@ const strings = {
4749
ENABLED_ITEMS_SELECTOR: '.mdc-list-item:not(.mdc-list-item--disabled)',
4850
};
4951

50-
export {strings, cssClasses};
52+
/** @typedef {number|!Array<number>} */
53+
let Index;
54+
55+
export {strings, cssClasses, Index};

0 commit comments

Comments
 (0)