Skip to content
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

Merged
merged 9 commits into from
Jun 25, 2024

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Jun 24, 2024

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 the link 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 the ReferenceField should link to the edit view even though the Resource 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 nested CustomRoute that matches the link.

Solution

Make <ReferenceField> default to show when link 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 the link prop.

Before

link default edit show false
Resource hasShow hasEdit edit edit show no link
Resource hasEdit edit edit no link no link
Resource hasShow no link no link show no link
Resource no link no link no link no link

This PR

link default edit show false
Resource hasShow hasEdit show edit show no link
Resource hasEdit edit edit show no link
Resource hasShow show edit show no link
Resource no link edit show no link

The link behavior of <Datagrid rowClick>, <ReferenceField link> and <ReferenceOneField link> is now consistent.

@fzaninotto fzaninotto added the RFR Ready For Review label Jun 24, 2024
@fzaninotto fzaninotto changed the title Update ReferenceField to render a link to show view when relevant Update <ReferenceField> to render a link to the show view when relevant Jun 24, 2024
@@ -146,25 +145,21 @@ export const ReferenceFieldView = <
if (link) {
return (
<Root className={className} sx={sx}>
<RecordContextProvider value={referenceRecord}>
Copy link
Member Author

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.

const resourceDefinition = useResourceDefinition({ resource });

// eslint-disable-next-line react-hooks/exhaustive-deps
const linkFunc = useCallback(
Copy link
Member Author

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.

Copy link
Collaborator

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?

Copy link
Member Author

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

@djhi djhi added this to the 5.1.0 milestone Jun 25, 2024
@djhi djhi merged commit a3bd5a6 into next Jun 25, 2024
14 checks passed
@djhi djhi deleted the referencefield-default-link branch June 25, 2024 14:18
Comment on lines -150 to +143
navigate(type);
if (path === false || path == null) {
return;
}
navigate(path, {
state: { _scrollToTop: true },
});
Copy link
Member Author

@fzaninotto fzaninotto Jun 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants