Repository navigation
Make SearchSingleSelectionPicker use new SelectionListWithSections
#81001
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import React, {useCallback, useEffect, useMemo, useState} from 'react'; | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import SelectionList from '@components/SelectionListWithSections'; | ||
| import SingleSelectListItem from '@components/SelectionListWithSections/SingleSelectListItem'; | ||
| import React, {useEffect, useState} from 'react'; | ||
| import SingleSelectListItem from '@components/SelectionList/ListItem/SingleSelectListItem'; | ||
| import SelectionList from '@components/SelectionList/SelectionListWithSections'; | ||
| import useDebouncedState from '@hooks/useDebouncedState'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
|
|
@@ -44,98 +43,93 @@ function SearchSingleSelectionPicker({ | |
| setSelectedItem(initiallySelectedItem); | ||
| }, [initiallySelectedItem]); | ||
|
|
||
| const {sections, noResultsFound} = useMemo(() => { | ||
| const initiallySelectedItemSection = initiallySelectedItem?.name.toLowerCase().includes(debouncedSearchTerm?.toLowerCase()) | ||
| ? [ | ||
| { | ||
| text: initiallySelectedItem.name, | ||
| keyForList: initiallySelectedItem.value, | ||
| isSelected: selectedItem?.value === initiallySelectedItem.value, | ||
| value: initiallySelectedItem.value, | ||
| }, | ||
| ] | ||
| : []; | ||
| const remainingItemsSection = items | ||
| .filter((item) => item?.value !== initiallySelectedItem?.value && item?.name?.toLowerCase().includes(debouncedSearchTerm?.toLowerCase())) | ||
| .sort((a, b) => sortOptionsWithEmptyValue(a.name.toString(), b.name.toString(), localeCompare)) | ||
| .map((item) => ({ | ||
| text: item.name, | ||
| keyForList: item.value, | ||
| isSelected: selectedItem?.value === item.value, | ||
| value: item.value, | ||
| })); | ||
| const isEmpty = !initiallySelectedItemSection.length && !remainingItemsSection.length; | ||
| return { | ||
| sections: isEmpty | ||
| ? [] | ||
| : [ | ||
| { | ||
| title: undefined, | ||
| data: initiallySelectedItemSection, | ||
| shouldShow: initiallySelectedItemSection.length > 0, | ||
| indexOffset: 0, | ||
| }, | ||
| { | ||
| title: pickerTitle, | ||
| data: remainingItemsSection, | ||
| shouldShow: remainingItemsSection.length > 0, | ||
| indexOffset: initiallySelectedItemSection.length, | ||
| }, | ||
| ], | ||
| noResultsFound: isEmpty, | ||
| }; | ||
| }, [initiallySelectedItem, selectedItem?.value, items, pickerTitle, debouncedSearchTerm, localeCompare]); | ||
|
|
||
| const onSelectItem = useCallback( | ||
| (item: Partial<OptionData & SearchSingleSelectionPickerItem>) => { | ||
| if (!item.text || !item.keyForList || !item.value) { | ||
| return; | ||
| } | ||
| if (shouldAutoSave) { | ||
| onSaveSelection(item.isSelected ? '' : item.value); | ||
| Navigation.goBack(backToRoute ?? ROUTES.SEARCH_ADVANCED_FILTERS.getRoute()); | ||
| return; | ||
| } | ||
| if (!item.isSelected) { | ||
| setSelectedItem({name: item.text, value: item.value}); | ||
| } | ||
| }, | ||
| [shouldAutoSave, backToRoute, onSaveSelection], | ||
| ); | ||
| const initiallySelectedItemSection = initiallySelectedItem?.name.toLowerCase().includes(debouncedSearchTerm?.toLowerCase()) | ||
| ? [ | ||
| { | ||
| text: initiallySelectedItem.name, | ||
| keyForList: initiallySelectedItem.value, | ||
| isSelected: selectedItem?.value === initiallySelectedItem.value, | ||
| value: initiallySelectedItem.value, | ||
| }, | ||
| ] | ||
| : []; | ||
|
|
||
| const remainingItemsSection = items | ||
| .filter((item) => item.value !== initiallySelectedItem?.value && item.name.toLowerCase().includes(debouncedSearchTerm?.toLowerCase())) | ||
| .sort((a, b) => sortOptionsWithEmptyValue(a.name.toString(), b.name.toString(), localeCompare)) | ||
| .map((item) => ({ | ||
| text: item.name, | ||
| keyForList: item.value, | ||
| isSelected: selectedItem?.value === item.value, | ||
| value: item.value, | ||
| })); | ||
|
|
||
| const noResultsFound = !initiallySelectedItemSection.length && !remainingItemsSection.length; | ||
|
|
||
| const sections = noResultsFound | ||
| ? [] | ||
| : [ | ||
| { | ||
| title: undefined, | ||
| data: initiallySelectedItemSection, | ||
| sectionIndex: 0, | ||
| }, | ||
| { | ||
| title: pickerTitle, | ||
| data: remainingItemsSection, | ||
| sectionIndex: 1, | ||
| }, | ||
| ]; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)The Suggested fix: const onSelectItem = useCallback(
(item: Partial<OptionData & SearchSingleSelectionPickerItem>) => {
if (\!item.text || \!item.keyForList || \!item.value) {
return;
}
if (shouldAutoSave) {
onSaveSelection(item.isSelected ? '' : item.value);
Navigation.goBack(backToRoute ?? ROUTES.SEARCH_ADVANCED_FILTERS.getRoute());
return;
}
if (\!item.isSelected) {
setSelectedItem({name: item.text, value: item.value});
}
},
[shouldAutoSave, backToRoute, onSaveSelection],
);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK this component can be compiled by React Compiler so the |
||
| const resetChanges = useCallback(() => { | ||
| const onSelectItem = (item: Partial<OptionData & SearchSingleSelectionPickerItem>) => { | ||
| if (!item.text || !item.keyForList || !item.value) { | ||
| return; | ||
| } | ||
| if (shouldAutoSave) { | ||
| onSaveSelection(item.isSelected ? '' : item.value); | ||
| Navigation.goBack(backToRoute ?? ROUTES.SEARCH_ADVANCED_FILTERS.getRoute()); | ||
| return; | ||
| } | ||
| if (!item.isSelected) { | ||
| setSelectedItem({name: item.text, value: item.value}); | ||
| } | ||
| }; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)The Suggested fix: const resetChanges = useCallback(() => {
setSelectedItem(undefined);
}, []);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK this component can be compiled by React Compiler so the |
||
| const resetChanges = () => { | ||
| setSelectedItem(undefined); | ||
| }, []); | ||
| }; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)The Suggested fix: const applyChanges = useCallback(() => {
onSaveSelection(selectedItem?.value);
Navigation.goBack(backToRoute ?? ROUTES.SEARCH_ADVANCED_FILTERS.getRoute());
}, [onSaveSelection, selectedItem?.value, backToRoute]);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK this component can be compiled by React Compiler so the |
||
| const applyChanges = useCallback(() => { | ||
| const applyChanges = () => { | ||
| onSaveSelection(selectedItem?.value); | ||
| Navigation.goBack(backToRoute ?? ROUTES.SEARCH_ADVANCED_FILTERS.getRoute()); | ||
| }, [onSaveSelection, selectedItem?.value, backToRoute]); | ||
|
|
||
| const footerContent = useMemo( | ||
| () => ( | ||
| <SearchFilterPageFooterButtons | ||
| applyChanges={applyChanges} | ||
| resetChanges={resetChanges} | ||
| /> | ||
| ), | ||
| [resetChanges, applyChanges], | ||
| }; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)The Suggested fix: const footerContent = useMemo(
() => (
<SearchFilterPageFooterButtons
applyChanges={applyChanges}
resetChanges={resetChanges}
/>
),
[resetChanges, applyChanges],
);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK this component can be compiled by React Compiler so the |
||
| const footerContent = ( | ||
| <SearchFilterPageFooterButtons | ||
| applyChanges={applyChanges} | ||
| resetChanges={resetChanges} | ||
| /> | ||
| ); | ||
|
|
||
| const textInputOptions = { | ||
| value: searchTerm, | ||
| label: translate('common.search'), | ||
| onChangeText: setSearchTerm, | ||
| headerMessage: noResultsFound ? translate('common.noResultsFound') : undefined, | ||
| }; | ||
|
|
||
| return ( | ||
| <SelectionList | ||
| sections={sections} | ||
| initiallyFocusedOptionKey={initiallySelectedItem?.value} | ||
| textInputValue={searchTerm} | ||
| onChangeText={setSearchTerm} | ||
| textInputLabel={shouldShowTextInput ? translate('common.search') : undefined} | ||
| onSelectRow={onSelectItem} | ||
| headerMessage={noResultsFound ? translate('common.noResultsFound') : undefined} | ||
| ListItem={SingleSelectListItem} | ||
| initiallyFocusedItemKey={initiallySelectedItem?.value} | ||
| shouldShowTextInput={shouldShowTextInput} | ||
| textInputOptions={textInputOptions} | ||
| footerContent={shouldAutoSave ? undefined : footerContent} | ||
| shouldStopPropagation | ||
| showLoadingPlaceholder={!noResultsFound} | ||
| shouldShowTooltips | ||
| ListItem={SingleSelectListItem} | ||
| shouldUpdateFocusedIndex | ||
| shouldStopPropagation | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This migration from the old |
||
| /> | ||
| ); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ PERF-6 (docs)
The
sectionscomputation is expensive (filtering, sorting, mapping on every render) and should be memoized withuseMemo. Without memoization, the entireitemsarray is processed on every render, which can cause performance issues with large lists.Suggested fix:
Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK this component can be compiled by React Compiler so the
useMemois not needed