Raw
1 import { Minus } from "@medusajs/icons"
2 import * as React from "react"
3
4 import { Button } from "@/components/button"
5 import { clx } from "@/utils/clx"
6
7 /**
8 * This component is based on the table element and its various children:
9 *
10 * - `Table`: `table`
11 * - `Table.Header`: `thead`
12 * - `Table.Row`: `tr`
13 * - `Table.HeaderCell`: `th`
14 * - `Table.Body`: `tbody`
15 * - `Table.Cell`: `td`
16 *
17 * Each component supports the props or attributes of its equivalent HTML element.
18 */
19 const Root = React.forwardRef<
20 HTMLTableElement,
21 React.HTMLAttributes<HTMLTableElement>
22 >(({ className, ...props }, ref) => (
23 <table
24 ref={ref}
25 className={clx("text-ui-fg-subtle txt-compact-small w-full", className)}
26 {...props}
27 />
28 ))
29 Root.displayName = "Table"
30
31 const Row = React.forwardRef<
32 HTMLTableRowElement,
33 React.HTMLAttributes<HTMLTableRowElement>
34 >(({ className, ...props }, ref) => (
35 <tr
36 ref={ref}
37 className={clx(
38 "bg-ui-bg-base hover:bg-ui-bg-base-hover border-ui-border-base transition-fg border-b",
39 "[&_td:last-child]:pr-6 [&_th:last-child]:pr-6",
40 "[&_td:first-child]:pl-6 [&_th:first-child]:pl-6",
41 className
42 )}
43 {...props}
44 />
45 ))
46 Row.displayName = "Table.Row"
47
48 const Cell = React.forwardRef<
49 HTMLTableCellElement,
50 React.HTMLAttributes<HTMLTableCellElement>
51 >(({ className, ...props }, ref) => (
52 <td ref={ref} className={clx("h-12 py-0 pl-0 pr-6", className)} {...props} />
53 ))
54 Cell.displayName = "Table.Cell"
55
56 const Header = React.forwardRef<
57 HTMLTableSectionElement,
58 React.HTMLAttributes<HTMLTableSectionElement>
59 >(({ className, ...props }, ref) => (
60 <thead
61 ref={ref}
62 className={clx(
63 "border-ui-border-base txt-compact-small-plus [&_tr]:bg-ui-bg-subtle [&_tr]:hover:bg-ui-bg-subtle border-y",
64 className
65 )}
66 {...props}
67 />
68 ))
69 Header.displayName = "Table.Header"
70
71 const HeaderCell = React.forwardRef<
72 HTMLTableCellElement,
73 React.TdHTMLAttributes<HTMLTableCellElement>
74 >(({ className, ...props }, ref) => (
75 <th
76 ref={ref}
77 className={clx(
78 "txt-compact-small-plus h-12 py-0 pl-0 pr-6 text-left",
79 className
80 )}
81 {...props}
82 />
83 ))
84 HeaderCell.displayName = "Table.HeaderCell"
85
86 const Body = React.forwardRef<
87 HTMLTableSectionElement,
88 React.HTMLAttributes<HTMLTableSectionElement>
89 >(({ className, ...props }, ref) => (
90 <tbody
91 ref={ref}
92 className={clx("border-ui-border-base border-b", className)}
93 {...props}
94 />
95 ))
96 Body.displayName = "Table.Body"
97
98 interface TablePaginationProps extends React.HTMLAttributes<HTMLDivElement> {
99 count: number
100 pageSize: number
101 pageIndex: number
102 pageCount: number
103 canPreviousPage: boolean
104 canNextPage: boolean
105 translations?: {
106 of?: string
107 results?: string
108 pages?: string
109 prev?: string
110 next?: string
111 }
112 previousPage: () => void
113 nextPage: () => void
114 }
115
116 /**
117 * This component is based on the `div` element and supports all of its props
118 */
119 const Pagination = React.forwardRef<HTMLDivElement, TablePaginationProps>(
120 (
121 {
122 className,
123 /**
124 * The total number of items.
125 */
126 count,
127 /**
128 * The number of items per page.
129 */
130 pageSize,
131 /**
132 * The total number of pages.
133 */
134 pageCount,
135 /**
136 * The current page index.
137 */
138 pageIndex,
139 /**
140 * Whether there's a previous page that can be navigated to.
141 */
142 canPreviousPage,
143 /**
144 * Whether there's a next page that can be navigated to.
145 */
146 canNextPage,
147 /**
148 * A function that handles navigating to the next page.
149 * This function should handle retrieving data for the next page.
150 */
151 nextPage,
152 /**
153 * A function that handles navigating to the previous page.
154 * This function should handle retrieving data for the previous page.
155 */
156 previousPage,
157 /**
158 * An optional object of words to use in the pagination component.
159 * Use this to override the default words, or translate them into another language.
160 */
161 translations = {
162 of: "of",
163 results: "results",
164 pages: "pages",
165 prev: "Prev",
166 next: "Next",
167 },
168 ...props
169 }: TablePaginationProps,
170 ref
171 ) => {
172 const { from, to } = React.useMemo(() => {
173 const from = count === 0 ? count : pageIndex * pageSize + 1
174 const to = Math.min(count, (pageIndex + 1) * pageSize)
175
176 return { from, to }
177 }, [count, pageIndex, pageSize])
178
179 return (
180 <div
181 ref={ref}
182 className={clx(
183 "text-ui-fg-subtle txt-compact-small-plus flex w-full items-center justify-between px-3 py-4",
184 className
185 )}
186 {...props}
187 >
188 <div className="inline-flex items-center gap-x-1 px-3 py-[5px]">
189 <p>{from}</p>
190 <Minus className="text-ui-fg-muted" />
191 <p>{`${to} ${translations.of} ${count} ${translations.results}`}</p>
192 </div>
193 <div className="flex items-center gap-x-2">
194 <div className="inline-flex items-center gap-x-1 px-3 py-[5px]">
195 <p>
196 {pageIndex + 1} {translations.of} {Math.max(pageCount, 1)}{" "}
197 {translations.pages}
198 </p>
199 </div>
200 <Button
201 type="button"
202 variant={"transparent"}
203 onClick={previousPage}
204 disabled={!canPreviousPage}
205 >
206 {translations.prev}
207 </Button>
208 <Button
209 type="button"
210 variant={"transparent"}
211 onClick={nextPage}
212 disabled={!canNextPage}
213 >
214 {translations.next}
215 </Button>
216 </div>
217 </div>
218 )
219 }
220 )
221 Pagination.displayName = "Table.Pagination"
222
223 const Table = Object.assign(Root, {
224 Row,
225 Cell,
226 Header,
227 HeaderCell,
228 Body,
229 Pagination,
230 })
231
232 export { Table }