UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

74 lines 3.69 kB
import * as React from 'react'; import { ReferenceFieldContextProvider } from "./ReferenceFieldContext.js"; import { useReferenceFieldController, } from "./useReferenceFieldController.js"; import { ResourceContextProvider } from "../../core/index.js"; import { RecordContextProvider } from "../record/index.js"; import { useFieldValue } from "../../util/index.js"; /** * Fetch reference record, and render its representation, or delegate rendering to child component. * * The reference prop should be the name of one of the <Resource> components * added as <Admin> child. * * @example // using recordRepresentation * <ReferenceFieldBase source="userId" reference="users" /> * * @example // using a Field component to represent the record * <ReferenceFieldBase source="userId" reference="users"> * <TextField source="name" /> * </ReferenceFieldBase> * * @example // 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. * <ReferenceFieldBase source="userId" reference="users" link="show" /> * * @example // You can also prevent `<ReferenceFieldBase>` from adding link to children * // by setting `link` to false. * <ReferenceFieldBase source="userId" reference="users" link={false} /> * * @example // Alternatively, you can also pass a custom function to `link`. * // It must take reference and record as arguments and return a string * <ReferenceFieldBase source="userId" reference="users" link={(record, reference) => "/path/to/${reference}/${record}"} /> * * @default * In previous versions of React-Admin, the prop `linkType` was used. It is now deprecated and replaced with `link`. However * backward-compatibility is still kept */ export var ReferenceFieldBase = function (props) { var children = props.children, render = props.render, loading = props.loading, error = props.error, empty = props.empty, offline = props.offline; var id = useFieldValue(props); var controllerProps = useReferenceFieldController(props); if (!render && !children) { throw new Error("<ReferenceFieldBase> requires either a 'render' prop or 'children' prop"); } var controllerError = controllerProps.error, isPending = controllerProps.isPending, isPaused = controllerProps.isPaused, referenceRecord = controllerProps.referenceRecord; var shouldRenderLoading = id != null && !isPaused && isPending && loading !== false && loading !== undefined; var shouldRenderOffline = isPaused && isPending && offline !== false && offline !== undefined; var shouldRenderError = !!controllerError && error !== false && error !== undefined; var shouldRenderEmpty = !isPaused && (!id || (!referenceRecord && !controllerError && !isPending && empty !== false && empty !== undefined)); return (React.createElement(ResourceContextProvider, { value: props.reference }, React.createElement(ReferenceFieldContextProvider, { value: controllerProps }, React.createElement(RecordContextProvider, { value: referenceRecord }, shouldRenderLoading ? loading : shouldRenderOffline ? offline : shouldRenderError ? error : shouldRenderEmpty ? empty : render ? render(controllerProps) : children)))); }; //# sourceMappingURL=ReferenceFieldBase.js.map