forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferenceFieldController.tsx
64 lines (58 loc) · 1.92 KB
/
ReferenceFieldController.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { ReactNode, ReactElement } from 'react';
import get from 'lodash/get';
import { Record } from '../../types';
import getResourceLinkPath, { LinkToFunctionType } from './getResourceLinkPath';
import useReference, { UseReferenceProps } from '../useReference';
interface ChildrenParams extends UseReferenceProps {
resourceLinkPath: string | false;
}
export interface ReferenceFieldControllerProps {
basePath: string;
children: (params: ChildrenParams) => ReactNode;
record?: Record;
reference: string;
resource: string;
source: string;
link?: string | boolean | LinkToFunctionType;
label?: string;
}
/**
* Fetch reference record, and delegate rendering to child component.
*
* The reference prop should be the name of one of the <Resource> components
* added as <Admin> child.
*
* @example
* <ReferenceField label="User" source="userId" reference="users">
* <TextField source="name" />
* </ReferenceField>
*
* By default, includes a link to the <Edit> page of the related record
* (`/users/:userId` in the previous example).
*
* Set the link prop to "show" to link to the <Show> page instead.
*
* @example
* <ReferenceField label="User" source="userId" reference="users" link="show">
* <TextField source="name" />
* </ReferenceField>
*
* You can also prevent `<ReferenceField>` from adding link to children by setting
* `link` to false.
*
* @example
* <ReferenceField label="User" source="userId" reference="users" link={false}>
* <TextField source="name" />
* </ReferenceField>
*/
export const ReferenceFieldController = (
props: ReferenceFieldControllerProps
) => {
const { children, record, source, ...rest } = props;
const id = get(record, source);
return children({
...useReference({ ...rest, id }),
resourceLinkPath: getResourceLinkPath({ ...rest, record, source }),
}) as ReactElement<any>;
};
export default ReferenceFieldController;