Skip to content

Commit fa375d7

Browse files
doublefacedoubleface
authored andcommitted
feat: Allow to hide checkboxes in VirtualizedTable
With `withCheckbox={false}` attribute added to VirtualizedTable, checkboxes are hidden and rows become clickable for selection.
1 parent 0ceedc7 commit fa375d7

File tree

5 files changed

+49
-30
lines changed

5 files changed

+49
-30
lines changed

react/Table/Readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const columns = [
7373
}
7474
]
7575

76-
const initialVariants = [{ grouped: false }]
76+
const initialVariants = [{ grouped: false, withCheckbox: true }, { grouped: false, withCheckbox: false }]
7777

7878
// Very basic usage only works when Dessert is sorted "asc"
7979
// Ideally you have to create a logic to create groups with sorted data
@@ -98,6 +98,7 @@ const ExampleTable = ({ variant, ...props }) => {
9898
onSelect={(row, event, index) => toggleSelectedItem(row.id)}
9999
onSelectAll={rows => toggleSelectAllItems(rows.map(item => item.id))}
100100
onSortChange={onSortChange}
101+
withCheckbox={variant.withCheckbox}
101102
componentsProps={{
102103
rowContent: {
103104
onClick: (row, column) => { console.info(`click on cell. Row ${row['id']}, Column ${column['id']}`) },

react/Table/Virtualized/FixedHeaderContent.jsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ const FixedHeaderContent = ({
3535

3636
return (
3737
<TableRow>
38-
<TableCell align="center" padding="checkbox">
39-
<Checkbox
40-
className={cx('virtualizedCheckbox', {
41-
checked: selectedCount > 0
42-
})}
43-
indeterminate={selectedCount > 0 && selectedCount < rowCount}
44-
checked={rowCount > 0 && selectedCount === rowCount}
45-
inputProps={{ 'aria-label': 'select all' }}
46-
size="small"
47-
onChange={onSelectAllClick}
48-
/>
49-
</TableCell>
38+
{context.withCheckbox && (
39+
<TableCell align="center" padding="checkbox">
40+
<Checkbox
41+
className={cx('virtualizedCheckbox', {
42+
checked: selectedCount > 0
43+
})}
44+
indeterminate={selectedCount > 0 && selectedCount < rowCount}
45+
checked={rowCount > 0 && selectedCount === rowCount}
46+
inputProps={{ 'aria-label': 'select all' }}
47+
size="small"
48+
onChange={onSelectAllClick}
49+
/>
50+
</TableCell>
51+
)}
5052
{columns.map(column => (
5153
<TableHeadCell
5254
key={column.id}

react/Table/Virtualized/RowContent.jsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ const RowContent = ({
1919

2020
return (
2121
<>
22-
<TableCell align="center" padding="checkbox">
23-
<Checkbox
24-
className={cx('virtualizedCheckbox', {
25-
visible: selectedCount > 0,
26-
checked: context.isSelectedItem(row)
27-
})}
28-
checked={context.isSelectedItem(row)}
29-
inputProps={{
30-
'aria-labelledby': `enhanced-table-checkbox-${index}`
31-
}}
32-
size="small"
33-
onClick={event => onSelectClick(row, event, index)}
34-
/>
35-
</TableCell>
22+
{context.withCheckbox && (
23+
<TableCell align="center" padding="checkbox">
24+
<Checkbox
25+
className={cx('virtualizedCheckbox', {
26+
visible: selectedCount > 0,
27+
checked: context.isSelectedItem(row)
28+
})}
29+
checked={context.isSelectedItem(row)}
30+
inputProps={{
31+
'aria-labelledby': `enhanced-table-checkbox-${index}`
32+
}}
33+
size="small"
34+
onClick={event => onSelectClick(row, event, index)}
35+
/>
36+
</TableCell>
37+
)}
3638
{columns.map(column => (
3739
<Cell
3840
key={column.id}

react/Table/Virtualized/TableRow.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ const TableRow = ({ item, context, className, ...props }) => {
77
const _item = item || context.data[props['data-item-index']]
88
const isSelected = context.isSelectedItem(_item)
99

10+
const handleClick = () => {
11+
if (!context.withCheckbox && _item) {
12+
const index = props['data-item-index']
13+
context.onSelect?.(_item, null, index)
14+
}
15+
}
16+
1017
return (
1118
<TableRowClassic
1219
{...props}
1320
className={cx(className, 'virtualized')}
1421
selected={isSelected}
1522
hover
23+
onClick={!context.withCheckbox ? handleClick : props.onClick}
1624
/>
1725
)
1826
}

react/Table/Virtualized/index.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const VirtualizedTable = forwardRef(
2525
components,
2626
onSortChange,
2727
isNewItem,
28+
withCheckbox,
2829
...props
2930
},
3031
ref
@@ -45,7 +46,9 @@ const VirtualizedTable = forwardRef(
4546
...(isGroupedTable && { data }), // we use directly `data` prop if no groupCounts
4647
isSelectedItem,
4748
selectedItems,
48-
isNewItem
49+
isNewItem,
50+
withCheckbox,
51+
onSelect
4952
}
5053

5154
const handleSort = property => {
@@ -117,7 +120,8 @@ VirtualizedTable.defaultProps = {
117120
selectedItems: [],
118121
isSelectedItem: () => {},
119122
onSelect: () => {},
120-
onSelectAll: () => {}
123+
onSelectAll: () => {},
124+
withCheckbox: true
121125
}
122126

123127
VirtualizedTable.propTypes = {
@@ -145,7 +149,9 @@ VirtualizedTable.propTypes = {
145149
/** Callback called after the sort */
146150
onSortChange: PropTypes.func,
147151
/** Function to determine if a row is new */
148-
isNewItem: PropTypes.func
152+
isNewItem: PropTypes.func,
153+
/** Whether to show checkboxes. When false, rows become clickable for selection */
154+
withCheckbox: PropTypes.bool
149155
}
150156

151157
export default VirtualizedTable

0 commit comments

Comments
 (0)