Skip to content

Commit d9f791f

Browse files
mocca102Mudaafi
andauthored
Fix tabIndex accessibility for Search Suggestions items (#240)
* Fix tabIndex accessibility for Search Suggestoins items * Conditionally add non interactive item props * Remove space key * Update src/hooks/useCioAutocomplete.ts Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io> * Update src/hooks/useCioAutocomplete.ts Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io> * Rename e -> event --------- Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io>
1 parent eeeb470 commit d9f791f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/hooks/useCioAutocomplete.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
117117
const { index, sectionId } = getItemPosition({ item, items });
118118
const sectionItemTestId = `cio-item-${sectionId?.replace(' ', '')}`;
119119

120+
// Products always have links, Search Suggestions with getSearchResultsUrl have links
121+
const hasLink =
122+
item.data?.url || (item.section === 'Search Suggestions' && getSearchResultsUrl) || false;
123+
124+
const nonInteractiveItemsProps = {
125+
tabIndex: 0,
126+
onKeyDown: (event: React.KeyboardEvent) => {
127+
const { code, key } = event;
128+
const isEnter = code === 'Enter' || key === 'Enter';
129+
130+
if (isEnter) {
131+
event.preventDefault();
132+
// Trigger default click behavior
133+
getItemPropsDownShift({ item, index }).onClick(event);
134+
}
135+
},
136+
};
137+
120138
return {
121139
...getItemPropsDownShift({ item, index }),
122140
// @deprecated `sectionItemTestId` will be removed as a className in the next major version
@@ -126,6 +144,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
126144
'data-cnstrc-item-group': item.groupId,
127145
'data-cnstrc-item-name': item.value,
128146
'data-cnstrc-item-id': item.data?.id,
147+
...(hasLink ? {} : nonInteractiveItemsProps),
129148
};
130149
},
131150
getInputProps: () => ({

0 commit comments

Comments
 (0)