diff --git a/src/components/SelectionList/BaseSelectionList.tsx b/src/components/SelectionList/BaseSelectionList.tsx index 73dace47b161..8bd4f66c197e 100644 --- a/src/components/SelectionList/BaseSelectionList.tsx +++ b/src/components/SelectionList/BaseSelectionList.tsx @@ -357,6 +357,19 @@ function BaseSelectionList({ [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; @@ -371,7 +384,7 @@ function BaseSelectionList({ } }, [onSelectAll, shouldShowTextInput, shouldPreventDefaultFocusOnSelectRow]); - useImperativeHandle(ref, () => ({scrollAndHighlightItem, scrollToIndex}), [scrollAndHighlightItem, scrollToIndex]); + useImperativeHandle(ref, () => ({scrollAndHighlightItem, scrollToIndex, updateFocusedIndex}), [scrollAndHighlightItem, scrollToIndex, updateFocusedIndex]); return ( {textInputComponent({shouldBeInsideList: false})} diff --git a/src/components/SelectionList/types.ts b/src/components/SelectionList/types.ts index 170faa0fc1d4..e7d5d2e699e2 100644 --- a/src/components/SelectionList/types.ts +++ b/src/components/SelectionList/types.ts @@ -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 = { diff --git a/src/pages/workspace/accounting/netsuite/export/NetSuiteInvoiceItemPreferenceSelectPage.tsx b/src/pages/workspace/accounting/netsuite/export/NetSuiteInvoiceItemPreferenceSelectPage.tsx index e711e4450445..ab4d6c9951ee 100644 --- a/src/pages/workspace/accounting/netsuite/export/NetSuiteInvoiceItemPreferenceSelectPage.tsx +++ b/src/pages/workspace/accounting/netsuite/export/NetSuiteInvoiceItemPreferenceSelectPage.tsx @@ -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'; @@ -34,13 +34,14 @@ function NetSuiteInvoiceItemPreferenceSelectPage({policy}: WithPolicyConnections const policyID = policy?.id; const config = policy?.connections?.netsuite.options.config; const route = useRoute>(); + const selectionListRef = useRef(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, @@ -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 ( 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]} > 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]}} /> {config?.invoiceItemPreference === CONST.NETSUITE_INVOICE_ITEM_PREFERENCE.SELECT && ( diff --git a/src/styles/utils/spacing.ts b/src/styles/utils/spacing.ts index b6aa84e49c7c..e0c5ec07c68f 100644 --- a/src/styles/utils/spacing.ts +++ b/src/styles/utils/spacing.ts @@ -835,6 +835,10 @@ export default { minHeight: 88, }, + minHeight32: { + minHeight: 128, + }, + minHeight65: { minHeight: 260, },