Skip to content

Commit a2b17ef

Browse files
committed
fix: added a function to set a key based on the index if the placeId is missing
1 parent dd48fce commit a2b17ef

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/suggest-list.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ export default class SuggestList extends React.PureComponent<Props, unknown> {
4141
return this.props.isHidden || this.props.suggests.length === 0;
4242
}
4343

44+
/**
45+
* Generate a unique key for each suggestion
46+
*/
47+
getSuggestionKey(suggest: Suggest, index: number): string {
48+
// Use placeId if available, otherwise fall back to combination of label and index
49+
return suggest.placeId || `${suggest.label}_${index}`;
50+
}
51+
4452
/**
4553
* There are new properties available for the list
4654
*/
@@ -75,15 +83,15 @@ export default class SuggestList extends React.PureComponent<Props, unknown> {
7583
role="listbox"
7684
aria-label={this.props.listLabel}
7785
id={this.props.listId}>
78-
{this.props.suggests.map((suggest) => {
86+
{this.props.suggests.map((suggest, index) => {
7987
const isActive =
8088
(this.props.activeSuggest &&
8189
suggest.placeId === this.props.activeSuggest.placeId) ||
8290
false;
8391

8492
return (
8593
<SuggestItem
86-
key={suggest.placeId}
94+
key={this.getSuggestionKey(suggest, index)}
8795
className={suggest.className || ''}
8896
userInput={this.props.userInput}
8997
isHighlightMatch={this.props.isHighlightMatch}

0 commit comments

Comments
 (0)