-
Notifications
You must be signed in to change notification settings - Fork 782
Expand file tree
/
Copy pathshow.tsx
More file actions
183 lines (174 loc) · 4.6 KB
/
Copy pathshow.tsx
File metadata and controls
183 lines (174 loc) · 4.6 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbPage,
} from "@/components/admin/breadcrumb";
import type { ShowBaseProps } from "ra-core";
import {
ShowBase,
Translate,
useCreatePath,
useHasDashboard,
useShowContext,
useGetRecordRepresentation,
useGetResourceLabel,
useResourceContext,
useResourceDefinition,
} from "ra-core";
import type { ReactNode } from "react";
import { Link } from "react-router";
import { cn } from "@/lib/utils";
import { EditButton } from "@/components/admin/edit-button";
export interface ShowProps
extends ShowViewProps, Omit<ShowBaseProps, "children"> {}
/**
* A complete show page with breadcrumb, title, and default actions.
*
* Combines data fetching and UI layout for displaying record details. Inside, use
* RecordField to display individual fields with labels.
*
* @see {@link https://marmelab.com/shadcn-admin-kit/docs/show/ Show documentation}
*
* @example
* import { RecordField, NumberField, ReferenceField, Show } from "@/components/admin";
*
* export const ProductShow = () => (
* <Show>
* <div className="flex flex-col gap-4">
* <RecordField source="reference" />
* <RecordField source="category_id">
* <ReferenceField source="category_id" reference="categories" />
* </RecordField>
* <RecordField
* source="price"
* render={(record) => Intl.NumberFormat().format(record.price)}
* />
* <RecordField source="size" field={NumberField} />
* </div>
* </Show>
* );
*/
export const Show = ({
actions,
children,
className,
disableAuthentication,
disableBreadcrumb,
id,
loading,
queryOptions,
render,
resource,
title,
}: ShowProps) => (
<ShowBase
id={id}
resource={resource}
queryOptions={queryOptions}
disableAuthentication={disableAuthentication}
render={render}
loading={loading}
>
<ShowView
title={title}
actions={actions}
className={className}
disableBreadcrumb={disableBreadcrumb}
>
{children}
</ShowView>
</ShowBase>
);
export interface ShowViewProps {
actions?: ReactNode;
disableBreadcrumb?: boolean;
children: ReactNode;
className?: string;
emptyWhileLoading?: boolean;
title?: ReactNode | string | false;
}
/**
* The view component for Show pages with layout and UI.
*
* Renders breadcrumb, title, and default actions for show pages. Use Show instead unless you need
* custom data fetching logic with ShowBase.
*
* @example
* import { ShowBase, ShowView, SimpleShowLayout } from '@/components/admin';
*
* export const PostShow = () => (
* <ShowBase>
* <ShowView>
* <SimpleShowLayout>...</SimpleShowLayout>
* </ShowView>
* </ShowBase>
* );
*/
export const ShowView = ({
actions,
children,
className,
disableBreadcrumb,
emptyWhileLoading,
title,
}: ShowViewProps) => {
const context = useShowContext();
const resource = useResourceContext();
if (!resource) {
throw new Error(
"The ShowView component must be used within a ResourceContextProvider",
);
}
const getResourceLabel = useGetResourceLabel();
const listLabel = getResourceLabel(resource, 2);
const createPath = useCreatePath();
const listLink = createPath({
resource,
type: "list",
});
const getRecordRepresentation = useGetRecordRepresentation(resource);
const recordRepresentation = getRecordRepresentation(context.record);
const { hasEdit } = useResourceDefinition({ resource });
const hasDashboard = useHasDashboard();
if (context.isLoading || !context.record) {
return null;
}
if (!context.record && emptyWhileLoading) {
return null;
}
return (
<>
{!disableBreadcrumb && (
<Breadcrumb>
{hasDashboard && (
<BreadcrumbItem>
<Link to="/">
<Translate i18nKey="ra.page.dashboard">Home</Translate>
</Link>
</BreadcrumbItem>
)}
<BreadcrumbItem>
<Link to={listLink}>{listLabel}</Link>
</BreadcrumbItem>
<BreadcrumbPage>{recordRepresentation}</BreadcrumbPage>
</Breadcrumb>
)}
<div
className={cn(
"flex justify-between items-start flex-wrap gap-2 my-2",
className,
)}
>
<h2 className="text-2xl font-bold tracking-tight">
{title !== undefined ? title : context.defaultTitle}
</h2>
{actions ?? (
<div className="flex justify-end items-center">
{hasEdit ? <EditButton /> : null}
</div>
)}
</div>
<div className="my-2">{children}</div>
</>
);
};