Skip to content

Commit

Permalink
Fix #3672: Listbox keyboard accessibility (#5989)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Feb 16, 2024
1 parent d2549a2 commit 9c9653f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
16 changes: 9 additions & 7 deletions components/lib/listbox/ListBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const ListBox = React.memo(
const [filterValueState, setFilterValueState] = React.useState('');
const elementRef = React.useRef(null);
const virtualScrollerRef = React.useRef(null);
const id = React.useRef(UniqueComponentId());
const id = React.useRef(null);
const listRef = React.useRef(null);
const optionTouched = React.useRef(false);
const filteredValue = (props.onFilterValueChange ? props.filterValue : filterValueState) || '';
Expand Down Expand Up @@ -261,7 +261,7 @@ export const ListBox = React.memo(
setFocusedOptionIndex(index);
scrollInView();

if (props.selectOnFocus && !props.multiple) {
if (event && props.selectOnFocus && !props.multiple) {
onOptionSelect(event, visibleOptions[index]);
}
}
Expand Down Expand Up @@ -391,7 +391,7 @@ export const ListBox = React.memo(
break;

case 'Tab':
//NOOP
// NOOP
break;

case 'ShiftLeft':
Expand Down Expand Up @@ -544,6 +544,7 @@ export const ListBox = React.memo(

lastHiddenFocusableElement.current.tabIndex = DomHandler.isElement(firstFocusableEl) ? undefined : -1;
firstHiddenFocusableElement.current.tabIndex = -1;
changeFocusedOptionIndex(null, 0);
};

const onLastHiddenFocus = (event) => {
Expand Down Expand Up @@ -617,6 +618,7 @@ export const ListBox = React.memo(

useMountEffect(() => {
scrollToSelectedIndex();
id.current = UniqueComponentId();
});

const createHeader = () => {
Expand Down Expand Up @@ -644,12 +646,12 @@ export const ListBox = React.memo(
const optionLabel = getOptionLabel(option);
const optionKey = j + '_' + getOptionRenderKey(option);
const disabled = isOptionDisabled(option);
const tabIndex = disabled ? null : props.tabIndex || 0;

return (
<ListBoxItem
id={id.current + '_' + j}
hostName="ListBox"
optionKey={optionKey}
key={optionKey}
label={optionLabel}
option={option}
Expand Down Expand Up @@ -695,12 +697,12 @@ export const ListBox = React.memo(
const optionLabel = getOptionLabel(option);
const optionKey = index + '_' + getOptionRenderKey(option);
const disabled = isOptionDisabled(option);
const tabIndex = disabled ? null : props.tabIndex || 0;

return (
<ListBoxItem
id={id.current + '_' + index}
hostName="ListBox"
optionKey={optionKey}
key={optionKey}
label={optionLabel}
index={index}
Expand Down Expand Up @@ -753,7 +755,7 @@ export const ListBox = React.memo(
contentTemplate: (options) => {
const listProps = mergeProps(
{
ref: options.contentRef,
ref: listRef,
style: ptCallbacks.sx('list', { options }),
className: ptCallbacks.cx('list', { options }),
role: 'listbox',
Expand Down Expand Up @@ -842,7 +844,7 @@ export const ListBox = React.memo(
'data-p-hidden-accessible': true,
'data-p-hidden-focusable': true
},
ptCallbacks.ptm('hiddenFirstFocusableEl')
ptCallbacks.ptm('hiddenLastFocusableEl')
);

return (
Expand Down
3 changes: 1 addition & 2 deletions components/lib/listbox/ListBoxItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const ListBoxItem = React.memo((props) => {
};

const content = props.template ? ObjectUtils.getJSXElement(props.template, props.option) : props.label;

const itemProps = mergeProps(
{
id: props.id,
Expand All @@ -65,7 +64,7 @@ export const ListBoxItem = React.memo((props) => {
onBlur: onBlur,
tabIndex: '-1',
'aria-label': props.label,
key: props.label,
key: props.optionKey,
role: 'option',
'aria-selected': props.selected,
'aria-disabled': props.disabled,
Expand Down

0 comments on commit 9c9653f

Please sign in to comment.