Skip to content

Commit

Permalink
fix: added a function to set a key based on the index if the placeId …
Browse files Browse the repository at this point in the history
…is missing
  • Loading branch information
nilsingwersen committed Jan 8, 2025
1 parent dd48fce commit a2b17ef
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/suggest-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export default class SuggestList extends React.PureComponent<Props, unknown> {
return this.props.isHidden || this.props.suggests.length === 0;
}

/**
* Generate a unique key for each suggestion
*/
getSuggestionKey(suggest: Suggest, index: number): string {
// Use placeId if available, otherwise fall back to combination of label and index
return suggest.placeId || `${suggest.label}_${index}`;
}

/**
* There are new properties available for the list
*/
Expand Down Expand Up @@ -75,15 +83,15 @@ export default class SuggestList extends React.PureComponent<Props, unknown> {
role="listbox"
aria-label={this.props.listLabel}
id={this.props.listId}>
{this.props.suggests.map((suggest) => {
{this.props.suggests.map((suggest, index) => {
const isActive =
(this.props.activeSuggest &&
suggest.placeId === this.props.activeSuggest.placeId) ||
false;

return (
<SuggestItem
key={suggest.placeId}
key={this.getSuggestionKey(suggest, index)}
className={suggest.className || ''}
userInput={this.props.userInput}
isHighlightMatch={this.props.isHighlightMatch}
Expand Down

0 comments on commit a2b17ef

Please sign in to comment.