ReferenceManyCount

Displays number of related records for a ReferenceMany relation.

Installation

pnpm dlx shadcn@latest add @shadmin/reference-many-count

Usage

Use <ReferenceManyCount> anywhere inside a RecordContext. You must set the reference and target props to match the relationship:

  • reference is the name of the related resource to fetch (e.g. comments)
  • target is the name of the field in the related resource that points to the current resource (e.g. post_id)
import { ReferenceManyCount } from "@/components/admin";
 
// display the number of comments for the current post
<ReferenceManyCount reference="comments" target="post_id" />;

It counts the number of comments related to the current post, where post_id in the comments resource matches the current post id, using dataProvider.getManyReference() with a pagination of { page: 1, perPage: 1 }.

You can get a count of a subset of the related records by passing a filter prop:

// display the number of published comments for the current post
<ReferenceManyCount
  reference="comments"
  target="post_id"
  filter={{ published: true }}
/>

offline

Element rendered when the network is offline and the reference data is unavailable. Defaults to the inherited offline component from ra-core.

<ReferenceManyCount
  reference="comments"
  target="post_id"
  offline={<span className="text-muted-foreground italic"></span>}
/>

Props

PropRequiredTypeDescription
referenceRequiredstringTarget resource name
targetRequiredstringForeign key field in target resource
filterOptionalobjectExtra filter values
linkOptionalbooleanMake count a link
recordOptionalRaRecordRecord providing id (from context if omitted)
offlineOptionalReactNodeElement rendered when the network is offline
sourceOptionalstringSource field of current record (default id)