Skip to content

Commit

Permalink
Merge pull request #39702 from Expensify/revert-38994-filip-solecki/r…
Browse files Browse the repository at this point in the history
…emove-options-selector-1

Revert "Replace OptionsSelector with SelectionList - part 1"
  • Loading branch information
danieldoglas authored Apr 5, 2024
2 parents 200de35 + 82b33e2 commit 2995f2c
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 127 deletions.
8 changes: 0 additions & 8 deletions src/components/SelectionList/BaseListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ function BaseListItem<TItem extends ListItem>({
</View>
</View>
)}
{!item.isSelected && item.brickRoadIndicator && (
<View style={[styles.alignItemsCenter, styles.justifyContentCenter]}>
<Icon
src={Expensicons.DotIndicator}
fill={item.brickRoadIndicator === CONST.BRICK_ROAD_INDICATOR_STATUS.INFO ? theme.iconSuccessFill : theme.danger}
/>
</View>
)}
{rightHandSideComponentRender()}
</View>
{FooterComponent}
Expand Down
26 changes: 6 additions & 20 deletions src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ function BaseSelectionList<TItem extends ListItem>(
listHeaderWrapperStyle,
isRowMultilineSupported = false,
textInputRef,
textInputIconLeft,
sectionTitleStyles,
headerMessageStyle,
shouldHideListOnInitialRender = true,
textInputAutoFocus = true,
}: BaseSelectionListProps<TItem>,
ref: ForwardedRef<SelectionListHandle>,
) {
Expand All @@ -82,7 +79,7 @@ function BaseSelectionList<TItem extends ListItem>(
const listRef = useRef<RNSectionList<TItem, SectionWithIndexOffset<TItem>>>(null);
const innerTextInputRef = useRef<RNTextInput | null>(null);
const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const shouldShowTextInput = !!textInputLabel || !!textInputIconLeft;
const shouldShowTextInput = !!textInputLabel;
const shouldShowSelectAll = !!onSelectAll;
const activeElementRole = useActiveElementRole();
const isFocused = useIsFocused();
Expand Down Expand Up @@ -313,7 +310,7 @@ function BaseSelectionList<TItem extends ListItem>(
// We do this so that we can reference the height in `getItemLayout` –
// we need to know the heights of all list items up-front in order to synchronously compute the layout of any given list item.
// So be aware that if you adjust the content of the section header (for example, change the font size), you may need to adjust this explicit height as well.
<View style={[styles.optionsListSectionHeader, styles.justifyContentCenter, sectionTitleStyles]}>
<View style={[styles.optionsListSectionHeader, styles.justifyContentCenter]}>
<Text style={[styles.ph4, styles.textLabelSupporting]}>{section.title}</Text>
</View>
);
Expand Down Expand Up @@ -380,9 +377,6 @@ function BaseSelectionList<TItem extends ListItem>(
/** Focuses the text input when the component comes into focus and after any navigation animations finish. */
useFocusEffect(
useCallback(() => {
if (!textInputAutoFocus) {
return;
}
if (shouldShowTextInput) {
focusTimeoutRef.current = setTimeout(() => {
if (!innerTextInputRef.current) {
Expand All @@ -397,7 +391,7 @@ function BaseSelectionList<TItem extends ListItem>(
}
clearTimeout(focusTimeoutRef.current);
};
}, [shouldShowTextInput, textInputAutoFocus]),
}, [shouldShowTextInput]),
);

const prevTextInputValue = usePrevious(textInputValue);
Expand Down Expand Up @@ -500,12 +494,8 @@ function BaseSelectionList<TItem extends ListItem>(
return;
}

if (typeof textInputRef === 'function') {
textInputRef(element as RNTextInput);
} else {
// eslint-disable-next-line no-param-reassign
textInputRef.current = element as RNTextInput;
}
// eslint-disable-next-line no-param-reassign
textInputRef.current = element as RNTextInput;
}}
label={textInputLabel}
accessibilityLabel={textInputLabel}
Expand All @@ -518,18 +508,14 @@ function BaseSelectionList<TItem extends ListItem>(
inputMode={inputMode}
selectTextOnFocus
spellCheck={false}
iconLeft={textInputIconLeft}
onSubmitEditing={selectFocusedOption}
blurOnSubmit={!!flattenedSections.allOptions.length}
isLoading={isLoadingNewOptions}
testID="selection-list-text-input"
/>
</View>
)}

{/* If we are loading new options we will avoid showing any header message. This is mostly because one of the header messages says there are no options. */}
{/* This is misleading because we might be in the process of loading fresh options from the server. */}
{!isLoadingNewOptions && headerMessage && (
{!!headerMessage && (
<View style={headerMessageStyle ?? [styles.ph5, styles.pb5]}>
<Text style={[styles.textLabel, styles.colorMuted]}>{headerMessage}</Text>
</View>
Expand Down
19 changes: 3 additions & 16 deletions src/components/SelectionList/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type {MutableRefObject, ReactElement, ReactNode} from 'react';
import type {GestureResponderEvent, InputModeOptions, LayoutChangeEvent, SectionListData, StyleProp, TextInput, TextStyle, ViewStyle} from 'react-native';
import type {MaybePhraseKey} from '@libs/Localize';
import type {BrickRoad} from '@libs/WorkspacesSettingsUtils';
import type CONST from '@src/CONST';
import type {Errors, Icon, PendingAction} from '@src/types/onyx/OnyxCommon';
import type {ReceiptErrors} from '@src/types/onyx/Transaction';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import type IconAsset from '@src/types/utils/IconAsset';
import type InviteMemberListItem from './InviteMemberListItem';
import type RadioListItem from './RadioListItem';
import type TableListItem from './TableListItem';
Expand Down Expand Up @@ -112,8 +110,6 @@ type ListItem = {

/** The search value from the selection list */
searchText?: string | null;

brickRoadIndicator?: BrickRoad | '' | null;
};

type ListItemProps = CommonListItemProps<ListItem> & {
Expand Down Expand Up @@ -218,20 +214,14 @@ type BaseSelectionListProps<TItem extends ListItem> = Partial<ChildrenProps> & {
/** Max length for the text input */
textInputMaxLength?: number;

/** Icon to display on the left side of TextInput */
textInputIconLeft?: IconAsset;

/** Whether text input should be focused */
textInputAutoFocus?: boolean;

/** Callback to fire when the text input changes */
onChangeText?: (text: string) => void;

/** Input mode for the text input */
inputMode?: InputModeOptions;

/** Item `keyForList` to focus initially */
initiallyFocusedOptionKey?: string | null;
initiallyFocusedOptionKey?: string;

/** Callback to fire when the list is scrolled */
onScroll?: () => void;
Expand Down Expand Up @@ -282,7 +272,7 @@ type BaseSelectionListProps<TItem extends ListItem> = Partial<ChildrenProps> & {
disableKeyboardShortcuts?: boolean;

/** Styles to apply to SelectionList container */
containerStyle?: StyleProp<ViewStyle>;
containerStyle?: ViewStyle;

/** Whether keyboard is visible on the screen */
isKeyboardShown?: boolean;
Expand All @@ -306,10 +296,7 @@ type BaseSelectionListProps<TItem extends ListItem> = Partial<ChildrenProps> & {
isRowMultilineSupported?: boolean;

/** Ref for textInput */
textInputRef?: MutableRefObject<TextInput | null> | ((ref: TextInput | null) => void);

/** Styles for the section title */
sectionTitleStyles?: StyleProp<ViewStyle>;
textInputRef?: MutableRefObject<TextInput | null>;

/**
* When true, the list won't be visible until the list layout is measured. This prevents the list from "blinking" as it's scrolled to the bottom which is recommended for large lists.
Expand Down
21 changes: 13 additions & 8 deletions src/components/TagPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import React, {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 SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import OptionsSelector from '@components/OptionsSelector';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -101,15 +100,21 @@ function TagPicker({selectedTag, tagListName, policyTags, tagListIndex, policyRe
const selectedOptionKey = sections[0]?.data?.filter((policyTag) => policyTag.searchText === selectedTag)?.[0]?.keyForList;

return (
<SelectionList
ListItem={RadioListItem}
containerStyle={{paddingBottom: StyleUtils.getSafeAreaMargins(insets).marginBottom}}
sectionTitleStyles={styles.mt5}
<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}
textInputValue={searchValue}
selectedOptions={selectedOptions}
headerMessage={headerMessage}
textInputLabel={shouldShowTextInput ? translate('common.search') : undefined}
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}
Expand Down
3 changes: 0 additions & 3 deletions src/components/WorkspaceSwitcherButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type WorkspaceSwitcherButtonProps = WorkspaceSwitcherButtonOnyxProps;
function WorkspaceSwitcherButton({policy}: WorkspaceSwitcherButtonProps) {
const {translate} = useLocalize();
const theme = useTheme();
const pressableRef = useRef<View>();

const pressableRef = useRef<HTMLDivElement | View | null>(null);

Expand All @@ -45,13 +44,11 @@ function WorkspaceSwitcherButton({policy}: WorkspaceSwitcherButtonProps) {
<PressableWithFeedback
ref={pressableRef}
accessibilityRole={CONST.ROLE.BUTTON}
ref={pressableRef}
accessibilityLabel={translate('common.workspaces')}
accessible
onPress={() => {
pressableRef?.current?.blur();
interceptAnonymousUser(() => {
pressableRef.current?.blur();
Navigation.navigate(ROUTES.WORKSPACE_SWITCHER);
});
}}
Expand Down
16 changes: 7 additions & 9 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,18 +1105,16 @@ function getCategoryListSections(
*
* @param tags - an initial tag array
*/
function getTagsOptions(tags: Array<Pick<PolicyTag, 'name' | 'enabled'>>, selectedOptions?: SelectedTagOption[]): Option[] {
function getTagsOptions(tags: Array<Pick<PolicyTag, 'name' | 'enabled'>>): Option[] {
return tags.map((tag) => {
// This is to remove unnecessary escaping backslash in tag name sent from backend.
const cleanedName = PolicyUtils.getCleanedTagName(tag.name);

return {
text: cleanedName,
keyForList: tag.name,
searchText: tag.name,
tooltipText: cleanedName,
isDisabled: !tag.enabled,
isSelected: selectedOptions?.some((selectedTag) => selectedTag.name === tag.name),
};
});
}
Expand Down Expand Up @@ -1148,7 +1146,7 @@ function getTagListSections(
// "Selected" section
title: '',
shouldShow: false,
data: getTagsOptions(selectedTagOptions, selectedOptions),
data: getTagsOptions(selectedTagOptions),
});

return tagSections;
Expand All @@ -1161,7 +1159,7 @@ function getTagListSections(
// "Search" section
title: '',
shouldShow: true,
data: getTagsOptions(searchTags, selectedOptions),
data: getTagsOptions(searchTags),
});

return tagSections;
Expand All @@ -1172,7 +1170,7 @@ function getTagListSections(
// "All" section when items amount less than the threshold
title: '',
shouldShow: false,
data: getTagsOptions(enabledTags, selectedOptions),
data: getTagsOptions(enabledTags),
});

return tagSections;
Expand All @@ -1197,7 +1195,7 @@ function getTagListSections(
// "Selected" section
title: '',
shouldShow: true,
data: getTagsOptions(selectedTagOptions, selectedOptions),
data: getTagsOptions(selectedTagOptions),
});
}

Expand All @@ -1208,15 +1206,15 @@ function getTagListSections(
// "Recent" section
title: Localize.translateLocal('common.recent'),
shouldShow: true,
data: getTagsOptions(cutRecentlyUsedTags, selectedOptions),
data: getTagsOptions(cutRecentlyUsedTags),
});
}

tagSections.push({
// "All" section when items amount more than the threshold
title: Localize.translateLocal('common.all'),
shouldShow: true,
data: getTagsOptions(filteredTags, selectedOptions),
data: getTagsOptions(filteredTags),
});

return tagSections;
Expand Down
29 changes: 17 additions & 12 deletions src/pages/EditReportFieldDropdownPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React, {useMemo, useState} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import OptionsSelector from '@components/OptionsSelector';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -43,7 +42,6 @@ type ReportFieldDropdownData = {
keyForList: string;
searchText: string;
tooltipText: string;
isSelected?: boolean;
};

type ReportFieldDropdownSectionItem = {
Expand Down Expand Up @@ -73,7 +71,6 @@ function EditReportFieldDropdownPage({fieldName, onSubmit, fieldKey, fieldValue,
keyForList: option,
searchText: option,
tooltipText: option,
isSelected: option === fieldValue,
})),
});
} else {
Expand All @@ -87,7 +84,6 @@ function EditReportFieldDropdownPage({fieldName, onSubmit, fieldKey, fieldValue,
keyForList: selectedValue,
searchText: selectedValue,
tooltipText: selectedValue,
isSelected: true,
},
],
});
Expand Down Expand Up @@ -134,18 +130,27 @@ function EditReportFieldDropdownPage({fieldName, onSubmit, fieldKey, fieldValue,
{({insets}) => (
<>
<HeaderWithBackButton title={fieldName} />
<SelectionList
ListItem={RadioListItem}
containerStyle={{paddingBottom: getSafeAreaMargins(insets).marginBottom}}
<OptionsSelector
// @ts-expect-error TODO: TS migration
contentContainerStyles={[{paddingBottom: getSafeAreaMargins(insets).marginBottom}]}
optionHoveredStyle={styles.hoveredComponentBG}
sectionHeaderStyle={styles.mt5}
selectedOptions={[{name: fieldValue}]}
textInputLabel={translate('common.search')}
boldStyle
sections={sections}
sectionTitleStyles={styles.mt5}
textInputValue={searchValue}
onSelectRow={(option) => onSubmit({[fieldKey]: fieldValue === option.text ? '' : option.text})}
// Focus the first option when searching
focusedIndex={0}
value={searchValue}
onSelectRow={(option: Record<string, string>) =>
onSubmit({
[fieldKey]: fieldValue === option.text ? '' : option.text,
})
}
onChangeText={setSearchValue}
highlightSelectedOptions
isRowMultilineSupported
headerMessage={headerMessage}
initiallyFocusedOptionKey={fieldValue}
/>
</>
)}
Expand Down
Loading

0 comments on commit 2995f2c

Please sign in to comment.