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

[TS migration] Migrate 'TagPicker' component to TypeScript #34561

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 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
94 changes: 0 additions & 94 deletions src/components/TagPicker/index.js
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

132 changes: 132 additions & 0 deletions src/components/TagPicker/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import {useMemo, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import type {EdgeInsets} from 'react-native-safe-area-context';
import OptionsSelector from '@components/OptionsSelector';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PolicyTags, RecentlyUsedTags} from '@src/types/onyx';

type TagPickerOnyxProps = {
/** Collection of tags attached to a policy */
policyTags: OnyxEntry<PolicyTags>;

/** List of recently used tags */
policyRecentlyUsedTags: OnyxEntry<RecentlyUsedTags>;
};

type TagPickerProps = TagPickerOnyxProps & {
/** The policyID we are getting tags for */
// It's used in withOnyx HOC.
// eslint-disable-next-line react/no-unused-prop-types
policyID: string;

/** The selected tag of the money request */
selectedTag: string;

/** The name of tag list we are getting tags for */
tag: string;

/** Callback to submit the selected tag */
onSubmit: () => void;

/**
* Safe area insets required for reflecting the portion of the view,
* that is not covered by navigation bars, tab bars, toolbars, and other ancestor views.
*/
insets: EdgeInsets;

/** Should show the selected option that is disabled? */
shouldShowDisabledAndSelectedOption?: boolean;
};

function TagPicker({selectedTag, tag, policyTags, policyRecentlyUsedTags, shouldShowDisabledAndSelectedOption = false, insets, onSubmit}: TagPickerProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const [searchValue, setSearchValue] = useState('');

const policyTagList = PolicyUtils.getTagList(policyTags, tag);
const policyTagsCount = Object.values(policyTagList).filter((policyTag) => policyTag.enabled).length;
const isTagsCountBelowThreshold = policyTagsCount < CONST.TAG_LIST_THRESHOLD;

const shouldShowTextInput = !isTagsCountBelowThreshold;

const selectedOptions = useMemo(() => {
if (!selectedTag) {
return [];
}

return [
{
name: selectedTag,
enabled: true,
accountID: null,
},
];
}, [selectedTag]);

const enabledTags = useMemo(() => {
if (!shouldShowDisabledAndSelectedOption) {
return policyTagList;
}
const selectedNames = selectedOptions.map((s) => s.name);

return [...selectedOptions, ...Object.values(policyTagList).filter((policyTag) => policyTag.enabled && !selectedNames.includes(policyTag.name))];
}, [selectedOptions, policyTagList, shouldShowDisabledAndSelectedOption]);

//
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
const sections = useMemo(() => {
const policyRecentlyUsedTagsList = policyRecentlyUsedTags?.tag ?? [];

// TODO: Remove this and one line under once OptionsListUtils (https://github.com/Expensify/App/issues/24921) is migrated to TypeScript.
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return (
// @ts-expect-error TODO: Remove this once OptionsListUtils (https://github.com/Expensify/App/issues/24921) is migrated to TypeScript.
OptionsListUtils.getFilteredOptions({}, {}, [], searchValue, selectedOptions, [], false, false, false, {}, [], true, enabledTags, policyRecentlyUsedTagsList, false).tagOptions
);
}, [searchValue, enabledTags, selectedOptions, policyRecentlyUsedTags?.tag]);

const headerMessage = OptionsListUtils.getHeaderMessageForNonUserList((sections?.[0]?.data?.length ?? 0) > 0, searchValue);

const selectedOptionKey = sections[0]?.data?.filter((policyTag) => policyTag.searchText === selectedTag)?.[0]?.keyForList;

return (
<OptionsSelector
// @ts-expect-error TODO: Remove this once OptionsSelector (https://github.com/Expensify/App/issues/25125) is migrated to TypeScript.
contentContainerStyles={[{paddingBottom: StyleUtils.getSafeAreaMargins(insets).marginBottom}]}
optionHoveredStyle={styles.hoveredComponentBG}
sectionHeaderStyle={styles.mt5}
sections={sections}
selectedOptions={selectedOptions}
headerMessage={headerMessage}
textInputLabel={translate('common.search')}
boldStyle
highlightSelectedOptions
isRowMultilineSupported
shouldShowTextInput={shouldShowTextInput}
// Focus the first option when searching
focusedIndex={0}
// Focus the selected option on first load
initiallyFocusedOptionKey={selectedOptionKey}
onChangeText={setSearchValue}
onSelectRow={onSubmit}
/>
);
}

TagPicker.displayName = 'TagPicker';

export default withOnyx<TagPickerProps, TagPickerOnyxProps>({
policyTags: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`,
},
policyRecentlyUsedTags: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}`,
},
})(TagPicker);
41 changes: 0 additions & 41 deletions src/components/TagPicker/tagPickerPropTypes.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function getTagListName(policyTags: OnyxEntry<PolicyTags>) {
/**
* Gets the tags of a policy for a specific key. Defaults to the first tag if no key is provided.
*/
function getTagList(policyTags: OnyxCollection<PolicyTags>, tagKey: string) {
function getTagList(policyTags: OnyxEntry<PolicyTags>, tagKey: string) {
if (Object.keys(policyTags ?? {})?.length === 0) {
return {};
}
Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/PolicyTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type PolicyTag = {
/** "General Ledger code" that corresponds to this tag in an accounting system. Similar to an ID. */
// eslint-disable-next-line @typescript-eslint/naming-convention
'GL Code': string;

/** Tags included in the policy */
tags: PolicyTags;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
};

type PolicyTags = Record<string, PolicyTag>;
Expand Down
Loading