-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented general Table component implemented table component eslint fixes removed as string usage delete unused projectTable comp emptyTableView updated emptytableview update fixed tbody in row components reverted BYON table changes fixed sorting and pagination fix expandable rows lint fix fix env
- Loading branch information
1 parent
0b996c1
commit d7e9bf7
Showing
14 changed files
with
393 additions
and
482 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { Pagination, Toolbar, ToolbarContent, ToolbarItem } from '@patternfly/react-core'; | ||
import { | ||
TableComposable, | ||
Thead, | ||
Tr, | ||
Th, | ||
TableComposableProps, | ||
Caption, | ||
Td, | ||
} from '@patternfly/react-table'; | ||
import React, { useEffect } from 'react'; | ||
import useTableColumnSort, { SortableData } from '~/utilities/useTableColumnSort'; | ||
|
||
type TableProps<DataType> = { | ||
data: DataType[]; | ||
columns: SortableData<DataType>[]; | ||
rowRenderer: (data: DataType) => React.ReactNode; | ||
enablePagination?: boolean; | ||
minPageSize?: number; | ||
toolbarContent?: React.ReactElement<typeof ToolbarItem>; | ||
emptyTableView?: React.ReactElement<typeof Tr>; | ||
caption?: string; | ||
} & Omit<TableComposableProps, 'ref' | 'data'>; | ||
|
||
const Table = <T,>({ | ||
data: allData, | ||
columns, | ||
rowRenderer, | ||
enablePagination, | ||
minPageSize = 10, | ||
toolbarContent, | ||
emptyTableView, | ||
caption, | ||
...props | ||
}: TableProps<T>): React.ReactElement => { | ||
const [page, setPage] = React.useState(1); | ||
const [pageSize, setPageSize] = React.useState(minPageSize); | ||
|
||
const data = enablePagination ? allData.slice(pageSize * (page - 1), pageSize * page) : allData; | ||
|
||
// update page to 1 if data changes (common when filter is applied) | ||
useEffect(() => setPage(1), [data]); | ||
|
||
const sort = useTableColumnSort<T>(columns, 0); | ||
|
||
const showPagination = enablePagination && data.length > minPageSize; | ||
const pagination = (pageDirection: 'up' | 'down') => ( | ||
<Pagination | ||
dropDirection={pageDirection} | ||
perPageComponent="button" | ||
itemCount={data.length} | ||
perPage={pageSize} | ||
page={page} | ||
onSetPage={(e, newPage) => setPage(newPage)} | ||
onPerPageSelect={(e, newSize, newPage) => { | ||
setPageSize(newSize); | ||
setPage(newPage); | ||
}} | ||
widgetId="table-pagination" | ||
/> | ||
); | ||
|
||
return ( | ||
<> | ||
{(toolbarContent || showPagination) && ( | ||
<Toolbar> | ||
<ToolbarContent> | ||
{toolbarContent} | ||
{showPagination && ( | ||
<ToolbarItem variant="pagination" alignment={{ default: 'alignRight' }}> | ||
{pagination('down')} | ||
</ToolbarItem> | ||
)} | ||
</ToolbarContent> | ||
</Toolbar> | ||
)} | ||
<TableComposable {...props}> | ||
{caption && <Caption>{caption}</Caption>} | ||
<Thead> | ||
<Tr> | ||
{columns.map((col, i) => ( | ||
<Th | ||
key={col.field + i} | ||
sort={col.sortable ? sort.getColumnSort(i) : undefined} | ||
width={col.width} | ||
> | ||
{col.label} | ||
</Th> | ||
))} | ||
</Tr> | ||
</Thead> | ||
<> | ||
{emptyTableView && data.length === 0 && ( | ||
<Tr> | ||
<Td colSpan={columns.length} style={{ textAlign: 'center' }}> | ||
{emptyTableView} | ||
</Td> | ||
</Tr> | ||
)} | ||
{sort.transformData(data).map((row) => rowRenderer(row))} | ||
</> | ||
</TableComposable> | ||
{showPagination && ( | ||
<Toolbar> | ||
<ToolbarContent> | ||
<ToolbarItem variant="pagination" alignment={{ default: 'alignRight' }}> | ||
{pagination('up')} | ||
</ToolbarItem> | ||
</ToolbarContent> | ||
</Toolbar> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default Table; |
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
Oops, something went wrong.