-
Notifications
You must be signed in to change notification settings - Fork 782
Expand file tree
/
Copy pathbadge-field.tsx
More file actions
64 lines (59 loc) · 1.87 KB
/
Copy pathbadge-field.tsx
File metadata and controls
64 lines (59 loc) · 1.87 KB
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 * 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<typeof Badge>;
/**
* 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 = () => (
* <List>
* <DataTable>
* <DataTable.Col source="id" />
* <DataTable.Col>
* <BadgeField source="status" variant="outline" />
* </DataTable.Col>
* </DataTable>
* </List>
* );
*/
export const BadgeField = <RecordType extends RaRecord = RaRecord>({
defaultValue,
source,
record,
empty,
variant = "outline",
...rest
}: BadgeFieldProps<RecordType>) => {
const value = useFieldValue({ defaultValue, source, record });
const translate = useTranslate();
if (value == null) {
return empty && typeof empty === "string"
? translate(empty, { _: empty })
: empty;
}
return (
<Badge variant={variant} {...rest}>
{typeof value !== "string" ? value.toString() : value}
</Badge>
);
};
export interface BadgeFieldProps<RecordType extends RaRecord = RaRecord>
extends FieldProps<RecordType>, BadgeProps {
variant?: "default" | "outline" | "secondary" | "destructive";
}