-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from BudgetBuddyDE/dev
dev
- Loading branch information
Showing
25 changed files
with
781 additions
and
658 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,33 @@ | ||
import React from 'react'; | ||
import { | ||
Checkbox as MuiCheckbox, | ||
type CheckboxProps, | ||
TableCell, | ||
type TableCellProps, | ||
} from '@mui/material'; | ||
import { AppConfig } from '@/app.config'; | ||
|
||
export type TSelectAllProps = CheckboxProps & | ||
( | ||
| { wrapWithTableCell: true; tableCellProps?: TableCellProps } | ||
| { wrapWithTableCell?: false; tableCellProps?: undefined } | ||
); | ||
|
||
export const SelectAll: React.FC<TSelectAllProps> = ({ | ||
wrapWithTableCell, | ||
tableCellProps, | ||
...checkboxProps | ||
}) => { | ||
const Checkbox = () => <MuiCheckbox {...checkboxProps} />; | ||
return wrapWithTableCell ? ( | ||
<TableCell | ||
size={AppConfig.table.cellSize} | ||
{...tableCellProps} | ||
sx={{ width: '1%', whiteSpace: 'nowrap', ...tableCellProps?.sx }} | ||
> | ||
<Checkbox /> | ||
</TableCell> | ||
) : ( | ||
<Checkbox /> | ||
); | ||
}; |
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,8 @@ | ||
export * from './SelectAll.component'; | ||
|
||
export interface ISelectionHandler<T> { | ||
onSelectAll: (shouldSelectAll: boolean) => void; | ||
onSelect: (entity: T) => void; | ||
isSelected: (entity: T) => boolean; | ||
onDeleteMultiple?: () => void; | ||
} |
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 |
---|---|---|
@@ -1,22 +1,175 @@ | ||
import React from 'react'; | ||
import { | ||
TableContainer, | ||
type TableContainerProps, | ||
Table as MuiTable, | ||
type TableProps as MuiTableProps, | ||
Box, | ||
Button, | ||
Skeleton, | ||
TableBody, | ||
TableCell, | ||
TableHead, | ||
TableRow, | ||
Typography, | ||
} from '@mui/material'; | ||
import { ActionPaper, Card, NoResults } from '../index'; | ||
import { | ||
type IPaginationHandler, | ||
InitialPaginationState, | ||
Pagination, | ||
usePagination, | ||
PaginationReducer, | ||
} from '../Pagination'; | ||
import { TableContainer } from './TableContainer.component'; | ||
import { CircularProgress } from '@/components/Loading'; | ||
import { type ISelectionHandler, SelectAll } from '../Select'; | ||
import { DeleteRounded } from '@mui/icons-material'; | ||
|
||
export type TTableProps<T> = { | ||
title?: string; | ||
subtitle?: string; | ||
headerCells: string[]; | ||
renderHeaderCell?: (headerCell: string) => React.ReactNode; | ||
data: T[]; | ||
renderRow: (row: T) => React.ReactNode; | ||
tableActions?: React.ReactNode; | ||
isLoading?: boolean; | ||
} & ( | ||
| { | ||
withSelection: true; | ||
onSelectAll: ISelectionHandler<T>['onSelectAll']; | ||
onDelete?: () => void; | ||
amountOfSelectedEntities: number; | ||
} | ||
| { withSelection?: false } | ||
); | ||
|
||
interface ITableHandler { | ||
pagination: IPaginationHandler; | ||
} | ||
|
||
export const Table = <T,>({ | ||
title, | ||
subtitle, | ||
headerCells, | ||
renderHeaderCell, | ||
data, | ||
renderRow, | ||
tableActions, | ||
isLoading = false, | ||
...props | ||
}: TTableProps<T>) => { | ||
const [state, dispatch] = React.useReducer(PaginationReducer, InitialPaginationState); | ||
const [checked, setChecked] = React.useState(false); | ||
const currentPageData = usePagination(data, state); | ||
|
||
export type TTable = React.PropsWithChildren<{ | ||
tableContainerProps?: TableContainerProps; | ||
tableProps?: MuiTableProps; | ||
}>; | ||
const handler: ITableHandler = { | ||
pagination: { | ||
onPageChange(newPage) { | ||
dispatch({ type: 'CHANGE_PAGE', page: newPage }); | ||
}, | ||
onRowsPerPageChange(rowsPerPage) { | ||
dispatch({ type: 'CHANGE_ROWS_PER_PAGE', rowsPerPage: rowsPerPage }); | ||
}, | ||
}, | ||
}; | ||
|
||
React.useLayoutEffect(() => { | ||
setChecked(false); | ||
if (!props.withSelection) return; | ||
if (props.amountOfSelectedEntities === data.length) { | ||
setChecked(true); | ||
} | ||
}, [props, data]); | ||
|
||
export const Table: React.FC<TTable> = ({ tableProps, tableContainerProps, children }) => { | ||
return ( | ||
<TableContainer {...tableContainerProps}> | ||
<MuiTable aria-label="Table" {...tableProps} sx={{ minWidth: 650, ...tableProps?.sx }}> | ||
{children} | ||
</MuiTable> | ||
</TableContainer> | ||
<Card sx={{ p: 0 }}> | ||
{(title || subtitle) && ( | ||
<Card.Header sx={{ px: 2, pt: 2 }}> | ||
<Box> | ||
{title && <Card.Title>{title || 'Table'}</Card.Title>} | ||
{subtitle && <Card.Subtitle>{subtitle}</Card.Subtitle>} | ||
</Box> | ||
{tableActions && ( | ||
<Card.HeaderActions sx={{ mt: { xs: 1, md: 0 }, width: { md: 'unset' } }}> | ||
<ActionPaper sx={{ display: 'flex', flexDirection: 'row' }}> | ||
{isLoading ? ( | ||
<Skeleton | ||
variant="rounded" | ||
sx={{ width: { xs: '5rem', md: '10rem' }, height: '2.3rem' }} | ||
/> | ||
) : ( | ||
tableActions | ||
)} | ||
</ActionPaper> | ||
</Card.HeaderActions> | ||
)} | ||
</Card.Header> | ||
)} | ||
<Card.Body sx={{ px: 0 }}> | ||
{isLoading ? ( | ||
<CircularProgress /> | ||
) : currentPageData.length > 0 ? ( | ||
<React.Fragment> | ||
{props.withSelection && props.amountOfSelectedEntities > 0 && ( | ||
<Box sx={{ px: 2, pt: 1.5 }}> | ||
{props.onDelete && ( | ||
<Button | ||
startIcon={<DeleteRounded />} | ||
onClick={() => { | ||
if (props.onDelete) props.onDelete(); | ||
}} | ||
sx={{ mr: 1 }} | ||
> | ||
Delete | ||
</Button> | ||
)} | ||
</Box> | ||
)} | ||
|
||
<TableContainer> | ||
<TableHead> | ||
<TableRow> | ||
{props.withSelection && ( | ||
<SelectAll | ||
checked={checked} | ||
indeterminate={ | ||
props.amountOfSelectedEntities > 0 && | ||
props.amountOfSelectedEntities < data.length | ||
} | ||
onChange={(_event, checked) => { | ||
const indeterminate = | ||
props.amountOfSelectedEntities > 0 && | ||
props.amountOfSelectedEntities < data.length; | ||
props.onSelectAll(indeterminate ? false : checked); | ||
}} | ||
wrapWithTableCell | ||
/> | ||
)} | ||
|
||
{headerCells.map((headerCell) => | ||
renderHeaderCell ? ( | ||
renderHeaderCell(headerCell) | ||
) : ( | ||
<TableCell key={headerCell.replaceAll(' ', '_').toLowerCase()}> | ||
<Typography fontWeight={'bold'}>{headerCell}</Typography> | ||
</TableCell> | ||
) | ||
)} | ||
</TableRow> | ||
</TableHead> | ||
<TableBody>{currentPageData.map(renderRow)}</TableBody> | ||
</TableContainer> | ||
</React.Fragment> | ||
) : ( | ||
<NoResults sx={{ mx: 2, mt: 2 }} /> | ||
)} | ||
</Card.Body> | ||
<Card.Footer sx={{ p: 2 }}> | ||
<Pagination | ||
{...state} | ||
count={currentPageData.length} | ||
onPageChange={handler.pagination.onPageChange} | ||
onRowsPerPageChange={handler.pagination.onRowsPerPageChange} | ||
/> | ||
</Card.Footer> | ||
</Card> | ||
); | ||
}; |
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,26 @@ | ||
import React from 'react'; | ||
import { | ||
TableContainer as MuiTableContainer, | ||
type TableContainerProps, | ||
Table as MuiTable, | ||
type TableProps as MuiTableProps, | ||
} from '@mui/material'; | ||
|
||
export type TTableContainerProps = React.PropsWithChildren<{ | ||
tableContainerProps?: TableContainerProps; | ||
tableProps?: MuiTableProps; | ||
}>; | ||
|
||
export const TableContainer: React.FC<TTableContainerProps> = ({ | ||
tableProps, | ||
tableContainerProps, | ||
children, | ||
}) => { | ||
return ( | ||
<MuiTableContainer {...tableContainerProps}> | ||
<MuiTable aria-label="Table" {...tableProps} sx={{ width: '100%', ...tableProps?.sx }}> | ||
{children} | ||
</MuiTable> | ||
</MuiTableContainer> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './TableContainer.component'; | ||
export * from './Table.component'; |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.