Skip to content

Commit

Permalink
add sourceId and reference as arguments to linkType function
Browse files Browse the repository at this point in the history
  • Loading branch information
fargito committed Jun 4, 2019
1 parent 2513035 commit d028458
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/Fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,11 @@ You can also prevent `<ReferenceField>` from adding link to children by setting
</ReferenceField>
```

You can also use a custom `linkType` function to get a custom path for the children.
You can also use a custom `linkType` function to get a custom path for the children. This function must accept sourceId and reference as arguments.

```jsx
// Custom path
<ReferenceField label="User" source="userId" reference="users" linkType={() => '/my/path'}>
<ReferenceField label="User" source="userId" reference="users" linkType={(sourceId, reference) => `/my/path/to/${reference}/${sourceId}`}>
<TextField source="name" />
</ReferenceField>
```
Expand Down
7 changes: 3 additions & 4 deletions packages/ra-core/src/controller/field/useReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { crudGetManyAccumulate } from '../../actions';
import { linkToRecord } from '../../util';
import { Record, ReduxState } from '../../types';

type linkTypeFunction = () => string;
type linkTypeFunction = (sourceId: string, reference: string) => string;

interface Option {
allowEmpty?: boolean;
Expand Down Expand Up @@ -52,8 +52,7 @@ export interface UseReferenceProps {
* @param {Object} option
* @param {boolean} option.allowEmpty do we allow for no referenced record (default to false)
* @param {string} option.basePath basepath to current resource
* @param {string | false | linkTypeFunction} option.linkType The type of the link toward the referenced record. 'edit', 'show' or
* false for no link (default to edit). Alternatively a function that returns a string
* @param {string | false | linkTypeFunction} option.linkType The type of the link toward the referenced record. 'edit', 'show' or false for no link (default to edit). Alternatively a function that returns a string
* @param {Object} option.record The The current resource record
* @param {string} option.reference The linked resource name
* @param {string} option.resource The current resource name
Expand Down Expand Up @@ -84,7 +83,7 @@ export const useReference = ({
const resourceLinkPath = !linkType
? false
: typeof linkType === 'function'
? linkType()
? linkType(sourceId, reference)
: linkToRecord(rootPath, sourceId, linkType as string);

return {
Expand Down

0 comments on commit d028458

Please sign in to comment.