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/35945: Selected category is not highlighted #36596

Merged
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: 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
Loading