-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Recreate: Migrate InitialListValueSelectorModal to dynamic @react-navigation route (with regression fixes) #94014
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
base: main
Are you sure you want to change the base?
Changes from all commits
84ecb57
6ec9bab
8ac54ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import React, {useEffect} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
| import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
| import ScreenWrapper from '@components/ScreenWrapper'; | ||
| import Text from '@components/Text'; | ||
| import useDynamicBackPath from '@hooks/useDynamicBackPath'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import {setDraftValues} from '@libs/actions/FormActions'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types'; | ||
| import {hasAccountingConnections} from '@libs/PolicyUtils'; | ||
| import type {SettingsNavigatorParamList} from '@navigation/types'; | ||
| import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper'; | ||
| import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading'; | ||
| import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import {DYNAMIC_ROUTES} from '@src/ROUTES'; | ||
| import type SCREENS from '@src/SCREENS'; | ||
| import INPUT_IDS from '@src/types/form/WorkspaceReportFieldForm'; | ||
| import ReportFieldsInitialListValuePicker from './ReportFieldsInitialListValuePicker'; | ||
|
|
||
| type DynamicReportFieldsInitialListValuePageProps = WithPolicyAndFullscreenLoadingProps & | ||
| PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.DYNAMIC_REPORT_FIELDS_INITIAL_LIST_VALUE>; | ||
|
|
||
| function DynamicReportFieldsInitialListValuePage({ | ||
| policy, | ||
| route: { | ||
| params: {policyID}, | ||
| }, | ||
| }: DynamicReportFieldsInitialListValuePageProps) { | ||
| const styles = useThemeStyles(); | ||
| const {translate} = useLocalize(); | ||
| const [formDraft, formDraftMetadata] = useOnyx(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT); | ||
| const backPath = useDynamicBackPath(DYNAMIC_ROUTES.WORKSPACE_REPORT_FIELDS_INITIAL_LIST_VALUE.path); | ||
|
|
||
| const currentValue = formDraft?.[INPUT_IDS.INITIAL_VALUE] ?? ''; | ||
| const isLoadingFormDraft = formDraftMetadata.status !== 'loaded'; | ||
| // Mirror the guard used on the create page: the initial value selector is only relevant when there are | ||
| // available (non-disabled) list values. On a refresh or deeplink the form draft can be empty, in which case | ||
| // we send the user back to the Add field page instead of showing an empty picker. | ||
| const availableListValuesLength = (formDraft?.[INPUT_IDS.DISABLED_LIST_VALUES] ?? []).filter((disabledListValue) => !disabledListValue).length; | ||
| const shouldRedirectToCreatePage = !isLoadingFormDraft && availableListValuesLength === 0; | ||
|
|
||
| useEffect(() => { | ||
| if (!shouldRedirectToCreatePage) { | ||
| return; | ||
| } | ||
| Navigation.goBack(backPath); | ||
| }, [shouldRedirectToCreatePage, backPath]); | ||
|
|
||
| const onValueSelected = (value: string) => { | ||
| setDraftValues(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM, { | ||
| [INPUT_IDS.INITIAL_VALUE]: currentValue === value ? '' : value, | ||
| }); | ||
| Navigation.goBack(backPath); | ||
| }; | ||
|
|
||
| return ( | ||
| <AccessOrNotFoundWrapper | ||
| accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]} | ||
| policyID={policyID} | ||
| featureName={CONST.POLICY.MORE_FEATURES.ARE_REPORT_FIELDS_ENABLED} | ||
| shouldBeBlocked={hasAccountingConnections(policy)} | ||
| > | ||
| <ScreenWrapper | ||
| style={styles.pb0} | ||
| enableEdgeToEdgeBottomSafeAreaPadding | ||
| testID="DynamicReportFieldsInitialListValuePage" | ||
| > | ||
| <HeaderWithBackButton | ||
| title={translate('common.initialValue')} | ||
| onBackButtonPress={() => Navigation.goBack(backPath)} | ||
| /> | ||
| {isLoadingFormDraft || shouldRedirectToCreatePage ? ( | ||
| <FullScreenLoadingIndicator reasonAttributes={{context: 'DynamicReportFieldsInitialListValuePage', isLoadingFormDraft: !!isLoadingFormDraft}} /> | ||
| ) : ( | ||
| <> | ||
| <View style={[styles.ph5, styles.pb4]}> | ||
| <Text style={[styles.sidebarLinkText, styles.optionAlternateText]}>{translate('workspace.reportFields.listValuesInputSubtitle')}</Text> | ||
| </View> | ||
| <ReportFieldsInitialListValuePicker | ||
| listValues={formDraft?.[INPUT_IDS.LIST_VALUES] ?? []} | ||
| disabledOptions={formDraft?.[INPUT_IDS.DISABLED_LIST_VALUES] ?? []} | ||
| value={currentValue} | ||
| onValueChange={onValueSelected} | ||
| /> | ||
| </> | ||
| )} | ||
| </ScreenWrapper> | ||
| </AccessOrNotFoundWrapper> | ||
| ); | ||
| } | ||
|
|
||
| export default withPolicyAndFullscreenLoading(DynamicReportFieldsInitialListValuePage); | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,29 @@ | ||
| import type {ForwardedRef} from 'react'; | ||
| import React, {useEffect, useState} from 'react'; | ||
| import React, {useEffect, useRef} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import type {MenuItemBaseProps} from '@components/MenuItem'; | ||
| import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import blurActiveElement from '@libs/Accessibility/blurActiveElement'; | ||
| import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/createDynamicRoute'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import InitialListValueSelectorModal from './InitialListValueSelectorModal'; | ||
| import {DYNAMIC_ROUTES} from '@src/ROUTES'; | ||
|
|
||
| type InitialListValueSelectorProps = Pick<MenuItemBaseProps, 'label' | 'rightLabel' | 'errorText'> & { | ||
| /** Currently selected value */ | ||
| value?: string; | ||
|
|
||
| /** Subtitle to display on field */ | ||
| subtitle?: string; | ||
|
|
||
| /** Function to call when the user selects a value */ | ||
| onInputChange?: (value: string) => void; | ||
|
|
||
| /** Reference to the outer element */ | ||
| ref: ForwardedRef<View>; | ||
| }; | ||
|
|
||
| function InitialListValueSelector({value = '', label = '', rightLabel, subtitle = '', errorText = '', onInputChange, ref}: InitialListValueSelectorProps) { | ||
| function InitialListValueSelector({value = '', label = '', rightLabel, errorText = '', onInputChange, ref}: InitialListValueSelectorProps) { | ||
| const [formDraft] = useOnyx(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT); | ||
|
|
||
| const [isPickerVisible, setIsPickerVisible] = useState(false); | ||
|
|
||
| const showPickerModal = () => { | ||
| setIsPickerVisible(true); | ||
| }; | ||
|
|
||
| const hidePickerModal = () => { | ||
| setIsPickerVisible(false); | ||
| blurActiveElement(); | ||
| }; | ||
|
|
||
| const updateValueInput = (initialValue: string) => { | ||
| onInputChange?.(value === initialValue ? '' : initialValue); | ||
| hidePickerModal(); | ||
| }; | ||
| const draftInitialValue = formDraft?.initialValue ?? ''; | ||
|
|
||
| useEffect(() => { | ||
| const currentValueIndex = Object.values(formDraft?.listValues ?? {}).findIndex((listValue) => listValue === value); | ||
|
|
@@ -51,6 +34,20 @@ function InitialListValueSelector({value = '', label = '', rightLabel, subtitle | |
| } | ||
| }, [formDraft?.disabledListValues, formDraft?.listValues, onInputChange, value]); | ||
|
|
||
| // The value is selected on a separate dynamic route which writes directly to the form draft. FormProvider | ||
| // already syncs that draft write into the form's value, but it never re-runs validation, so the "Initial value" | ||
| // required error lingers. We can't detect the change by comparing the draft against `value` (they're already in | ||
| // sync by the time this runs), so we track the previous draft value and push it through onInputChange whenever it | ||
| // changes. onInputChange triggers the form's onValidate, which clears the stale error. | ||
| const previousDraftInitialValue = useRef(draftInitialValue); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-10 (docs)This Prefer having the parent (the create page / Reviewed at: 8ac54ef | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| useEffect(() => { | ||
| if (draftInitialValue === previousDraftInitialValue.current) { | ||
| return; | ||
| } | ||
| previousDraftInitialValue.current = draftInitialValue; | ||
| onInputChange?.(draftInitialValue); | ||
| }, [draftInitialValue, onInputChange]); | ||
|
|
||
| return ( | ||
| <View> | ||
| <MenuItemWithTopDescription | ||
|
|
@@ -61,15 +58,7 @@ function InitialListValueSelector({value = '', label = '', rightLabel, subtitle | |
| rightLabel={rightLabel} | ||
| brickRoadIndicator={errorText ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined} | ||
| errorText={errorText} | ||
| onPress={showPickerModal} | ||
| /> | ||
| <InitialListValueSelectorModal | ||
| isVisible={isPickerVisible} | ||
| currentValue={value} | ||
| onClose={hidePickerModal} | ||
| onValueSelected={updateValueInput} | ||
| label={label} | ||
| subtitle={subtitle} | ||
| onPress={() => Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.WORKSPACE_REPORT_FIELDS_INITIAL_LIST_VALUE.path))} | ||
| /> | ||
| </View> | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ UI-1 (docs)
FullScreenLoadingIndicatoris rendered inside the same<ScreenWrapper>tree as the<HeaderWithBackButton>(lines 74-77). Because navigation (the back button) is visible alongside the loader, the user already has an escape route if loading hangs — so a full-screen loader is the wrong indicator here. This matches the UI-1 "Incorrect" example ofFullscreenLoadingIndicatorandHeaderWithBackButtonliving under the same JSX tree.Use an
ActivityIndicatorcentered in the content area instead, keeping the header rendered during loading:Reviewed at: 8ac54ef | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.