-
-
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
Update <ReferenceField>
to render a link to the show view when relevant
#9951
Conversation
<ReferenceField>
to render a link to the show view when relevant
@@ -146,25 +145,21 @@ export const ReferenceFieldView = < | |||
if (link) { | |||
return ( | |||
<Root className={className} sx={sx}> | |||
<RecordContextProvider value={referenceRecord}> |
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 RecordContextProvider was added twice, as ReferenceFieldBase
already does it.
packages/ra-core/src/controller/field/useReferenceFieldController.ts
Outdated
Show resolved
Hide resolved
const resourceDefinition = useResourceDefinition({ resource }); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const linkFunc = useCallback( |
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.
We cannot use useEvent
here because we need to call the function right away, and useEvent
throws an Error in that case.
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.
That means that if the function did change, it will never be updated here though right?
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.
yes indeed, that's a limitation I'm willing to accept
navigate(type); | ||
if (path === false || path == null) { | ||
return; | ||
} | ||
navigate(path, { | ||
state: { _scrollToTop: true }, | ||
}); |
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.
This is a small breaking change: if the user passed a path
or a function that returns a path
, it will have scrollToTop: true
while it used not to.
But it's easy to bypass by using a function that calls navigate
itself and returns false
to cancel the default navigation.
Problem
<ReferenceField>
renders a link to the edit view by default. When a resource has a show view but no edit view, and the developer hasn't specified thelink
type, a<ReferenceField>
targeting that resource doesn't show any link.Also, when there is a conflict between the
<ReferenceField link>
and the associated<Resource>
(e.g. when the developer specified that theReferenceField
should link to theedit
view even though theResource
doesn't defined such a view), react-admin ignores the developer choice and removes the link. Yet the developer may want to link to a nestedCustomRoute
that matches thelink
.Solution
Make
<ReferenceField>
default toshow
whenlink
is undefined and there are both an edit and a show view.Make
<ReferenceField>
smarter so that it can link to the show view if it exists.Make
<ReferenceField>
always honor thelink
prop.Before
This PR
The link behavior of
<Datagrid rowClick>
,<ReferenceField link>
and<ReferenceOneField link>
is now consistent.