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 duplicate key error in ListBox.js #1398

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 6 additions & 4 deletions src/components/dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,12 @@ export class Dropdown extends Component {
}

if (items) {
return items.map((option) => {
return items.map((option, i) => {
let optionLabel = this.getOptionLabel(option);
let optionKey = this.getOptionKey(option, i);

return (
<DropdownItem key={this.getOptionKey(option)} label={optionLabel} option={option} template={this.props.itemTemplate} selected={selectedOption === option} disabled={option.disabled} onClick={this.onOptionClick} />
<DropdownItem key={optionKey} label={optionLabel} option={option} template={this.props.itemTemplate} selected={selectedOption === option} disabled={option.disabled} onClick={this.onOptionClick} />
);
});
}
Expand Down Expand Up @@ -649,8 +651,8 @@ export class Dropdown extends Component {
return this.props.optionValue ? ObjectUtils.resolveFieldData(option, this.props.optionValue) : (option['value'] !== undefined ? option['value'] : option);
}

getOptionKey(option) {
return this.props.dataKey ? ObjectUtils.resolveFieldData(option, this.props.dataKey) : this.getOptionLabel(option);
getOptionKey(option, i) {
return this.props.dataKey ? ObjectUtils.resolveFieldData(option, this.props.dataKey) : `${this.getOptionLabel(option)}-${i}`;
}

checkValidity() {
Expand Down
3 changes: 2 additions & 1 deletion src/components/listbox/ListBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,10 @@ export class ListBox extends Component {

items = items.map((option, index) => {
let optionLabel = this.getOptionLabel(option);
let optionKey = `${optionLabel}_${index}`;

return (
<ListBoxItem key={optionLabel} label={optionLabel} option={option} template={this.props.itemTemplate} selected={this.isSelected(option)}
<ListBoxItem key={optionKey} label={optionLabel} option={option} template={this.props.itemTemplate} selected={this.isSelected(option)}
onClick={this.onOptionClick} onTouchEnd={(e) => this.onOptionTouchEnd(e, option, index)} tabIndex={this.props.tabIndex} />
);
});
Expand Down