@@ -17,9 +17,9 @@ export interface DataViewTableBasicProps extends Omit<TableProps, 'onSelect' | '
1717 /** Current page rows */
1818 rows : DataViewTr [ ] ;
1919 /** Table head states to be displayed when active */
20- headStates ?: Partial < Record < DataViewState , React . ReactNode > >
20+ headStates ?: Partial < Record < DataViewState | string , React . ReactNode > >
2121 /** Table body states to be displayed when active */
22- bodyStates ?: Partial < Record < DataViewState , React . ReactNode > >
22+ bodyStates ?: Partial < Record < DataViewState | string , React . ReactNode > >
2323 /** Custom OUIA ID */
2424 ouiaId ?: string ;
2525}
@@ -28,55 +28,53 @@ export const DataViewTableBasic: React.FC<DataViewTableBasicProps> = ({
2828 columns,
2929 rows,
3030 ouiaId = 'DataViewTableBasic' ,
31- headStates = { } ,
32- bodyStates = { } ,
31+ headStates,
32+ bodyStates,
3333 ...props
3434} : DataViewTableBasicProps ) => {
3535 const { selection, activeState, isSelectable } = useInternalContext ( ) ;
3636 const { onSelect, isSelected, isSelectDisabled } = selection ?? { } ;
3737
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 ] ) ;
4073
4174 return (
4275 < Table aria-label = "Data table" ouiaId = { ouiaId } { ...props } >
4376 { 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 > }
8078 </ Table >
8179 ) ;
8280} ;
0 commit comments