-
Notifications
You must be signed in to change notification settings - Fork 329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: generate elements ids in a consistent manner #1194
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,12 @@ import { | |
GetRootProps, | ||
InternalAutocompleteOptions, | ||
} from './types'; | ||
import { getActiveItem, isOrContainsNode, isSamsung } from './utils'; | ||
import { | ||
getActiveItem, | ||
getAutocompleteElementId, | ||
isOrContainsNode, | ||
isSamsung, | ||
} from './utils'; | ||
|
||
interface GetPropGettersOptions<TItem extends BaseItem> | ||
extends AutocompleteScopeApi<TItem> { | ||
|
@@ -104,8 +109,15 @@ export function getPropGetters< | |
role: 'combobox', | ||
'aria-expanded': store.getState().isOpen, | ||
'aria-haspopup': 'listbox', | ||
'aria-owns': store.getState().isOpen ? `${props.id}-list` : undefined, | ||
'aria-labelledby': `${props.id}-label`, | ||
'aria-owns': store.getState().isOpen | ||
? store | ||
.getState() | ||
.collections.map(({ source }) => | ||
getAutocompleteElementId(props.id, 'list', source) | ||
) | ||
.join(' ') | ||
: undefined, | ||
Comment on lines
+112
to
+119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is conform to the reference, making Autocomplete the owner of all the sources' lists. |
||
'aria-labelledby': getAutocompleteElementId(props.id, 'label'), | ||
...rest, | ||
}; | ||
}; | ||
|
@@ -180,12 +192,23 @@ export function getPropGetters< | |
'aria-autocomplete': 'both', | ||
'aria-activedescendant': | ||
store.getState().isOpen && store.getState().activeItemId !== null | ||
? `${props.id}-item-${store.getState().activeItemId}` | ||
? getAutocompleteElementId( | ||
props.id, | ||
`item-${store.getState().activeItemId}`, | ||
activeItem?.source | ||
) | ||
: undefined, | ||
'aria-controls': store.getState().isOpen ? `${props.id}-list` : undefined, | ||
'aria-labelledby': `${props.id}-label`, | ||
'aria-controls': store.getState().isOpen | ||
? store | ||
.getState() | ||
.collections.map(({ source }) => | ||
getAutocompleteElementId(props.id, 'list', source) | ||
) | ||
.join(' ') | ||
: undefined, | ||
Comment on lines
+201
to
+208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gives a comprehensive value of all lists that are controlled by the input (MDN). |
||
'aria-labelledby': getAutocompleteElementId(props.id, 'label'), | ||
value: store.getState().completion || store.getState().query, | ||
id: `${props.id}-input`, | ||
id: getAutocompleteElementId(props.id, 'input'), | ||
autoComplete: 'off', | ||
autoCorrect: 'off', | ||
autoCapitalize: 'off', | ||
|
@@ -241,29 +264,21 @@ export function getPropGetters< | |
}; | ||
}; | ||
|
||
const getAutocompleteId = (instanceId: string, sourceId?: number) => { | ||
return typeof sourceId !== 'undefined' | ||
? `${instanceId}-${sourceId}` | ||
: instanceId; | ||
}; | ||
|
||
const getLabelProps: GetLabelProps = (providedProps) => { | ||
const { sourceIndex, ...rest } = providedProps || {}; | ||
|
||
const getLabelProps: GetLabelProps = (rest) => { | ||
return { | ||
htmlFor: `${getAutocompleteId(props.id, sourceIndex)}-input`, | ||
id: `${getAutocompleteId(props.id, sourceIndex)}-label`, | ||
htmlFor: getAutocompleteElementId(props.id, 'input'), | ||
id: getAutocompleteElementId(props.id, 'label'), | ||
...rest, | ||
}; | ||
}; | ||
|
||
const getListProps: GetListProps = (providedProps) => { | ||
const { sourceIndex, ...rest } = providedProps || {}; | ||
const { source, ...rest } = providedProps || {}; | ||
|
||
return { | ||
role: 'listbox', | ||
'aria-labelledby': `${getAutocompleteId(props.id, sourceIndex)}-label`, | ||
id: `${getAutocompleteId(props.id, sourceIndex)}-list`, | ||
'aria-labelledby': getAutocompleteElementId(props.id, 'label'), | ||
id: getAutocompleteElementId(props.id, 'list', source), | ||
...rest, | ||
}; | ||
}; | ||
|
@@ -284,12 +299,14 @@ export function getPropGetters< | |
}; | ||
|
||
const getItemProps: GetItemProps<any, TMouseEvent> = (providedProps) => { | ||
const { item, source, sourceIndex, ...rest } = providedProps; | ||
const { item, source, ...rest } = providedProps; | ||
|
||
return { | ||
id: `${getAutocompleteId(props.id, sourceIndex as number)}-item-${ | ||
item.__autocomplete_id | ||
}`, | ||
id: getAutocompleteElementId( | ||
props.id, | ||
`item-${item.__autocomplete_id}`, | ||
source | ||
), | ||
role: 'option', | ||
'aria-selected': store.getState().activeItemId === item.__autocomplete_id, | ||
onMouseMove(event) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't create a specific test case for this simple method, because its output is testable on multiple occasions in other integration tests and snapshots. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { InternalAutocompleteSource } from '../types'; | ||
|
||
/** | ||
* Returns a full element id for an autocomplete element. | ||
* | ||
* @param autocompleteInstanceId The id of the autocomplete instance | ||
* @param elementId The specific element id | ||
* @param source The source of the element, when it needs to be scoped | ||
*/ | ||
export function getAutocompleteElementId( | ||
autocompleteInstanceId: string, | ||
elementId: string, | ||
source?: InternalAutocompleteSource<any> | ||
) { | ||
return [autocompleteInstanceId, source?.sourceId, elementId] | ||
.filter(Boolean) | ||
.join('-') | ||
.replace(/\s/g, ''); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bit unfortunate that this gets calculated again instead of saved in the collection, but it makes sense