-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Migrate TypeSelectorModal to react-navigation #91102
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
Open
sharabai
wants to merge
6
commits into
Expensify:main
Choose a base branch
from
software-mansion-labs:modal-migrate/type-selector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29393a9
Migrate TypeSelectorModal to react-navigation
sharabai 6c1b37c
Add short description to FORMS.md
sharabai 063644d
Add specific goBack route
sharabai 02e1616
Remove display name property
sharabai ba672dd
Fix TS
sharabai 2a86e78
Fix prettier
sharabai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| 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; | ||
| } | ||
|
|
||
| FormValueWatcher.displayName = 'FormValueWatcher'; | ||
|
|
||
| export default FormValueWatcher; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
src/pages/workspace/reports/TypeSelector/TypeSelectorModal.tsx
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
src/pages/workspace/reports/TypeSelector/TypeSelectorPage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import React from 'react'; | ||
| import {View} from 'react-native'; | ||
| import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
| import ScreenWrapper from '@components/ScreenWrapper'; | ||
| import Text from '@components/Text'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import {setDraftValues} from '@libs/actions/FormActions'; | ||
| import DateUtils from '@libs/DateUtils'; | ||
| 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 {ReportFieldItemType} from '@pages/workspace/reports/ReportFieldTypePicker'; | ||
| import ReportFieldTypePicker from '@pages/workspace/reports/ReportFieldTypePicker'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import type SCREENS from '@src/SCREENS'; | ||
| import INPUT_IDS from '@src/types/form/WorkspaceReportFieldForm'; | ||
| import type {PolicyReportFieldType} from '@src/types/onyx/Policy'; | ||
|
|
||
| type TypeSelectorPageProps = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.REPORT_FIELDS_TYPE_SELECTOR>; | ||
|
|
||
| function getDefaultInitialValueForReportFieldType(type: PolicyReportFieldType): string { | ||
| if (type === CONST.REPORT_FIELD_TYPES.DATE) { | ||
| return DateUtils.extractDate(new Date().toString()); | ||
| } | ||
| if (type === CONST.REPORT_FIELD_TYPES.FORMULA) { | ||
| return '{report:id}'; | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| function TypeSelectorPage({ | ||
| route: { | ||
| params: {policyID, currentType}, | ||
| }, | ||
| }: TypeSelectorPageProps) { | ||
| const styles = useThemeStyles(); | ||
| const {translate} = useLocalize(); | ||
| const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); | ||
|
|
||
| const onTypeSelected = (item: ReportFieldItemType) => { | ||
| setDraftValues(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM, { | ||
| [INPUT_IDS.TYPE]: item.value, | ||
| [INPUT_IDS.INITIAL_VALUE]: getDefaultInitialValueForReportFieldType(item.value), | ||
| }); | ||
| Navigation.goBack(ROUTES.WORKSPACE_CREATE_REPORT_FIELD.getRoute(policyID)); | ||
| }; | ||
|
|
||
| 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} | ||
| includePaddingTop={false} | ||
| enableEdgeToEdgeBottomSafeAreaPadding | ||
| testID="TypeSelectorPage" | ||
| > | ||
| <HeaderWithBackButton title={translate('common.type')} /> | ||
| <View style={[styles.ph5, styles.pb4]}> | ||
| <Text style={[styles.sidebarLinkText, styles.optionAlternateText]}>{translate('workspace.reportFields.typeInputSubtitle')}</Text> | ||
| </View> | ||
| <ReportFieldTypePicker | ||
| defaultValue={currentType ?? CONST.REPORT_FIELD_TYPES.TEXT} | ||
| onOptionSelected={onTypeSelected} | ||
| /> | ||
| </ScreenWrapper> | ||
| </AccessOrNotFoundWrapper> | ||
| ); | ||
| } | ||
|
|
||
| export default TypeSelectorPage; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we add this logic inside FormProvider?
Uh oh!
There was an error while loading. Please reload this page.
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.
@ZhenjaHorbach I don't think so. I separated it out into a component because
FormProviderhas more than 500 references across the codebase. And I was the only consumer. I wanted to be more modular and have something I could use, but also something that could be used by others if there's a need for it.Uh oh!
There was an error while loading. Please reload this page.
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.
the
useEffectwould run on every change of values for those 100+ consumers that don't use it.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.
Okay then
I'm just a little confused by render-null component and how it is used
But on the other hand, there seems to be no better solution in the current situation
So let's leave it as is 😁