Skip to content

Commit 67d2ff0

Browse files
authored
Merge pull request #266 from duyet/chore/ui
chore(ui): update table footer to use "footnote" variable for metadata
2 parents 1f243c7 + fddab82 commit 67d2ff0

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

app/[query]/table.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ export default async function Table({
6969
},
7070
})
7171

72+
const footerText = `${metadata.rows} row(s) in ${metadata.duration} seconds.`
73+
7274
return (
7375
<DataTable
7476
title={title}
7577
config={config}
7678
data={data}
77-
footer={`${metadata.rows} row(s) in ${metadata.duration} seconds.`}
79+
footnote={footerText}
7880
/>
7981
)
8082
} catch (error) {

components/data-table/data-table.cy.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,33 +227,33 @@ describe('<DataTable />', () => {
227227
cy.get('pre').should('not.exist')
228228
})
229229

230-
it('should display with footer prop as text', () => {
230+
it('should display with footnote prop as text', () => {
231231
cy.mount(
232232
<DataTable
233233
title="Test Table"
234234
config={config}
235235
data={[]}
236-
footer="This is footer"
236+
footnote="This is footnote"
237237
/>
238238
)
239239

240-
cy.get('div').contains('This is footer')
240+
cy.get('div').contains('This is footnote')
241241
})
242242

243-
it('should display with footer prop as elemenent', () => {
243+
it('should display with footnote prop as elemenent', () => {
244244
cy.mount(
245245
<DataTable
246246
title="Test Table"
247247
config={config}
248248
data={[]}
249-
footer={<div className="footer">This is footer</div>}
249+
footnote={<div className="footnote">This is footnote</div>}
250250
/>
251251
)
252252

253-
cy.get('.footer').contains('This is footer').should('exist')
253+
cy.get('.footnote').contains('This is footnote').should('exist')
254254
})
255255

256-
it('should display without footer props, with 1 row, pageSize=10', () => {
256+
it('should display without footnote props, with 1 row, pageSize=10', () => {
257257
cy.mount(
258258
<DataTable
259259
config={config}
@@ -266,7 +266,7 @@ describe('<DataTable />', () => {
266266
cy.get('[aria-label="Pagination"]').should('is.not.visible')
267267
})
268268

269-
it('should display without footer, with 10 row, pageSize=10', () => {
269+
it('should display without footnote, with 10 row, pageSize=10', () => {
270270
const data = []
271271
for (let i = 0; i < 10; i++) {
272272
data.push({ col1: '1', col2: '2' })

components/data-table/data-table.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { uniq } from '@/lib/utils'
3333
import { ColumnVisibilityButton } from './buttons/column-visibility'
3434
import { ShowSQLButton } from './buttons/show-sql'
3535
import { DataTableToolbar } from './data-table-toolbar'
36+
import { Footnote, type FootnoteProps } from './footnote'
3637

3738
interface DataTableProps<TData extends RowData> {
3839
title?: string
@@ -43,7 +44,7 @@ interface DataTableProps<TData extends RowData> {
4344
data: TData[]
4445
defaultPageSize?: number
4546
showSQL?: boolean
46-
footer?: string | React.ReactNode
47+
footnote?: FootnoteProps<TData>['footnote']
4748
}
4849

4950
export function DataTable<TData extends RowData, TValue>({
@@ -55,7 +56,7 @@ export function DataTable<TData extends RowData, TValue>({
5556
data,
5657
defaultPageSize = 100,
5758
showSQL = true,
58-
footer,
59+
footnote,
5960
}: DataTableProps<TData>) {
6061
// Columns available in the data, normalized
6162
const allColumns: string[] = uniq(
@@ -185,16 +186,7 @@ export function DataTable<TData extends RowData, TValue>({
185186
</div>
186187

187188
<div className="flex items-center justify-between px-2">
188-
<div className="flex-1 text-sm text-muted-foreground">
189-
{footer ? (
190-
footer
191-
) : (
192-
<>
193-
{table.getFilteredSelectedRowModel().rows.length} of{' '}
194-
{table.getFilteredRowModel().rows.length} row(s) selected.
195-
</>
196-
)}
197-
</div>
189+
<Footnote table={table} footnote={footnote} />
198190
<DataTablePagination table={table} />
199191
</div>
200192
</div>

components/data-table/footnote.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Table } from '@tanstack/react-table'
2+
3+
export interface FootnoteProps<TData> {
4+
table: Table<TData>
5+
footnote?: string | React.ReactNode
6+
}
7+
8+
export function Footnote<TData>({ table, footnote }: FootnoteProps<TData>) {
9+
return (
10+
<div className="flex-1 text-sm text-muted-foreground">
11+
{footnote ? (
12+
footnote
13+
) : (
14+
<>
15+
{table.getFilteredSelectedRowModel().rows.length} of{' '}
16+
{table.getFilteredRowModel().rows.length} row(s) selected.
17+
</>
18+
)}
19+
</div>
20+
)
21+
}

components/data-table/pagination.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
DoubleArrowLeftIcon,
55
DoubleArrowRightIcon,
66
} from '@radix-ui/react-icons'
7-
import { Table } from '@tanstack/react-table'
7+
import type { Table } from '@tanstack/react-table'
88

99
import { Button } from '@/components/ui/button'
1010
import {

0 commit comments

Comments
 (0)