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

20354 category picker list refactor #35567

Merged
Merged
Show file tree
Hide file tree
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
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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comming from #37326. When we switch to SelectList, we haven't passed selectedOptions anymore, it causes a bug in above issue

// 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
Loading