-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
88 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import { Table, TableProps, Tbody, Td, Th, Thead, ThProps, Tr } from '@patternfly/react-table'; | ||
|
||
export interface DataViewTableColumn extends Omit<ThProps, 'label'> { | ||
key: string, | ||
label: React.ReactNode, | ||
} | ||
|
||
export interface DataViewTableProps extends Omit<TableProps, 'onSelect'> { | ||
/** Columns definition in key, label format */ | ||
columns: DataViewTableColumn[]; | ||
/** Current page data */ | ||
pageData: Record<string, any>[]; | ||
/** Callback when a row is selected or deselected */ | ||
onSelect: (isSelecting: boolean, dataItem: Record<string, any>) => void; | ||
/** Function to determine if a row is selected */ | ||
isSelected: (dataItem: Record<string, any>) => boolean; | ||
/** Custom OUIA ID */ | ||
ouiaId?: string; | ||
} | ||
|
||
export const DataViewTable: React.FC<DataViewTableProps> = ({ | ||
columns, | ||
pageData, | ||
ouiaId = "DataViewTable", | ||
onSelect, | ||
isSelected, | ||
...props | ||
}: DataViewTableProps) => ( | ||
<Table aria-label="Data table" ouiaId={ouiaId} {...props}> | ||
<Thead data-ouia-component-id={`${ouiaId}-thead`}> | ||
<Tr ouiaId={`${ouiaId}-tr-head`}> | ||
<Th key="row-select" /> | ||
{columns.map(({ key, label, ...rest }) => ( | ||
<Th key={key} data-ouia-component-id={`${ouiaId}-th-${key}`} {...rest}> | ||
{label} | ||
</Th> | ||
))} | ||
</Tr> | ||
</Thead> | ||
<Tbody> | ||
{pageData.map((item, rowIndex) => ( | ||
<Tr key={item.name} ouiaId={`${ouiaId}-tr-${rowIndex}`}> | ||
{onSelect && ( | ||
<Td | ||
key={`select-${rowIndex}`} | ||
select={{ | ||
rowIndex, | ||
onSelect: (_event, isSelecting) => onSelect(isSelecting, item), | ||
isSelected: isSelected(item), | ||
isDisabled: false | ||
}} | ||
/>) | ||
} | ||
{/* How to modify rendered cell content? Pass node or mapping function? */} | ||
{columns.map((column, colIndex) => ( | ||
<Td | ||
key={column.key} | ||
data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`} | ||
> | ||
{item[column.key]} | ||
</Td> | ||
))} | ||
</Tr> | ||
))} | ||
</Tbody> | ||
</Table> | ||
) | ||
|
||
export default DataViewTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './DataViewTable'; | ||
export * from './DataViewTable'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters