Skip to content

Commit 68adee2

Browse files
committed
onSelect -> onSingleSelect, logic + comments around selection in Table
1 parent 45abafe commit 68adee2

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

libs/table/QueryTable.tsx

+17-9
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,23 @@ type QueryTableProps<Item> = {
5555
emptyState: React.ReactElement
5656
} & (
5757
| {
58-
onSelect: (selection: string) => void
58+
/**
59+
* If present, the table will include a select column and make rows
60+
* selectable one at a time.
61+
*/
62+
onSingleSelect: (selection: string) => void
5963
onMultiSelect?: never
6064
}
6165
| {
62-
onSelect?: never
66+
onSingleSelect?: never
67+
/**
68+
* If present, the table will include a select column and make rows
69+
* selectable.
70+
*/
6371
onMultiSelect: (selections: string[]) => void
6472
}
6573
| {
66-
onSelect?: never
74+
onSingleSelect?: never
6775
onMultiSelect?: never
6876
}
6977
)
@@ -81,7 +89,7 @@ const makeQueryTable = <Item,>(
8189
pagination = 'page',
8290
pageSize = 10,
8391
emptyState,
84-
onSelect,
92+
onSingleSelect,
8593
onMultiSelect,
8694
}: QueryTableProps<Item>) {
8795
invariant(
@@ -92,9 +100,9 @@ const makeQueryTable = <Item,>(
92100
const [rowSelection, setRowSelection] = React.useState({})
93101
useEffect(() => {
94102
const selected = Object.keys(rowSelection)
95-
onSelect?.(selected[0])
103+
onSingleSelect?.(selected[0])
96104
onMultiSelect?.(selected)
97-
}, [rowSelection, onSelect, onMultiSelect])
105+
}, [rowSelection, onSingleSelect, onMultiSelect])
98106

99107
const { currentPage, goToNextPage, goToPrevPage, hasPrev } = usePagination()
100108
const tableHelper = useMemo(() => createTable().setRowType<Item>(), [])
@@ -123,7 +131,7 @@ const makeQueryTable = <Item,>(
123131
)
124132
})
125133

126-
if (onSelect) {
134+
if (onSingleSelect) {
127135
columns.unshift(getSelectCol())
128136
} else if (onMultiSelect) {
129137
columns.unshift(getMultiSelectCol())
@@ -134,7 +142,7 @@ const makeQueryTable = <Item,>(
134142
}
135143

136144
return columns
137-
}, [children, tableHelper, makeActions, onSelect, onMultiSelect])
145+
}, [children, tableHelper, makeActions, onSingleSelect, onMultiSelect])
138146

139147
const { data, isLoading } = useApiQuery(
140148
query,
@@ -155,7 +163,7 @@ const makeQueryTable = <Item,>(
155163
rowSelection,
156164
},
157165
manualPagination: true,
158-
enableRowSelection: !!onSelect,
166+
enableRowSelection: !!onSingleSelect,
159167
enableMultiRowSelection: !!onMultiSelect,
160168
onRowSelectionChange: setRowSelection,
161169
})

libs/table/Table.tsx

+20-20
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,29 @@ export const Table = <TGenerics extends OurTableGenerics>({
4949
</UITable.Header>
5050
<UITable.Body>
5151
{table.getRowModel().rows.map((row) => {
52-
const onSingleSelect = row.getCanSelect()
53-
? () => {
54-
table.resetRowSelection()
55-
row.toggleSelected()
52+
// For single-select, the entire row is clickable
53+
const rowProps = row.getCanSelect() // this means single-select only
54+
? {
55+
className: cn(rowClassName, 'cursor-pointer'),
56+
selected: row.getIsSelected(),
57+
// select only this row
58+
onClick: () => table.setRowSelection(() => ({ [row.id]: true })),
5659
}
57-
: undefined
58-
const onMultiSelect = row.getCanMultiSelect()
59-
? () => row.toggleSelected()
60-
: undefined
60+
: { className: rowClassName }
61+
62+
// For multi-select, assume the first cell is the checkbox and make the
63+
// whole cell clickable
64+
const firstCellProps = row.getCanMultiSelect()
65+
? {
66+
className: 'cursor-pointer',
67+
onClick: () => row.toggleSelected(),
68+
}
69+
: {}
70+
6171
const [firstCell, ...cells] = row.getAllCells()
6272
return (
63-
<UITable.Row
64-
className={cn(rowClassName, { 'cursor-pointer': !!onSingleSelect })}
65-
selected={row.getIsSelected()}
66-
key={row.id}
67-
onClick={onSingleSelect}
68-
>
69-
<UITable.Cell
70-
onClick={onMultiSelect}
71-
className={cn({ 'cursor-pointer': !!onMultiSelect })}
72-
>
73-
{firstCell.renderCell()}
74-
</UITable.Cell>
73+
<UITable.Row key={row.id} {...rowProps}>
74+
<UITable.Cell {...firstCellProps}>{firstCell.renderCell()}</UITable.Cell>
7575
{cells.map((cell) => (
7676
<UITable.Cell key={cell.column.id}>{cell.renderCell()}</UITable.Cell>
7777
))}

0 commit comments

Comments
 (0)