import * as React from "react"; import type { RaRecord } from "ra-core"; import { useFieldValue, useTranslate } from "ra-core"; import { Badge } from "@/components/ui/badge"; import type { FieldProps } from "@/lib/field.type"; type BadgeProps = React.ComponentProps; /** * Displays a text value inside a styled badge component. * * This field wraps values in a Badge UI component with customizable variants (default, outline, secondary, destructive). * Use it to highlight status values, tags, or categorical information. * To be used with RecordField or DataTable.Col components, or anywhere a RecordContext is available. * * @see {@link https://marmelab.com/shadcn-admin-kit/docs/badgefield/ BadgeField documentation} * @see {@link https://ui.shadcn.com/docs/components/badge Badge documentation} * * @example * import { * List, * DataTable, * BadgeField, * } from '@/components/admin'; * * const OrderList = () => ( * * * * * * * * * ); */ export const BadgeField = ({ defaultValue, source, record, empty, variant = "outline", ...rest }: BadgeFieldProps) => { const value = useFieldValue({ defaultValue, source, record }); const translate = useTranslate(); if (value == null) { return empty && typeof empty === "string" ? translate(empty, { _: empty }) : empty; } return ( {typeof value !== "string" ? value.toString() : value} ); }; export interface BadgeFieldProps extends FieldProps, BadgeProps { variant?: "default" | "outline" | "secondary" | "destructive"; }