Skip to content

Commit

Permalink
Merge pull request #35567 from lukemorawski/20354-category_picker_lis…
Browse files Browse the repository at this point in the history
…t_refactor

20354 category picker list refactor
  • Loading branch information
cristipaval authored Feb 20, 2024
2 parents 4dc82e5 + 1fd520f commit a9d1683
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
50 changes: 22 additions & 28 deletions src/components/CategoryPicker/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import lodashGet from 'lodash/get';
import React, {useMemo, useState} from 'react';
import React, {useMemo} from 'react';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import OptionsSelector from '@components/OptionsSelector';
import SelectionList from '@components/SelectionList';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {defaultProps, propTypes} from './categoryPickerPropTypes';

function CategoryPicker({selectedCategory, policyCategories, policyRecentlyUsedCategories, onSubmit}) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [searchValue, setSearchValue] = useState('');

const policyCategoriesCount = OptionsListUtils.getEnabledCategoriesCount(_.values(policyCategories));
const isCategoriesCountBelowThreshold = policyCategoriesCount < CONST.CATEGORY_LIST_THRESHOLD;
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');

const selectedOptions = useMemo(() => {
if (!selectedCategory) {
Expand All @@ -28,17 +24,18 @@ function CategoryPicker({selectedCategory, policyCategories, policyRecentlyUsedC
name: selectedCategory,
enabled: true,
accountID: null,
isSelected: true,
},
];
}, [selectedCategory]);

const sections = useMemo(() => {
const [sections, headerMessage, policyCategoriesCount, shouldShowTextInput] = useMemo(() => {
const validPolicyRecentlyUsedCategories = _.filter(policyRecentlyUsedCategories, (p) => !_.isEmpty(p));
const {categoryOptions} = OptionsListUtils.getFilteredOptions(
{},
{},
[],
searchValue,
debouncedSearchValue,
selectedOptions,
[],
false,
Expand All @@ -49,31 +46,28 @@ function CategoryPicker({selectedCategory, policyCategories, policyRecentlyUsedC
false,
);

return categoryOptions;
}, [policyCategories, policyRecentlyUsedCategories, searchValue, selectedOptions]);
const header = OptionsListUtils.getHeaderMessageForNonUserList(lodashGet(categoryOptions, '[0].data', []).length > 0, debouncedSearchValue);
const policiesCount = OptionsListUtils.getEnabledCategoriesCount(_.values(policyCategories));
const isCategoriesCountBelowThreshold = policyCategoriesCount < CONST.CATEGORY_LIST_THRESHOLD;
const showInput = !isCategoriesCountBelowThreshold;

return [categoryOptions, header, policiesCount, showInput];
}, [policyCategories, policyRecentlyUsedCategories, debouncedSearchValue, selectedOptions]);

const headerMessage = OptionsListUtils.getHeaderMessageForNonUserList(lodashGet(sections, '[0].data.length', 0) > 0, searchValue);
const shouldShowTextInput = !isCategoriesCountBelowThreshold;
const selectedOptionKey = lodashGet(_.filter(lodashGet(sections, '[0].data', []), (category) => category.searchText === selectedCategory)[0], 'keyForList');
const selectedOptionKey = useMemo(
() => lodashGet(_.filter(lodashGet(sections, '[0].data', []), (category) => category.searchText === selectedCategory)[0], 'keyForList'),
[sections, selectedCategory],
);

return (
<OptionsSelector
optionHoveredStyle={styles.hoveredComponentBG}
sectionHeaderStyle={styles.mt5}
<SelectionList
sections={sections}
selectedOptions={selectedOptions}
// Focus the first option when searching
focusedIndex={0}
// Focus the selected option on first load
initiallyFocusedOptionKey={selectedOptionKey}
headerMessage={headerMessage}
shouldShowTextInput={shouldShowTextInput}
textInputLabel={translate('common.search')}
boldStyle
highlightSelectedOptions
isRowMultilineSupported
textInputValue={searchValue}
textInputLabel={shouldShowTextInput && translate('common.search')}
onChangeText={setSearchValue}
onSelectRow={onSubmit}
initiallyFocusedOptionKey={selectedOptionKey}
/>
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type CategorySection = {
type Category = {
name: string;
enabled: boolean;
isSelected?: boolean;
};

type Hierarchy = Record<string, Category & {[key: string]: Hierarchy & Category}>;
Expand Down Expand Up @@ -900,6 +901,7 @@ function getCategoryOptionTree(options: Record<string, Category> | Category[], i
searchText: option.name,
tooltipText: option.name,
isDisabled: !option.enabled,
isSelected: !!option.isSelected,
});

return;
Expand All @@ -920,6 +922,7 @@ function getCategoryOptionTree(options: Record<string, Category> | Category[], i
searchText,
tooltipText: optionName,
isDisabled: isChild ? !option.enabled : true,
isSelected: !!option.isSelected,
});
});
});
Expand Down
Loading

0 comments on commit a9d1683

Please sign in to comment.