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

[RFR] Add link function to ReferenceField #3282

Merged
merged 6 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/Fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,15 @@ 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.

```jsx
// Custom path
<ReferenceField label="User" source="userId" reference="users" linkType={() => '/my/path'}>
fargito marked this conversation as resolved.
Show resolved Hide resolved
<TextField source="name" />
</ReferenceField>
```

**Tip**: React-admin uses `CRUD_GET_ONE_REFERENCE` action to accumulate and deduplicate the ids of the referenced records to make *one* `GET_MANY` call for the entire list, instead of n `GET_ONE` calls. So for instance, if the API returns the following list of comments:

```jsx
Expand Down
9 changes: 7 additions & 2 deletions packages/ra-core/src/controller/field/useReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import { crudGetManyAccumulate } from '../../actions';
import { linkToRecord } from '../../util';
import { Record, ReduxState } from '../../types';

type linkTypeFunction = () => string;

interface Option {
allowEmpty?: boolean;
basePath: string;
record?: Record;
reference: string;
resource: string;
source: string;
linkType: string | boolean;
linkType: string | boolean | linkTypeFunction;
}

export interface UseReferenceProps {
Expand Down Expand Up @@ -50,7 +52,8 @@ 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} option.linkType The type of the link toward the referenced record. edit, show of false for no link (default to edit)
* @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
fargito marked this conversation as resolved.
Show resolved Hide resolved
* @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 @@ -80,6 +83,8 @@ export const useReference = ({
const rootPath = basePath.replace(resource, reference);
const resourceLinkPath = !linkType
? false
: typeof linkType === 'function'
? linkType()
fargito marked this conversation as resolved.
Show resolved Hide resolved
: linkToRecord(rootPath, sourceId, linkType as string);

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/field/ReferenceField.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ ReferenceField.propTypes = {
sortBy: PropTypes.string,
source: PropTypes.string.isRequired,
translateChoice: PropTypes.func,
linkType: PropTypes.oneOfType([PropTypes.string, PropTypes.bool])
linkType: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.func])
.isRequired,
};

Expand Down