Skip to content
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: duplicated IDs in panel #1078

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions packages/autocomplete-core/src/getPropGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,29 @@ export function getPropGetters<
};
};

const getLabelProps: GetLabelProps = (rest) => {
const getAutocompleteId = (instanceId: string, sourceId?: number) => {
return typeof sourceId !== 'undefined'
? `${instanceId}-${sourceId}`
: instanceId;
};
FabienMotte marked this conversation as resolved.
Show resolved Hide resolved

const getLabelProps: GetLabelProps = (providedProps) => {
const { sourceIndex, ...rest } = providedProps || {};

return {
htmlFor: `${props.id}-input`,
id: `${props.id}-label`,
htmlFor: `${getAutocompleteId(props.id, sourceIndex)}-input`,
id: `${getAutocompleteId(props.id, sourceIndex)}-label`,
...rest,
};
};

const getListProps: GetListProps = (rest) => {
const getListProps: GetListProps = (providedProps) => {
const { sourceIndex, ...rest } = providedProps || {};

return {
role: 'listbox',
'aria-labelledby': `${props.id}-label`,
id: `${props.id}-list`,
'aria-labelledby': `${getAutocompleteId(props.id, sourceIndex)}-label`,
id: `${getAutocompleteId(props.id, sourceIndex)}-list`,
...rest,
};
};
Expand All @@ -272,10 +282,12 @@ export function getPropGetters<
};

const getItemProps: GetItemProps<any, TMouseEvent> = (providedProps) => {
const { item, source, ...rest } = providedProps;
const { item, source, sourceIndex, ...rest } = providedProps;

return {
id: `${props.id}-item-${item.__autocomplete_id}`,
id: `${getAutocompleteId(props.id, sourceIndex as number)}-item-${
item.__autocomplete_id
}`,
role: 'option',
'aria-selected': store.getState().activeItemId === item.__autocomplete_id,
onMouseMove(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type GetFormProps<TEvent = Event> = (props: {

export type GetLabelProps = (props?: {
[key: string]: unknown;
sourceIndex?: number;
}) => {
htmlFor: string;
id: string;
Expand Down Expand Up @@ -101,6 +102,7 @@ export type GetPanelProps<TMouseEvent> = (props?: {

export type GetListProps = (props?: {
[key: string]: unknown;
sourceIndex?: number;
}) => {
role: string;
'aria-labelledby': string;
Expand Down
2 changes: 1 addition & 1 deletion packages/autocomplete-js/src/__tests__/detached.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('detached', () => {
});

const firstItem = document.querySelector<HTMLLIElement>(
'#autocomplete-item-0'
'#autocomplete-0-item-0'
);

// Select the first item
Expand Down
6 changes: 3 additions & 3 deletions packages/autocomplete-js/src/__tests__/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ describe('renderer', () => {
data-autocomplete-source-id="testSource"
>
<ul
aria-labelledby="autocomplete-0-label"
aria-labelledby="autocomplete-0-0-label"
class="aa-List"
id="autocomplete-0-list"
id="autocomplete-0-0-list"
role="listbox"
>
<li
aria-selected="false"
class="aa-Item"
id="autocomplete-0-item-0"
id="autocomplete-0-0-item-0"
role="option"
>
1
Expand Down
24 changes: 12 additions & 12 deletions packages/autocomplete-js/src/__tests__/templates.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ describe('templates', () => {
expect(within(panelContainer).getByRole('listbox'))
.toMatchInlineSnapshot(`
<ul
aria-labelledby="autocomplete-0-label"
aria-labelledby="autocomplete-0-0-label"
class="aa-List"
id="autocomplete-0-list"
id="autocomplete-0-0-list"
role="listbox"
>
<li
aria-selected="false"
class="aa-Item"
id="autocomplete-0-item-0"
id="autocomplete-0-0-item-0"
role="option"
>
<div
Expand Down Expand Up @@ -208,15 +208,15 @@ describe('templates', () => {
expect(within(panelContainer).getByRole('listbox'))
.toMatchInlineSnapshot(`
<ul
aria-labelledby="autocomplete-0-label"
aria-labelledby="autocomplete-0-0-label"
class="aa-List"
id="autocomplete-0-list"
id="autocomplete-0-0-list"
role="listbox"
>
<li
aria-selected="false"
class="aa-Item"
id="autocomplete-0-item-0"
id="autocomplete-0-0-item-0"
role="option"
>
<div
Expand Down Expand Up @@ -343,15 +343,15 @@ describe('templates', () => {
</header>
</div>
<ul
aria-labelledby="autocomplete-0-label"
aria-labelledby="autocomplete-0-0-label"
class="aa-List"
id="autocomplete-0-list"
id="autocomplete-0-0-list"
role="listbox"
>
<li
aria-selected="false"
class="aa-Item"
id="autocomplete-0-item-0"
id="autocomplete-0-0-item-0"
role="option"
>
<div
Expand Down Expand Up @@ -507,15 +507,15 @@ describe('templates', () => {
</header>
</div>
<ul
aria-labelledby="autocomplete-0-label"
aria-labelledby="autocomplete-0-0-label"
class="aa-List"
id="autocomplete-0-list"
id="autocomplete-0-0-list"
role="listbox"
>
<li
aria-selected="false"
class="aa-Item"
id="autocomplete-0-item-0"
id="autocomplete-0-0-item-0"
role="option"
>
div
Expand Down
5 changes: 4 additions & 1 deletion packages/autocomplete-js/src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,17 @@ export function renderPanel<TItem extends BaseItem>(
className={classNames.list}
{...propGetters.getListProps({
state,
props: autocomplete.getListProps({}),
props: autocomplete.getListProps({
sourceIndex,
}),
...autocompleteScopeApi,
})}
>
{items.map((item) => {
const itemProps = autocomplete.getItemProps({
item,
source,
sourceIndex,
});

return (
Expand Down