Skip to content

Commit

Permalink
feat: add table tagination totals option
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 10, 2021
1 parent bb983a5 commit 85bc3da
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 61 deletions.
2 changes: 1 addition & 1 deletion ui/blocks/src/ComponentCommits/ComponentCommits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type ComponentCommitsProps = BlockContainerProps &
export const ComponentCommits: FC<ComponentCommitsProps> = ({
id,
name,
pagination,
pagination = { totalCountTemplate: '${totalData} commits' },
...rest
}) => {
const props = useCustomProps<ComponentCommitsProps>('commits', rest);
Expand Down
2 changes: 2 additions & 0 deletions ui/components/src/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export const pagination: Example<TablePaginationProps> = props => {
};

pagination.controls = {
totalCountVisible: true,
totalCountTemplate: 'Total: ${totalData} records',
pageIndex: 0,
pageSize: 10,
pageTemplate: 'Page ${pageIndex} of ${pageLength}',
Expand Down
142 changes: 84 additions & 58 deletions ui/components/src/Table/TablePagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ const runtimeTemplate = (str: string, obj: Record<string, any>) =>
str.replace(/\${(.*?)}/g, (x, g) => obj[g]);

export interface TablePaginationProps {
/**
* array of data records
*/
data?: any[];
/**
* whether to make totlal countfield visible
*/
totalCountVisible?: boolean;

/**
* total count label text
*/
totalCountTemplate?: string;
/**
* 'Page ${pageIndex} of ${pageLength}' template
*/
Expand Down Expand Up @@ -50,6 +63,9 @@ export const TablePagination: FC<UsePaginationInstanceProps<{
}> &
TablePaginationProps> = props => {
const {
totalCountVisible = true,
totalCountTemplate = 'Total: ${totalData} records',
data = [],
gotoPage,
canPreviousPage,
previousPage,
Expand All @@ -71,70 +87,80 @@ export const TablePagination: FC<UsePaginationInstanceProps<{
pageIndex: pageIndex + 1,
pageLength: pageOptions.length,
});

const totalCountResolvedTemplate = runtimeTemplate(totalCountTemplate, {
totalData: data.length,
});
return (
<Box variant="table.pagination.container">
{(canPreviousPage || canNextPage) && (
<Box variant="table.pagination.navigation">
<Box variant="table.pagination.navigation.button">
<Button onClick={() => gotoPage(0)} disabled={!canPreviousPage}>
{'<<'}
</Button>
</Box>
<Box variant="table.pagination.navigation.button">
<Button onClick={() => previousPage()} disabled={!canPreviousPage}>
{'<'}
</Button>
<Box variant="table.pagination.total">
{totalCountVisible && totalCountResolvedTemplate}
</Box>
<Box variant="table.pagination.navigationContainer">
{(canPreviousPage || canNextPage) && (
<Box variant="table.pagination.navigation">
<Box variant="table.pagination.navigation.button">
<Button onClick={() => gotoPage(0)} disabled={!canPreviousPage}>
{'<<'}
</Button>
</Box>
<Box variant="table.pagination.navigation.button">
<Button
onClick={() => previousPage()}
disabled={!canPreviousPage}
>
{'<'}
</Button>
</Box>
<Box variant="table.pagination.navigation.button">
<Button onClick={() => nextPage()} disabled={!canNextPage}>
{'>'}
</Button>
</Box>
<Box variant="table.pagination.navigation.button">
<Button
onClick={() => gotoPage(pageCount - 1)}
disabled={!canNextPage}
>
{'>>'}
</Button>
</Box>
</Box>
<Box variant="table.pagination.navigation.button">
<Button onClick={() => nextPage()} disabled={!canNextPage}>
{'>'}
</Button>
)}
{pageVisible && (
<Box variant="table.pagination.page">{pageResolvedTemplate}</Box>
)}
{goToPageVisible && (
<Box variant="table.pagination.interactive">
{goToPageTemplate}
<Input
type="number"
defaultValue={pageIndex + 1}
onChange={e => {
const page = e.target.value ? Number(e.target.value) - 1 : 0;
gotoPage(page);
}}
/>
</Box>
<Box variant="table.pagination.navigation.button">
<Button
onClick={() => gotoPage(pageCount - 1)}
disabled={!canNextPage}
)}
{pageSizeVisible && (
<Box variant="table.pagination.pagesize">
<Select
value={pageSize}
onChange={e => {
setPageSize(Number(e.target.value));
}}
>
{'>>'}
</Button>
{[10, 20, 30, 40, 50].map(pageSize => (
<option key={pageSize} value={pageSize}>
{runtimeTemplate(pageSizeTemplate, {
pageSize,
})}
</option>
))}
</Select>
</Box>
</Box>
)}
{pageVisible && (
<Box variant="table.pagination.page">{pageResolvedTemplate}</Box>
)}
{goToPageVisible && (
<Box variant="table.pagination.interactive">
{goToPageTemplate}
<Input
type="number"
defaultValue={pageIndex + 1}
onChange={e => {
const page = e.target.value ? Number(e.target.value) - 1 : 0;
gotoPage(page);
}}
/>
</Box>
)}
{pageSizeVisible && (
<Box variant="table.pagination.pagesize">
<Select
value={pageSize}
onChange={e => {
setPageSize(Number(e.target.value));
}}
>
{[10, 20, 30, 40, 50].map(pageSize => (
<option key={pageSize} value={pageSize}>
{runtimeTemplate(pageSizeTemplate, {
pageSize,
})}
</option>
))}
</Select>
</Box>
)}
)}
</Box>
</Box>
);
};
15 changes: 13 additions & 2 deletions ui/components/src/ThemeContext/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,21 @@ export const theme: ControlsTheme = {
container: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'flex-end',
justifyContent: 'space-between',
alignItems: 'center',
backgroundColor: 'background',
pt: 2,
py: 2,
},
total: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
px: 3,
},
navigationContainer: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
},
navigation: {
display: 'flex',
Expand Down

0 comments on commit 85bc3da

Please sign in to comment.