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 3 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
12 changes: 9 additions & 3 deletions src/components/OptionsSelector/BaseOptionsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {Component} from 'react';
import {ScrollView, View} from 'react-native';
import _ from 'underscore';
import _, {find, findIndex} from 'underscore';
dukenv0307 marked this conversation as resolved.
Show resolved Hide resolved
import ArrowKeyFocusManager from '@components/ArrowKeyFocusManager';
import Button from '@components/Button';
import FixedFooter from '@components/FixedFooter';
Expand Down 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