-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Fix react-admin requires custom routers to be data routers #9723
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c566862
Fix react-admin requires custom routers to be data routers
fzaninotto 4cd02d0
Fix upgrade guide
fzaninotto 426fe10
Add story and unit test
fzaninotto f6500e1
Update docs/Upgrade.md
fzaninotto 375ad47
Review
fzaninotto aee15f8
[no ci] code review
slax57 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 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import * as React from 'react'; | ||
import { ReactNode } from 'react'; | ||
import { ReactNode, useContext } from 'react'; | ||
import { | ||
FormProvider, | ||
FieldValues, | ||
UseFormProps, | ||
SubmitHandler, | ||
} from 'react-hook-form'; | ||
import { | ||
UNSAFE_DataRouterContext, | ||
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. Unfortunately react-router doesn't expose the DataRouterContext, see remix-run/react-router#11347 |
||
UNSAFE_DataRouterStateContext, | ||
} from 'react-router'; | ||
|
||
import { FormGroupsProvider } from './FormGroupsProvider'; | ||
import { RaRecord } from '../types'; | ||
|
@@ -14,8 +18,13 @@ import { | |
OptionalRecordContextProvider, | ||
SaveHandler, | ||
} from '../controller'; | ||
import { SourceContextProvider, SourceContextValue, useResourceContext } from '../core'; | ||
import { | ||
SourceContextProvider, | ||
SourceContextValue, | ||
useResourceContext, | ||
} from '../core'; | ||
import { ValidateForm } from './getSimpleValidationResolver'; | ||
import { WarnWhenUnsavedChanges } from './WarnWhenUnsavedChanges'; | ||
import { useAugmentedForm } from './useAugmentedForm'; | ||
|
||
/** | ||
|
@@ -45,17 +54,40 @@ import { useAugmentedForm } from './useAugmentedForm'; | |
* | ||
* @link https://react-hook-form.com/docs/useformcontext | ||
*/ | ||
// TODO: remove after upgrading prettier | ||
// eslint-disable-next-line prettier/prettier | ||
export const Form = <RecordType = any>(props: FormProps<RecordType>) => { | ||
const { children, id, className, noValidate = false } = props; | ||
export function Form<RecordType = any>(props: FormProps<RecordType>) { | ||
const { | ||
children, | ||
id, | ||
className, | ||
noValidate = false, | ||
formRootPathname, | ||
warnWhenUnsavedChanges, | ||
WarnWhenUnsavedChangesComponent = WarnWhenUnsavedChanges, | ||
} = props; | ||
const record = useRecordContext(props); | ||
const resource = useResourceContext(props); | ||
const { form, formHandleSubmit } = useAugmentedForm(props); | ||
const sourceContext = React.useMemo<SourceContextValue>(() => ({ | ||
getSource: (source: string) => source, | ||
getLabel: (source: string) => `resources.${resource}.fields.${source}`, | ||
}), [resource]); | ||
const sourceContext = React.useMemo<SourceContextValue>( | ||
() => ({ | ||
getSource: (source: string) => source, | ||
getLabel: (source: string) => | ||
`resources.${resource}.fields.${source}`, | ||
}), | ||
[resource] | ||
); | ||
const dataRouterContext = useContext(UNSAFE_DataRouterContext); | ||
const dataRouterStateContext = useContext(UNSAFE_DataRouterStateContext); | ||
if ( | ||
warnWhenUnsavedChanges && | ||
(!dataRouterContext || !dataRouterStateContext) && | ||
process.env.NODE_ENV === 'development' | ||
) { | ||
console.error( | ||
'Cannot use the warnWhenUnsavedChanges feature outside of a DataRouter. ' + | ||
'The warnWhenUnsavedChanges feature is disabled. ' + | ||
'Remove the warnWhenUnsavedChanges prop or convert your custom router to a Data Router.' | ||
); | ||
} | ||
|
||
return ( | ||
<OptionalRecordContextProvider value={record}> | ||
|
@@ -70,17 +102,31 @@ export const Form = <RecordType = any>(props: FormProps<RecordType>) => { | |
> | ||
{children} | ||
</form> | ||
{warnWhenUnsavedChanges && | ||
dataRouterContext && | ||
dataRouterStateContext && ( | ||
<WarnWhenUnsavedChangesComponent | ||
enable | ||
formRootPathName={formRootPathname} | ||
formControl={form.control} | ||
/> | ||
)} | ||
</FormGroupsProvider> | ||
</FormProvider> | ||
</SourceContextProvider> | ||
</OptionalRecordContextProvider> | ||
); | ||
}; | ||
} | ||
|
||
export type FormProps<RecordType = any> = FormOwnProps<RecordType> & | ||
Omit<UseFormProps, 'onSubmit'> & { | ||
validate?: ValidateForm; | ||
noValidate?: boolean; | ||
WarnWhenUnsavedChangesComponent?: React.ComponentType<{ | ||
enable?: boolean; | ||
formRootPathName?: string; | ||
formControl?: any; | ||
}>; | ||
}; | ||
|
||
export interface FormOwnProps<RecordType = any> { | ||
|
This file contains 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,10 @@ | ||
import { useWarnWhenUnsavedChanges } from './useWarnWhenUnsavedChanges'; | ||
|
||
export const WarnWhenUnsavedChanges = ({ | ||
enable = true, | ||
formRootPathName, | ||
formControl, | ||
}) => { | ||
useWarnWhenUnsavedChanges(enable, formRootPathName, formControl); | ||
return null; | ||
}; |
This file contains 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 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
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.
nitpick: I'd add a link to react-router's documentation for those not knowing what a Data Router is
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.
you're right, added.