Skip to content

Commit

Permalink
Fix url property for cell links
Browse files Browse the repository at this point in the history
I think this has been broken forever, sadly. This simplifies the
`TableColumnResult` type so that TypeScript can now properly warn if you
specify an unknown value (`href`). We lose it knowing that `action` is
required for `params`, but the `href` thing is more confusing and
important to warn about I think.
  • Loading branch information
jacobmischka committed Sep 14, 2022
1 parent 11294fe commit 0f1cb48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
22 changes: 9 additions & 13 deletions src/examples/basic/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { IntervalActionHandler } from '../..'
import { faker } from '@faker-js/faker'

function generateRows(count: number) {
const rows: { [key: string]: string | number | boolean | Date }[] = []

for (let i = 0; i < count; i++) {
rows.push({
return Array(count)
.fill(null)
.map((_, i) => ({
id: i,
email: faker.internet.email(),
description: faker.helpers.arrayElement([
Expand All @@ -17,10 +16,7 @@ function generateRows(count: number) {
number: faker.datatype.number(100),
boolean: faker.datatype.boolean(),
date: faker.datatype.datetime(),
})
}

return rows
}))
}

export const no_pagination: IntervalActionHandler = async io => {
Expand Down Expand Up @@ -71,7 +67,7 @@ export const display_table: IntervalActionHandler = async io => {
},
{
label: 'Link',
renderCell: row => ({ href: '#', label: row.email }),
renderCell: row => ({ url: '#', label: row.email }),
},
],
rowMenuItems: row => [
Expand Down Expand Up @@ -105,7 +101,7 @@ export const select_table: IntervalActionHandler = async io => {
},
{
label: 'Link',
renderCell: row => ({ href: '#', label: row.email }),
renderCell: row => ({ url: '#', label: row.email }),
},
],
minSelections: 1,
Expand All @@ -121,17 +117,17 @@ export const select_table: IntervalActionHandler = async io => {
await io.display.table('Display users', {
data: selected,
columns: [
'string',
'description',
'number',
'boolean',
'date',
{
label: 'renderCell',
renderCell: row => `${row.string} ${row.number}`,
renderCell: row => `${row.description} ${row.number}`,
},
{
label: 'Edit',
renderCell: row => ({ href: '#', label: row.string }),
renderCell: row => ({ url: '#', label: row.email }),
},
],
})
Expand Down
11 changes: 5 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,13 @@ export type GroupConfig = {
export type TableCellValue = string | number | boolean | null | Date | undefined

export type TableColumnResult =
| ({
| {
label: string | number | boolean | null | Date | undefined
value?: TableCellValue
} & (
| { url: string }
| { action: string; params?: z.infer<typeof serializableRecord> }
| {}
))
url?: string
action?: string
params?: z.infer<typeof serializableRecord>
}
| TableCellValue

export interface TableColumn<Row> extends z.input<typeof tableColumn> {
Expand Down

0 comments on commit 0f1cb48

Please sign in to comment.