Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions contributingGuides/FORMS.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,33 @@ import KEYBOARD_SUBMIT_BEHAVIOR from './keyboardSubmitBehavior';
> [!NOTE]
> Only override `keyboardSubmitBehavior` on screens where `onSubmit` triggers navigation. For forms that stay on-screen after submission, keep the default (`'dismiss-then-submit'`) to avoid layout jumps.

### Reacting to form value changes (`FormValueWatcher`)

Sometimes a parent screen needs to react when a form value changes from outside the form — for example when an RHP picker writes to the Onyx draft via `setDraftValues` and the parent has to re-sync `FormProvider`'s internal state (clear `touched` flags, wipe local errors via `resetForm`, etc.).

Instead of adding a `useEffect` inside the parent (which can't see `FormProvider`'s `inputValues` directly), use the `FormValueWatcher` primitive together with `FormProvider`'s children render prop. It's a render-null component that fires `onValuesChange(current, previous)` whenever the `values` reference changes, and short-circuits when the reference is identical (no spurious fires).

```jsx
<FormProvider formID={ONYXKEYS.FORMS.MY_FORM} ref={formRef} /* ... */>
{({inputValues}) => (
<>
<FormValueWatcher
values={inputValues}
onValuesChange={(current, previous) => {
if (current.type === previous.type) {
return;
}
formRef.current?.resetForm(current);
}}
/>
{/* ...InputWrappers... */}
</>
)}
</FormProvider>
```

Use it only when you genuinely need a cross-component reaction; for in-form logic, prefer `onValueChange` / `onInputChange` on the specific input.

### Safe Area Padding

Any `FormProvider.tsx` that has a button at the bottom. If the `<FormProvider>` is inside a `<ScreenWrapper>`, the bottom safe area inset is handled automatically (`includeSafeAreaPaddingBottom` needs to be set to `true`, but its the default).
Expand Down
6 changes: 5 additions & 1 deletion src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {getUrlWithParams} from './libs/Url';
import SCREENS from './SCREENS';
import type {Screen} from './SCREENS';
import type {CompanyCardFeedWithDomainID} from './types/onyx';
import type {ConnectionName, SageIntacctMappingName} from './types/onyx/Policy';
import type {ConnectionName, PolicyReportFieldType, SageIntacctMappingName} from './types/onyx/Policy';
import type {CustomFieldType} from './types/onyx/PolicyEmployee';

type WorkspaceCompanyCardsAssignCardParams = {
Expand Down Expand Up @@ -2715,6 +2715,10 @@ const ROUTES = {
route: 'workspaces/:policyID/reports/listValues/:reportFieldID?',
getRoute: (policyID: string, reportFieldID?: string) => `workspaces/${policyID}/reports/listValues/${reportFieldID ? encodeURIComponent(reportFieldID) : ''}` as const,
},
WORKSPACE_REPORT_FIELDS_TYPE_SELECTOR: {
route: 'workspaces/:policyID/reports/typeSelector/:currentType?',
getRoute: (policyID: string, currentType?: PolicyReportFieldType) => `workspaces/${policyID}/reports/typeSelector/${currentType ? encodeURIComponent(currentType) : ''}` as const,
},
WORKSPACE_REPORT_FIELDS_ADD_VALUE: {
route: 'workspaces/:policyID/reports/addValue/:reportFieldID?',
getRoute: (policyID: string, reportFieldID?: string) => `workspaces/${policyID}/reports/addValue/${reportFieldID ? encodeURIComponent(reportFieldID) : ''}` as const,
Expand Down
1 change: 1 addition & 0 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ const SCREENS = {
REPORT_FIELDS_VALUE_SETTINGS: 'Workspace_ReportFields_ValueSettings',
REPORT_FIELDS_EDIT_VALUE: 'Workspace_ReportFields_EditValue',
REPORT_FIELDS_EDIT_INITIAL_VALUE: 'Workspace_ReportFields_EditInitialValue',
REPORT_FIELDS_TYPE_SELECTOR: 'Workspace_ReportFields_TypeSelector',
TAX_EDIT: 'Workspace_Tax_Edit',
TAX_NAME: 'Workspace_Tax_Name',
TAX_VALUE: 'Workspace_Tax_Value',
Expand Down
36 changes: 36 additions & 0 deletions src/components/Form/FormValueWatcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {useEffect, useRef} from 'react';
import type {OnyxFormKey} from '@src/ONYXKEYS';
import type {FormOnyxValues} from './types';

type FormValueWatcherProps<TFormID extends OnyxFormKey> = {
/** Form values to watch - the `inputValues` exposed by FormProvider's render prop. */
values: FormOnyxValues<TFormID>;

/**
* Fires when the `values` prop reference changes.
* Receives the new values and the previous values for direct comparison.
*/
onValuesChange: (current: FormOnyxValues<TFormID>, previous: FormOnyxValues<TFormID>) => void;
};

/**
* Render-null primitive that observes a `values` object and fires `onValuesChange` with `(current, previous)`
* whenever the reference changes. Opt-in: only consumers who render it pay the cost.
*
* Designed to live inside `FormProvider`'s render prop so consumers can react to draft-driven updates
* (e.g. from an RHP picker page) without having to wire up their own previous-value ref + effect.
*/
function FormValueWatcher<TFormID extends OnyxFormKey>({values, onValuesChange}: FormValueWatcherProps<TFormID>) {
const previousValuesRef = useRef(values);
useEffect(() => {
if (previousValuesRef.current === values) {
return;
}
const previous = previousValuesRef.current;
previousValuesRef.current = values;
onValuesChange(values, previous);
}, [values, onValuesChange]);
return null;
}

export default FormValueWatcher;
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ const SettingsModalStackNavigator = createModalStackNavigator<SettingsNavigatorP
[SCREENS.WORKSPACE.REPORT_FIELDS_ADD_VALUE]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/ReportFieldsAddListValuePage').default,
[SCREENS.WORKSPACE.REPORT_FIELDS_VALUE_SETTINGS]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/ReportFieldsValueSettingsPage').default,
[SCREENS.WORKSPACE.REPORT_FIELDS_EDIT_INITIAL_VALUE]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/ReportFieldsInitialValuePage').default,
[SCREENS.WORKSPACE.REPORT_FIELDS_TYPE_SELECTOR]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/TypeSelector/TypeSelectorPage').default,
[SCREENS.WORKSPACE.REPORT_FIELDS_EDIT_VALUE]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/ReportFieldsEditValuePage').default,
[SCREENS.WORKSPACE.ACCOUNTING.CERTINIA_IMPORT]: () => require<ReactComponentModule>('../../../../pages/workspace/accounting/certinia/import/CertiniaImportPage').default,
[SCREENS.WORKSPACE.ACCOUNTING.CERTINIA_DIMENSION_MAPPING]: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ const WORKSPACE_TO_RHP: Partial<Record<keyof WorkspaceSplitNavigatorParamList, s
SCREENS.WORKSPACE.REPORT_FIELDS_VALUE_SETTINGS,
SCREENS.WORKSPACE.REPORT_FIELDS_EDIT_VALUE,
SCREENS.WORKSPACE.REPORT_FIELDS_EDIT_INITIAL_VALUE,
SCREENS.WORKSPACE.REPORT_FIELDS_TYPE_SELECTOR,
],
[SCREENS.WORKSPACE.INVOICES]: [SCREENS.WORKSPACE.INVOICES_COMPANY_NAME, SCREENS.WORKSPACE.INVOICES_COMPANY_WEBSITE, SCREENS.WORKSPACE.INVOICES_VERIFY_ACCOUNT],
[SCREENS.WORKSPACE.COMPANY_CARDS]: [
Expand Down
3 changes: 3 additions & 0 deletions src/libs/Navigation/linkingConfig/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,9 @@ const config: LinkingOptions<RootNavigatorParamList>['config'] = {
[SCREENS.WORKSPACE.REPORT_FIELDS_EDIT_INITIAL_VALUE]: {
path: ROUTES.WORKSPACE_EDIT_REPORT_FIELDS_INITIAL_VALUE.route,
},
[SCREENS.WORKSPACE.REPORT_FIELDS_TYPE_SELECTOR]: {
path: ROUTES.WORKSPACE_REPORT_FIELDS_TYPE_SELECTOR.route,
},
[SCREENS.CONNECT_EXISTING_BUSINESS_BANK_ACCOUNT_ROOT]: {
path: ROUTES.BANK_ACCOUNT_CONNECT_EXISTING_BUSINESS_BANK_ACCOUNT.route,
exact: true,
Expand Down
6 changes: 5 additions & 1 deletion src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type NAVIGATORS from '@src/NAVIGATORS';
import type {Route as ExpensifyRoute, Route as Routes} from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {CompanyCardFeedWithDomainID} from '@src/types/onyx';
import type {ConnectionName, SageIntacctMappingName} from '@src/types/onyx/Policy';
import type {ConnectionName, PolicyReportFieldType, SageIntacctMappingName} from '@src/types/onyx/Policy';
import type {CustomFieldType} from '@src/types/onyx/PolicyEmployee';
import type {FileObject} from '@src/types/utils/Attachment';
import type {SIDEBAR_TO_SPLIT} from './linkingConfig/RELATIONS';
Expand Down Expand Up @@ -664,6 +664,10 @@ type SettingsNavigatorParamList = {
policyID: string;
reportFieldID: string;
};
[SCREENS.WORKSPACE.REPORT_FIELDS_TYPE_SELECTOR]: {
policyID: string;
currentType?: PolicyReportFieldType;
};
[SCREENS.WORKSPACE.MEMBER_DETAILS]: {
policyID: string;
accountID: string;
Expand Down
7 changes: 7 additions & 0 deletions src/libs/actions/Policy/ReportField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ function openPolicyReportFieldsPage(policyID: string) {
*/
function setInitialCreateReportFieldsForm() {
Onyx.set(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {
[INPUT_IDS.NAME]: '',
[INPUT_IDS.TYPE]: '',
[INPUT_IDS.INITIAL_VALUE]: '',
});
}
Expand Down Expand Up @@ -171,6 +173,11 @@ function createReportField({name, type, initialValue, listValues, disabledListVa
return;
}

if (!type) {
Log.warn('Report field type is not present');
return;
}

const previousFieldList = policy?.fieldList ?? {};
const fieldID = WorkspaceReportFieldUtils.generateFieldID(name);
const fieldKey = ReportUtils.getReportFieldKey(fieldID);
Expand Down
Loading
Loading