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: Recalculate the sections only on actual change #37341

Merged
merged 4 commits into from
Feb 28, 2024
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
25 changes: 16 additions & 9 deletions src/components/OptionsSelector/BaseOptionsSelector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useIsFocused} from '@react-navigation/native';
import lodashGet from 'lodash/get';
import lodashIsEqual from 'lodash/isEqual';
import PropTypes from 'prop-types';
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
import {ScrollView, View} from 'react-native';
Expand All @@ -14,6 +15,7 @@ import ShowMoreButton from '@components/ShowMoreButton';
import TextInput from '@components/TextInput';
import useKeyboardShortcut from '@hooks/useKeyboardShortcut';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import useThemeStyles from '@hooks/useThemeStyles';
import getPlatform from '@libs/getPlatform';
import KeyboardShortcut from '@libs/KeyboardShortcut';
Expand Down Expand Up @@ -83,7 +85,6 @@ function BaseOptionsSelector(props) {
const accessibilityRoles = _.values(CONST.ROLE);

const [disabledOptionsIndexes, setDisabledOptionsIndexes] = useState([]);
const [sections, setSections] = useState();
const [shouldDisableRowSelection, setShouldDisableRowSelection] = useState(false);
const [errorMessage, setErrorMessage] = useState('');
const [value, setValue] = useState('');
Expand All @@ -100,6 +101,7 @@ function BaseOptionsSelector(props) {
const prevPaginationPage = useRef(paginationPage);
const prevSelectedOptions = useRef(props.selectedOptions);
const prevValue = useRef(value);
const previousSections = usePrevious(props.sections);

useImperativeHandle(props.forwardedRef, () => textInputRef.current);

Expand Down Expand Up @@ -134,16 +136,10 @@ function BaseOptionsSelector(props) {
return calcAllOptions;
}, [props.sections]);

// eslint-disable-next-line react-hooks/exhaustive-deps
const initialAllOptions = useMemo(() => flattenSections(), []);
const [allOptions, setAllOptions] = useState(initialAllOptions);
const [focusedIndex, setFocusedIndex] = useState(getInitiallyFocusedIndex(initialAllOptions));
const [focusedOption, setFocusedOption] = useState(allOptions[focusedIndex]);

/**
* Maps sections to render only allowed count of them per section.
*
* @returns {Objects[]}
* @returns {Object[]}
*/
const sliceSections = useCallback(
() =>
Expand All @@ -162,6 +158,13 @@ function BaseOptionsSelector(props) {
[paginationPage, props.sections],
);

// eslint-disable-next-line react-hooks/exhaustive-deps
const initialAllOptions = useMemo(() => flattenSections(), []);
const [sections, setSections] = useState(sliceSections());
const [allOptions, setAllOptions] = useState(initialAllOptions);
const [focusedIndex, setFocusedIndex] = useState(getInitiallyFocusedIndex(initialAllOptions));
const [focusedOption, setFocusedOption] = useState(allOptions[focusedIndex]);

/**
* Completes the follow-up actions after a row is selected
*
Expand Down Expand Up @@ -415,6 +418,10 @@ function BaseOptionsSelector(props) {
}, [isFocused, props.autoFocus]);

useEffect(() => {
if (lodashIsEqual(props.sections, previousSections)) {
return;
}

const newSections = sliceSections();
const newOptions = flattenSections();

Expand All @@ -434,7 +441,7 @@ function BaseOptionsSelector(props) {
setFocusedIndex(prevFocusedOptionIndex || (_.isNumber(props.focusedIndex) ? props.focusedIndex : newFocusedIndex));
// we want to run this effect only when the sections change
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.sections]);
}, [props.sections, previousSections]);

useEffect(() => {
// If we just toggled an option on a multi-selection page or cleared the search input, scroll to top
Expand Down
Loading