Skip to content

Commit

Permalink
enable linkType function for ReferenceField
Browse files Browse the repository at this point in the history
  • Loading branch information
fargito committed May 28, 2019
1 parent ade3229 commit c5fb016
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
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
* @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()
: 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

0 comments on commit c5fb016

Please sign in to comment.