ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
36 lines • 1.6 kB
JavaScript
import React from 'react';
import { useReferenceManyFieldController, } from "./useReferenceManyFieldController.js";
import { useTimeout } from "../../util/hooks.js";
/**
* Fetch and render the number of records related to the current one
*
* Relies on dataProvider.getManyReference() returning a total property
*
* @example // Display the number of comments for the current post
* <ReferenceManyCountBase reference="comments" target="post_id" />
*
* @example // Display the number of published comments for the current post
* <ReferenceManyCountBase reference="comments" target="post_id" filter={{ is_published: true }} />
*/
export const ReferenceManyCountBase = (props) => {
const { loading, error, offline, timeout = 1000, ...rest } = props;
const oneSecondHasPassed = useTimeout(timeout);
const { isPaused, isPending, error: fetchError, total, } = useReferenceManyFieldController({
...rest,
page: 1,
perPage: 1,
});
const shouldRenderLoading = isPending && !isPaused && loading !== undefined && loading !== false;
const shouldRenderOffline = isPending && isPaused && offline !== undefined && offline !== false;
const shouldRenderError = !isPending && fetchError && error !== undefined && error !== false;
return (React.createElement(React.Fragment, null, shouldRenderLoading
? oneSecondHasPassed
? loading
: null
: shouldRenderOffline
? offline
: shouldRenderError
? error
: total));
};
//# sourceMappingURL=ReferenceManyCountBase.js.map