@@ -17,9 +17,9 @@ export interface DataViewTableBasicProps extends Omit<TableProps, 'onSelect' | '
17
17
/** Current page rows */
18
18
rows : DataViewTr [ ] ;
19
19
/** Table head states to be displayed when active */
20
- headStates ?: Partial < Record < DataViewState , React . ReactNode > >
20
+ headStates ?: Partial < Record < DataViewState | string , React . ReactNode > >
21
21
/** Table body states to be displayed when active */
22
- bodyStates ?: Partial < Record < DataViewState , React . ReactNode > >
22
+ bodyStates ?: Partial < Record < DataViewState | string , React . ReactNode > >
23
23
/** Custom OUIA ID */
24
24
ouiaId ?: string ;
25
25
}
@@ -28,55 +28,53 @@ export const DataViewTableBasic: React.FC<DataViewTableBasicProps> = ({
28
28
columns,
29
29
rows,
30
30
ouiaId = 'DataViewTableBasic' ,
31
- headStates = { } ,
32
- bodyStates = { } ,
31
+ headStates,
32
+ bodyStates,
33
33
...props
34
34
} : DataViewTableBasicProps ) => {
35
35
const { selection, activeState, isSelectable } = useInternalContext ( ) ;
36
36
const { onSelect, isSelected, isSelectDisabled } = selection ?? { } ;
37
37
38
- const activeHeadState = useMemo ( ( ) => activeState ? headStates [ activeState ] : undefined , [ activeState , headStates ] ) ;
39
- const activeBodyState = useMemo ( ( ) => activeState ? bodyStates [ activeState ] : undefined , [ activeState , bodyStates ] ) ;
38
+ const activeHeadState = useMemo ( ( ) => activeState ? headStates ?. [ activeState ] : undefined , [ activeState , headStates ] ) ;
39
+ const activeBodyState = useMemo ( ( ) => activeState ? bodyStates ?. [ activeState ] : undefined , [ activeState , bodyStates ] ) ;
40
+
41
+ const renderedRows = useMemo ( ( ) => rows . map ( ( row , rowIndex ) => {
42
+ const rowIsObject = isDataViewTrObject ( row ) ;
43
+ return (
44
+ < Tr key = { rowIndex } ouiaId = { `${ ouiaId } -tr-${ rowIndex } ` } { ...( rowIsObject && row ?. props ) } >
45
+ { isSelectable && (
46
+ < Td
47
+ key = { `select-${ rowIndex } ` }
48
+ select = { {
49
+ rowIndex,
50
+ onSelect : ( _event , isSelecting ) => {
51
+ onSelect ?.( isSelecting , rowIsObject ? row : [ row ] ) ;
52
+ } ,
53
+ isSelected : isSelected ?.( row ) || false ,
54
+ isDisabled : isSelectDisabled ?.( row ) || false ,
55
+ } }
56
+ />
57
+ ) }
58
+ { ( rowIsObject ? row . row : row ) . map ( ( cell , colIndex ) => {
59
+ const cellIsObject = isDataViewTdObject ( cell ) ;
60
+ return (
61
+ < Td
62
+ key = { colIndex }
63
+ { ...( cellIsObject && ( cell ?. props ?? { } ) ) }
64
+ data-ouia-component-id = { `${ ouiaId } -td-${ rowIndex } -${ colIndex } ` }
65
+ >
66
+ { cellIsObject ? cell . cell : cell }
67
+ </ Td >
68
+ ) ;
69
+ } ) }
70
+ </ Tr >
71
+ ) ;
72
+ } ) , [ rows , isSelectable , isSelected , isSelectDisabled , onSelect , ouiaId ] ) ;
40
73
41
74
return (
42
75
< Table aria-label = "Data table" ouiaId = { ouiaId } { ...props } >
43
76
{ activeHeadState || < DataViewTableHead columns = { columns } ouiaId = { ouiaId } /> }
44
- { activeBodyState || (
45
- < Tbody >
46
- { rows . map ( ( row , rowIndex ) => {
47
- const rowIsObject = isDataViewTrObject ( row ) ;
48
- return (
49
- < Tr key = { rowIndex } ouiaId = { `${ ouiaId } -tr-${ rowIndex } ` } { ...( rowIsObject && row ?. props ) } >
50
- { isSelectable && (
51
- < Td
52
- key = { `select-${ rowIndex } ` }
53
- select = { {
54
- rowIndex,
55
- onSelect : ( _event , isSelecting ) => {
56
- onSelect ?.( isSelecting , rowIsObject ? row : [ row ] ) ;
57
- } ,
58
- isSelected : isSelected ?.( row ) || false ,
59
- isDisabled : isSelectDisabled ?.( row ) || false ,
60
- } }
61
- />
62
- ) }
63
- { ( rowIsObject ? row . row : row ) . map ( ( cell , colIndex ) => {
64
- const cellIsObject = isDataViewTdObject ( cell ) ;
65
- return (
66
- < Td
67
- key = { colIndex }
68
- { ...( cellIsObject && ( cell ?. props ?? { } ) ) }
69
- data-ouia-component-id = { `${ ouiaId } -td-${ rowIndex } -${ colIndex } ` }
70
- >
71
- { cellIsObject ? cell . cell : cell }
72
- </ Td >
73
- ) ;
74
- } ) }
75
- </ Tr >
76
- ) ;
77
- } ) }
78
- </ Tbody >
79
- ) }
77
+ { activeBodyState || < Tbody > { renderedRows } </ Tbody > }
80
78
</ Table >
81
79
) ;
82
80
} ;
0 commit comments