From d4ba8e92b2d4fb69780060dfbbefa9515a7632b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Gabriele?= Date: Thu, 4 Feb 2021 14:00:38 +0100 Subject: [PATCH 1/2] [Doc] Add documentation for `linkToRecord` --- docs/Fields.md | 15 +++++++++++++++ docs/Reference.md | 1 + 2 files changed, 16 insertions(+) diff --git a/docs/Fields.md b/docs/Fields.md index e7deef84187..1b6df629999 100644 --- a/docs/Fields.md +++ b/docs/Fields.md @@ -1524,6 +1524,21 @@ export const UserList = (props) => ( **Tip**: In such custom fields, the `source` is optional. React-admin uses it to determine which column to use for sorting when the column header is clicked. In case you use the `source` property for additional purposes, the sorting can be overridden by the `sortBy` property on any `Field` component. +### Linking to other records + +Your custom Field component might need to display a link to another record. React Admin provides a `linkToRecord(basePath, id[, linkType])` method for this purpose. + +```js +import { linkToRecord } from 'react-admin'; +import { Link } from 'react-router-dom'; + +const MyCustomField = ({ record: post }) => { + const linkToUser = linkToRecord('users', post.user_id, 'show'); + + return {seller.username}; +}; +``` + ## Third-Party Components You can find components for react-admin in third-party repositories. diff --git a/docs/Reference.md b/docs/Reference.md index be825a92f9e..8d46d3ec83a 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -140,6 +140,7 @@ title: "Reference" * `` * [``](./Fields.md#urlfield) * [``](https://marmelab.com/ra-enterprise/modules/ra-form-layout#wizardform) +* [`linkToRecord`](./Fields.md#linking-to-other-records) * [`useAppLocationState`](https://marmelab.com/ra-enterprise/modules/ra-navigation#useapplocationstate-retrieve-and-define-app-location) * [`useAppLocationMatcher`](https://marmelab.com/ra-enterprise/modules/ra-navigation#useapplocationmatcher-apply-a-matching-on-the-current-app-location) * [`useAuthenticated`](./Authentication.md#useauthenticated-hook) From 69706ef64a0a217d09bb87020fb843362086ac73 Mon Sep 17 00:00:00 2001 From: Jeremy Gabriele Date: Thu, 4 Feb 2021 14:15:19 +0100 Subject: [PATCH 2/2] Update `linkToRecord`'s `basePath` to be absolute Co-authored-by: Gildas Garcia <1122076+djhi@users.noreply.github.com> --- docs/Fields.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Fields.md b/docs/Fields.md index 1b6df629999..a320b3e9aa0 100644 --- a/docs/Fields.md +++ b/docs/Fields.md @@ -1533,7 +1533,7 @@ import { linkToRecord } from 'react-admin'; import { Link } from 'react-router-dom'; const MyCustomField = ({ record: post }) => { - const linkToUser = linkToRecord('users', post.user_id, 'show'); + const linkToUser = linkToRecord('/users', post.user_id, 'show'); return {seller.username}; };