diff --git a/src/pages/workspace/categories/SpendCategorySelectorListItem.tsx b/src/components/SelectionList/ListItem/SpendCategorySelectorListItem.tsx similarity index 83% rename from src/pages/workspace/categories/SpendCategorySelectorListItem.tsx rename to src/components/SelectionList/ListItem/SpendCategorySelectorListItem.tsx index d30be7adfe2b..41c2610d744a 100644 --- a/src/pages/workspace/categories/SpendCategorySelectorListItem.tsx +++ b/src/components/SelectionList/ListItem/SpendCategorySelectorListItem.tsx @@ -1,10 +1,10 @@ import React from 'react'; import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription'; -import BaseListItem from '@components/SelectionListWithSections/BaseListItem'; -import type {BaseListItemProps, ListItem} from '@components/SelectionListWithSections/types'; import useThemeStyles from '@hooks/useThemeStyles'; +import BaseListItem from './BaseListItem'; +import type {ListItem, SpendCategorySelectorListItemProps} from './types'; -function SpendCategorySelectorListItem({item, onSelectRow, isFocused}: BaseListItemProps) { +function SpendCategorySelectorListItem({item, onSelectRow, isFocused}: SpendCategorySelectorListItemProps) { const styles = useThemeStyles(); const {groupID, categoryID} = item; diff --git a/src/components/SelectionList/ListItem/types.ts b/src/components/SelectionList/ListItem/types.ts index 8a7b06e2823d..050ce0b1a0cf 100644 --- a/src/components/SelectionList/ListItem/types.ts +++ b/src/components/SelectionList/ListItem/types.ts @@ -7,7 +7,11 @@ import type {BrickRoad} from '@libs/WorkspacesSettingsUtils'; import type CursorStyles from '@styles/utils/cursor/types'; import type {Errors, Icon, PendingAction} from '@src/types/onyx/OnyxCommon'; import type {ReceiptErrors} from '@src/types/onyx/Transaction'; +import type BaseListItem from './BaseListItem'; +import type MultiSelectListItem from './MultiSelectListItem'; import type RadioListItem from './RadioListItem'; +import type SingleSelectListItem from './SingleSelectListItem'; +import type SpendCategorySelectorListItem from './SpendCategorySelectorListItem'; type ListItem = { /** Text to display */ @@ -230,7 +234,7 @@ type ListItemProps = CommonListItemProps & { shouldUseDefaultRightHandSideCheckmark?: boolean; }; -type ValidListItem = typeof RadioListItem; +type ValidListItem = typeof RadioListItem | typeof BaseListItem | typeof MultiSelectListItem | typeof SingleSelectListItem | typeof SpendCategorySelectorListItem; type BaseListItemProps = CommonListItemProps & { item: TItem; @@ -259,6 +263,8 @@ type SingleSelectListItemProps = ListItemProps; type MultiSelectListItemProps = ListItemProps; +type SpendCategorySelectorListItemProps = ListItemProps; + type UserListItemProps = ListItemProps & ForwardedFSClassProps; export type { @@ -271,5 +277,6 @@ export type { ValidListItem, SingleSelectListItemProps, MultiSelectListItemProps, + SpendCategorySelectorListItemProps, UserListItemProps, }; diff --git a/src/components/SelectionListWithSections/types.ts b/src/components/SelectionListWithSections/types.ts index 3ed19fc3d8be..39e70490e721 100644 --- a/src/components/SelectionListWithSections/types.ts +++ b/src/components/SelectionListWithSections/types.ts @@ -21,7 +21,6 @@ import type {SearchColumnType, SearchGroupBy, SearchQueryJSON} from '@components import type {ForwardedFSClassProps} from '@libs/Fullstory/types'; import type {BrickRoad} from '@libs/WorkspacesSettingsUtils'; import type UnreportedExpenseListItem from '@pages/UnreportedExpenseListItem'; -import type SpendCategorySelectorListItem from '@pages/workspace/categories/SpendCategorySelectorListItem'; // eslint-disable-next-line no-restricted-imports import type CursorStyles from '@styles/utils/cursor/types'; import type {TransactionPreviewData} from '@userActions/Search'; @@ -581,8 +580,7 @@ type ValidListItem = | typeof SearchQueryListItem | typeof SearchRouterItem | typeof TravelDomainListItem - | typeof UnreportedExpenseListItem - | typeof SpendCategorySelectorListItem; + | typeof UnreportedExpenseListItem; type Section = { /** Title of the section */ diff --git a/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx b/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx index f3d008f8ee1b..dd372951c63d 100644 --- a/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx +++ b/src/pages/workspace/categories/WorkspaceCategoriesSettingsPage.tsx @@ -4,8 +4,10 @@ import CategorySelectorModal from '@components/CategorySelector/CategorySelector import HeaderWithBackButton from '@components/HeaderWithBackButton'; import ScreenWrapper from '@components/ScreenWrapper'; import ScrollView from '@components/ScrollView'; -import SelectionList from '@components/SelectionListWithSections'; -import type {ListItem} from '@components/SelectionListWithSections/types'; +import SelectionList from '@components/SelectionList'; +import SpendCategorySelectorListItem from '@components/SelectionList/ListItem/SpendCategorySelectorListItem'; +import type {ListItem} from '@components/SelectionList/ListItem/types'; +import type {ListItem as SelectionListWithSectionsListItem} from '@components/SelectionListWithSections/types'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; @@ -25,7 +27,6 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; -import SpendCategorySelectorListItem from './SpendCategorySelectorListItem'; type WorkspaceCategoriesSettingsPageProps = WithPolicyConnectionsProps & ( @@ -55,38 +56,31 @@ function WorkspaceCategoriesSettingsPage({policy, route}: WorkspaceCategoriesSet setWorkspaceRequiresCategory(policyID, value, policyTagLists, policyCategories, allTransactionViolations); }; - const {sections} = useMemo(() => { + const data = useMemo(() => { if (!(currentPolicy && currentPolicy.mccGroup)) { - return {sections: [{data: []}]}; + return []; } - return { - sections: [ - { - data: Object.entries(currentPolicy.mccGroup).map( - ([mccKey, mccGroup]) => - ({ - categoryID: mccGroup.category, - keyForList: mccKey, - groupID: mccKey, - tabIndex: -1, - pendingAction: mccGroup?.pendingAction, - }) as ListItem, - ), - }, - ], - }; + return Object.entries(currentPolicy.mccGroup).map( + ([mccKey, mccGroup]): ListItem => ({ + categoryID: mccGroup.category, + keyForList: mccKey, + groupID: mccKey, + tabIndex: -1, + pendingAction: mccGroup?.pendingAction, + }), + ); }, [currentPolicy]); const hasEnabledCategories = hasEnabledOptions(policyCategories ?? {}); const isToggleDisabled = !policy?.areCategoriesEnabled || !hasEnabledCategories || isConnectedToAccounting; - const setNewCategory = (selectedCategory: ListItem) => { - if (!selectedCategory.keyForList || !groupID) { + const setNewCategory = (selectedCategory: SelectionListWithSectionsListItem, currentGroupID: string) => { + if (!selectedCategory.keyForList) { return; } if (categoryID !== selectedCategory.keyForList) { - setWorkspaceDefaultSpendCategory(policyID, groupID, selectedCategory.keyForList); + setWorkspaceDefaultSpendCategory(policyID, currentGroupID, selectedCategory.keyForList); } Keyboard.dismiss(); @@ -96,6 +90,23 @@ function WorkspaceCategoriesSettingsPage({policy, route}: WorkspaceCategoriesSet }); }; + const onSelectItem = (item: ListItem) => { + if (!item.groupID || !item.categoryID) { + return; + } + + setIsSelectorModalVisible(true); + setCategoryID(item.categoryID); + setGroupID(item.groupID); + }; + + const selectionListHeaderContent = ( + + {translate('workspace.categories.defaultSpendCategories')} + {translate('workspace.categories.spendCategoriesDescription')} + + ); + return ( - {!!currentPolicy && (sections.at(0)?.data?.length ?? 0) > 0 && ( + {!!currentPolicy && data.length > 0 && ( - {translate('workspace.categories.defaultSpendCategories')} - {translate('workspace.categories.spendCategoriesDescription')} - - } - sections={sections} + customListHeaderContent={selectionListHeaderContent} + data={data} ListItem={SpendCategorySelectorListItem} - onSelectRow={(item) => { - if (!item.groupID || !item.categoryID) { - return; - } - setIsSelectorModalVisible(true); - setCategoryID(item.categoryID); - setGroupID(item.groupID); - }} + onSelectRow={onSelectItem} /> )} @@ -156,7 +155,7 @@ function WorkspaceCategoriesSettingsPage({policy, route}: WorkspaceCategoriesSet isVisible={isSelectorModalVisible} currentCategory={categoryID} onClose={() => setIsSelectorModalVisible(false)} - onCategorySelected={setNewCategory} + onCategorySelected={(selectedCategory) => setNewCategory(selectedCategory, groupID)} label={groupID[0].toUpperCase() + groupID.slice(1)} /> )}