Skip to content

Commit

Permalink
Merge pull request #36596 from dukenv0307/fix/35945-selected-category…
Browse files Browse the repository at this point in the history
…-not-highlighted

Fix/35945: Selected category is not highlighted
  • Loading branch information
luacmartins authored Feb 20, 2024
2 parents 126572d + 54a92b6 commit a21cfbc
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/components/OptionsSelector/BaseOptionsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class BaseOptionsSelector extends Component {
const allOptions = this.flattenSections();
const sections = this.sliceSections();
const focusedIndex = this.getInitiallyFocusedIndex(allOptions);
this.focusedOption = allOptions[focusedIndex];

this.state = {
sections,
Expand Down Expand Up @@ -146,6 +147,10 @@ class BaseOptionsSelector extends Component {
});
}

if (prevState.focusedIndex !== this.state.focusedIndex) {
this.focusedOption = this.state.allOptions[this.state.focusedIndex];
}

if (_.isEqual(this.props.sections, prevProps.sections)) {
return;
}
Expand All @@ -162,13 +167,14 @@ class BaseOptionsSelector extends Component {
}
const newFocusedIndex = this.props.selectedOptions.length;
const isNewFocusedIndex = newFocusedIndex !== this.state.focusedIndex;

const prevFocusedOption = _.find(newOptions, (option) => this.focusedOption && option.keyForList === this.focusedOption.keyForList);
const prevFocusedOptionIndex = prevFocusedOption ? _.findIndex(newOptions, (option) => this.focusedOption && option.keyForList === this.focusedOption.keyForList) : undefined;
// eslint-disable-next-line react/no-did-update-set-state
this.setState(
{
sections: newSections,
allOptions: newOptions,
focusedIndex: _.isNumber(this.props.focusedIndex) ? this.props.focusedIndex : newFocusedIndex,
focusedIndex: prevFocusedOptionIndex || (_.isNumber(this.props.focusedIndex) ? this.props.focusedIndex : newFocusedIndex),
},
() => {
// If we just toggled an option on a multi-selection page or cleared the search input, scroll to top
Expand Down

0 comments on commit a21cfbc

Please sign in to comment.