Skip to content
Closed
15 changes: 14 additions & 1 deletion src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,19 @@ function BaseSelectionList<TItem extends ListItem>({
[data, itemsToHighlight, scrollToIndex],
);

const updateFocusedIndex = useCallback(
(newFocusedIndex: number, shouldScroll = false) => {
if (newFocusedIndex < 0 || newFocusedIndex >= data.length) {
return;
}
setFocusedIndex(newFocusedIndex);
if (shouldScroll) {
scrollToIndex(newFocusedIndex);
}
},
[data.length, scrollToIndex, setFocusedIndex],
);

useEffect(() => {
if (!itemFocusTimeoutRef.current) {
return;
Expand All @@ -371,7 +384,7 @@ function BaseSelectionList<TItem extends ListItem>({
}
}, [onSelectAll, shouldShowTextInput, shouldPreventDefaultFocusOnSelectRow]);

useImperativeHandle(ref, () => ({scrollAndHighlightItem, scrollToIndex}), [scrollAndHighlightItem, scrollToIndex]);
useImperativeHandle(ref, () => ({scrollAndHighlightItem, scrollToIndex, updateFocusedIndex}), [scrollAndHighlightItem, scrollToIndex, updateFocusedIndex]);
return (
<View style={[styles.flex1, !addBottomSafeAreaPadding && paddingBottomStyle, style?.containerStyle]}>
{textInputComponent({shouldBeInsideList: false})}
Expand Down
3 changes: 3 additions & 0 deletions src/components/SelectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ type SelectionListHandle = {

/** Scrolls to the item at the specified index */
scrollToIndex: (index: number) => void;

/** Updates the focused index and scrolls to it */
updateFocusedIndex: (newFocusedIndex: number, shouldScroll?: boolean) => void;
};

type DataDetailsType<TItem extends ListItem> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {useRoute} from '@react-navigation/native';
import React, {useCallback, useMemo} from 'react';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import {View} from 'react-native';
import type {ValueOf} from 'type-fest';
import ConnectionLayout from '@components/ConnectionLayout';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import SelectionList from '@components/SelectionListWithSections';
import RadioListItem from '@components/SelectionListWithSections/RadioListItem';
import type {ListItem} from '@components/SelectionListWithSections/types';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/ListItem/RadioListItem';
import type {ListItem, SelectionListHandle} from '@components/SelectionList/types';
import type {SelectorType} from '@components/SelectionScreen';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
Expand All @@ -34,13 +34,14 @@ function NetSuiteInvoiceItemPreferenceSelectPage({policy}: WithPolicyConnections
const policyID = policy?.id;
const config = policy?.connections?.netsuite.options.config;
const route = useRoute<PlatformStackRouteProp<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.ACCOUNTING.NETSUITE_INVOICE_ITEM_PREFERENCE_SELECT>>();
const selectionListRef = useRef<SelectionListHandle>(null);

const {items} = policy?.connections?.netsuite?.options.data ?? {};
const selectedItem = useMemo(() => findSelectedInvoiceItemWithDefaultSelect(items, config?.invoiceItem), [items, config?.invoiceItem]);

const selectedValue = Object.values(CONST.NETSUITE_INVOICE_ITEM_PREFERENCE).find((value) => value === config?.invoiceItemPreference) ?? CONST.NETSUITE_INVOICE_ITEM_PREFERENCE.CREATE;

const data: MenuListItem[] = Object.values(CONST.NETSUITE_INVOICE_ITEM_PREFERENCE).map((postingPreference) => ({
const options: MenuListItem[] = Object.values(CONST.NETSUITE_INVOICE_ITEM_PREFERENCE).map((postingPreference) => ({
value: postingPreference,
text: translate(`workspace.netsuite.invoiceItem.values.${postingPreference}.label`),
keyForList: postingPreference,
Expand All @@ -63,6 +64,14 @@ function NetSuiteInvoiceItemPreferenceSelectPage({policy}: WithPolicyConnections
[config?.invoiceItemPreference, policyID, goBack],
);

// Update focused index when selectedValue changes (after an error reverts the selection)
useEffect(() => {
const selectedIndex = options.findIndex((option) => option.isSelected);
if (selectedIndex !== -1 && selectionListRef.current) {
selectionListRef.current?.updateFocusedIndex(selectedIndex);
}
}, [selectedValue, options]);

return (
<ConnectionLayout
headerTitle="workspace.netsuite.invoiceItem.label"
Expand All @@ -81,17 +90,20 @@ function NetSuiteInvoiceItemPreferenceSelectPage({policy}: WithPolicyConnections
errors={getLatestErrorField(config, CONST.NETSUITE_CONFIG.INVOICE_ITEM_PREFERENCE)}
errorRowStyles={[styles.ph5, styles.pv3]}
onClose={() => clearNetSuiteErrorField(policyID, CONST.NETSUITE_CONFIG.INVOICE_ITEM_PREFERENCE)}
style={[styles.flexGrow1, styles.flexShrink1]}
contentContainerStyle={[styles.flexGrow1, styles.flexShrink1]}
style={[styles.flexGrow1, styles.flexShrink1, styles.minHeight32]}
contentContainerStyle={[styles.flexGrow1, styles.flexShrink1, styles.minHeight32]}
>
<SelectionList
onSelectRow={(selection: SelectorType) => selectInvoicePreference(selection as MenuListItem)}
sections={[{data}]}
ref={selectionListRef}
data={options}
onSelectRow={(selection: SelectorType) => {
selectInvoicePreference(selection as MenuListItem);
}}
ListItem={RadioListItem}
showScrollIndicator
shouldUpdateFocusedIndex
initiallyFocusedOptionKey={data.find((mode) => mode.isSelected)?.keyForList}
containerStyle={[styles.flexReset, styles.flexGrow1, styles.flexShrink1, styles.pb0]}
initiallyFocusedItemKey={options.find((mode) => mode.isSelected)?.keyForList}
style={{containerStyle: [styles.pb0]}}
/>
</OfflineWithFeedback>
{config?.invoiceItemPreference === CONST.NETSUITE_INVOICE_ITEM_PREFERENCE.SELECT && (
Expand Down
4 changes: 4 additions & 0 deletions src/styles/utils/spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,10 @@ export default {
minHeight: 88,
},

minHeight32: {
minHeight: 128,
},

minHeight65: {
minHeight: 260,
},
Expand Down