Skip to content

Commit e3119eb

Browse files
author
Amrit Borah
committed
feat: expose expand row to CellComponent
1 parent df03df5 commit e3119eb

4 files changed

Lines changed: 55 additions & 22 deletions

File tree

src/Shared/Components/Table/InternalTable.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const InternalTable = <
5757
pageSizeOptions,
5858
clearFilters: userGivenUrlClearFilters,
5959
rowStartIconConfig,
60+
onRowClick,
6061
}: InternalTableProps<RowData, FilterVariant, AdditionalProps>) => {
6162
const {
6263
sortBy,
@@ -222,6 +223,7 @@ const InternalTable = <
222223
getRows={getRows}
223224
totalRows={totalRows}
224225
rowStartIconConfig={rowStartIconConfig}
226+
onRowClick={onRowClick}
225227
/>
226228
</UseRegisterShortcutProvider>
227229
)

src/Shared/Components/Table/TableContent.tsx

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useEffect, useMemo, useRef, useState } from 'react'
17+
import { MouseEvent, useEffect, useMemo, useRef, useState } from 'react'
1818

1919
import { Checkbox } from '@Common/Checkbox'
2020
import { DEFAULT_BASE_PAGE_SIZE } from '@Common/Constants'
@@ -29,7 +29,15 @@ import { ComponentSizeType } from '@Shared/constants'
2929
import { BulkSelection } from '../BulkSelection'
3030
import BulkSelectionActionWidget from './BulkSelectionActionWidget'
3131
import { ACTION_GUTTER_SIZE, BULK_ACTION_GUTTER_LABEL, EVENT_TARGET, SHIMMER_DUMMY_ARRAY } from './constants'
32-
import { BulkActionStateType, FiltersTypeEnum, PaginationEnum, RowType, SignalsType, TableContentProps } from './types'
32+
import {
33+
BulkActionStateType,
34+
ExpandedRowPrefixType,
35+
FiltersTypeEnum,
36+
PaginationEnum,
37+
RowType,
38+
SignalsType,
39+
TableContentProps,
40+
} from './types'
3341
import useTableWithKeyboardShortcuts from './useTableWithKeyboardShortcuts'
3442
import { getStickyColumnConfig, scrollToShowActiveElementIfNeeded } from './utils'
3543

@@ -57,6 +65,7 @@ const TableContent = <
5765
getRows,
5866
totalRows,
5967
rowStartIconConfig,
68+
onRowClick,
6069
}: TableContentProps<RowData, FilterVariant, AdditionalProps>) => {
6170
const rowsContainerRef = useRef<HTMLDivElement>(null)
6271
const parentRef = useRef<HTMLDivElement>(null)
@@ -194,7 +203,9 @@ const TableContent = <
194203
handleSorting(newSortBy)
195204
}
196205

197-
const toggleExpandAll = () => {
206+
const toggleExpandAll = (e: MouseEvent<HTMLButtonElement>) => {
207+
e.stopPropagation()
208+
198209
setExpandState(
199210
visibleRows.reduce((acc, row) => {
200211
if ((row as RowType<RowData>).expandableRows) {
@@ -272,21 +283,29 @@ const TableContent = <
272283
return visibleRows.map((row, visibleRowIndex) => {
273284
const isRowActive = activeRowIndex === visibleRowIndex
274285
const isRowBulkSelected = !!bulkSelectionState[row.id] || isBulkSelectionApplied
275-
const isExpandedRow = row.id.startsWith('expanded-row')
286+
const isExpandedRow = row.id.startsWith('expanded-row-' satisfies ExpandedRowPrefixType)
287+
288+
const handleChangeActiveRowIndex = (e: MouseEvent<HTMLDivElement>) => {
289+
e.stopPropagation()
276290

277-
const handleChangeActiveRowIndex = () => {
278291
setActiveRowIndex(visibleRowIndex)
292+
293+
onRowClick?.(row)
279294
}
280295

281296
const handleToggleBulkSelectionForRow = () => {
282297
handleToggleBulkSelectionOnRow(row)
283298
}
284299

285-
const toggleExpandRow = () => {
286-
setExpandState({
287-
...expandState,
288-
[row.id]: !expandState[row.id],
289-
})
300+
const toggleExpandRow = (e: MouseEvent<HTMLButtonElement>) => {
301+
e.stopPropagation()
302+
303+
if ((row as RowType<RowData>).expandableRows) {
304+
setExpandState({
305+
...expandState,
306+
[row.id]: !expandState[row.id],
307+
})
308+
}
290309
}
291310

292311
const hasBulkOrExpandAction =
@@ -306,7 +325,9 @@ const TableContent = <
306325
isRowBulkSelected ? 'generic-table__row--bulk-selected' : ''
307326
} ${isExpandedRow ? 'generic-table__row--expanded-row' : ''} ${
308327
rowStartIconConfig && hasBulkOrExpandAction ? 'with-start-icon-and-bulk-or-expand-action' : ''
309-
} ${!isExpandedRow && expandState[row.id] ? 'generic-table__row--is-expanded' : ''}`}
328+
} ${!isExpandedRow && expandState[row.id] ? 'generic-table__row--is-expanded' : ''} ${
329+
onRowClick ? 'pointer' : ''
330+
}`}
310331
style={{
311332
gridTemplateColumns,
312333
}}
@@ -332,7 +353,7 @@ const TableContent = <
332353
rotateBy={expandState[row.id] ? 90 : 0}
333354
/>
334355
}
335-
ariaLabel="Expand row"
356+
ariaLabel="Expand/Collapse row"
336357
showAriaLabelInTippy={false}
337358
variant={ButtonVariantType.borderLess}
338359
size={ComponentSizeType.xs}
@@ -345,7 +366,7 @@ const TableContent = <
345366
{/* empty div needed for alignment; therefore hide if rowStartIconConfig (only applies to parent rows) is present */}
346367
{isAnyRowExpandable &&
347368
(isExpandedRow || (!(row as RowType<RowData>).expandableRows && !rowStartIconConfig)) && (
348-
<div />
369+
<div className="dc__position-rel expanded-tree-line" />
349370
)}
350371

351372
{visibleColumns.map(({ field, horizontallySticky: isStickyColumn, CellComponent }, index) => {
@@ -388,6 +409,7 @@ const TableContent = <
388409
isRowActive={isRowActive}
389410
isExpandedRow={isExpandedRow}
390411
isRowInExpandState={expandState[row.id]}
412+
expandRowCallback={toggleExpandRow}
391413
{...additionalProps}
392414
/>
393415
) : (
@@ -453,7 +475,7 @@ const TableContent = <
453475
rotateBy={areAllRowsExpanded ? 90 : 0}
454476
/>
455477
}
456-
ariaLabel="Expand row"
478+
ariaLabel="Expand/Collapse all rows"
457479
showAriaLabelInTippy={false}
458480
variant={ButtonVariantType.borderLess}
459481
size={ComponentSizeType.xs}

src/Shared/Components/Table/styles.scss

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@
6969
outline: none;
7070
}
7171

72-
&--expanded-row,
73-
&--expanded-row > * {
74-
background-color: var(--N100);
75-
}
76-
7772
&:hover,
7873
&:hover > *,
7974
&--active,
@@ -138,7 +133,13 @@
138133
}
139134
}
140135

141-
&__expanse {
142-
background-color: var(--bg-hover-opaque);
136+
.expanded-tree-line::after {
137+
content: '';
138+
width: 1px;
139+
height: 100%;
140+
background: var(--N100);
141+
left: calc(50% - 1px); // offset to left by width for perfect centering
142+
top: 0;
143+
position: absolute;
143144
}
144145
}

src/Shared/Components/Table/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ type CommonRowType<Data extends unknown> = {
9595
data: Data
9696
}
9797

98+
export type ExpandedRowPrefixType = 'expanded-row-'
99+
98100
export type ExpandedRowType<Data extends unknown> = CommonRowType<Data> & {
99-
id: `expanded-row-${string}`
101+
id: `${ExpandedRowPrefixType}${string}`
100102
}
101103

102104
export type RowType<Data extends unknown> = CommonRowType<Data> & {
@@ -128,6 +130,8 @@ export type CellComponentProps<
128130
isRowActive: boolean
129131
isExpandedRow: boolean
130132
isRowInExpandState: boolean
133+
// NOTE: no action if the row is not expandable
134+
expandRowCallback: (e: MouseEvent<HTMLButtonElement>) => void
131135
}
132136

133137
export type RowActionsOnHoverComponentProps<
@@ -324,6 +328,8 @@ export type InternalTableProps<
324328
* until user hovers over the row or the row has focus from keyboard navigation
325329
*/
326330
rowStartIconConfig?: Omit<IconsProps, 'dataTestId'>
331+
332+
onRowClick?: (row: RowType<RowData>) => void
327333
} & (
328334
| {
329335
/**
@@ -408,6 +414,7 @@ export type TableProps<
408414
| 'pageSizeOptions'
409415
| 'clearFilters'
410416
| 'rowStartIconConfig'
417+
| 'onRowClick'
411418
>
412419

413420
export type BulkActionStateType = string | null
@@ -459,6 +466,7 @@ export interface TableContentProps<
459466
| 'pageSizeOptions'
460467
| 'getRows'
461468
| 'rowStartIconConfig'
469+
| 'onRowClick'
462470
>,
463471
RowsResultType<RowData> {
464472
areFilteredRowsLoading: boolean

0 commit comments

Comments
 (0)