Skip to content

Commit

Permalink
fix: prevent id conflicts when rendering multiple ListEntries components
Browse files Browse the repository at this point in the history
  • Loading branch information
jwertkin authored and jcestibariz committed Feb 3, 2025
1 parent 3466a75 commit ae73ce2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/pelagos/__tests__/listInput/ListEntries-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import ListEntries from '../../src/listInput/ListEntries';
import renderListItem from '../../src/listItems/renderListItem';
import scrollIntoView from '../../src/functions/scrollIntoView';
import moveListItem from '../../src/functions/moveListItem';
import useRandomId from '../../src/hooks/useRandomId';
import useReorder from '../../src/hooks/useReorder';

jest.unmock('../../src/listInput/ListEntries');

jest.mock('lodash-es/debounce', () => jest.fn((f) => ((f.cancel = jest.fn()), f)));

useRandomId.mockReturnValue('random-id');

const anyFunction = expect.any(Function);

const list = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`ListEntries rendering renders expected elements 1`] = `
/>
<ul
className="ListEntries ListEntries--grid TestClass"
id="test"
id="random-id"
onClick={[Function]}
>
<Layer
Expand Down Expand Up @@ -44,7 +44,7 @@ exports[`ListEntries rendering renders expected elements when column is set 1`]
/>
<ul
className="ListEntries ListEntries--column"
id="test"
id="random-id"
onClick={[Function]}
>
<Layer
Expand Down Expand Up @@ -80,7 +80,7 @@ exports[`ListEntries rendering renders expected elements when highlightKey is se
/>
<ul
className="ListEntries ListEntries--grid"
id="test"
id="random-id"
onClick={[Function]}
>
<Layer
Expand Down Expand Up @@ -116,7 +116,7 @@ exports[`ListEntries rendering renders expected elements when renderItem is not
/>
<ul
className="ListEntries ListEntries--grid"
id="test"
id="random-id"
onClick={[Function]}
>
<Layer
Expand Down Expand Up @@ -152,17 +152,17 @@ exports[`ListEntries rendering renders expected elements when reorderable is set
/>
<div
className="sr-only"
id="test-operation"
id="random-id-operation"
>
Press space bar to reorder
</div>
<ul
className="ListEntries ListEntries--column"
id="test"
id="random-id"
onClick={[Function]}
>
<Layer
aria-describedby="test-operation"
aria-describedby="random-id-operation"
as="li"
className="ListEntries__item"
data-index={0}
Expand Down Expand Up @@ -203,7 +203,7 @@ exports[`ListEntries rendering renders expected elements when the item has class
/>
<ul
className="ListEntries ListEntries--grid"
id="test"
id="random-id"
onClick={[Function]}
>
<Layer
Expand Down
2 changes: 2 additions & 0 deletions packages/pelagos/src/listInput/ListEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {cloneElement, useCallback, useEffect, useMemo} from 'react';
import Layer from '../components/Layer';
import moveListItem from '../functions/moveListItem';
import scrollIntoView from '../functions/scrollIntoView';
import useRandomId from '../hooks/useRandomId';
import useReorder from '../hooks/useReorder';
import renderListItem from '../listItems/renderListItem';

Expand Down Expand Up @@ -68,6 +69,7 @@ const ListEntries = ({
return clearHighlight && clearHighlight.cancel;
}, [highlightKey, clearHighlight]);

id = useRandomId(id);
const operationId = `${id}-operation`;
return (
<>
Expand Down

0 comments on commit ae73ce2

Please sign in to comment.