Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add batch actions and row selection to filter panel #26

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 143 additions & 3 deletions react/filterPanel/src/Example/FilterPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ import {
Popover,
OperationalTag,
PopoverContent,
Pagination,
TableBatchActions,
TableBatchAction,
} from '@carbon/react';
import { Close, Filter } from '@carbon/react/icons';
import {
Add,
Close,
Download,
Filter,
Save,
TrashCan,
} from '@carbon/react/icons';
const {
Table,
TableBody,
Expand All @@ -48,6 +58,8 @@ import {
Header,
getFacetedUniqueValues,
ColumnFilter,
getPaginationRowModel,
PaginationState,
} from '@tanstack/react-table';
import { rankItem } from '@tanstack/match-sorter-utils';

Expand Down Expand Up @@ -84,6 +96,49 @@ const tableId = 'table-' + Math.random().toString(36).substring(2, 15);
const columnHelper = createColumnHelper<Resource>();

const columns = [
{
id: 'select',
width: 48,
header: ({ table }) => (
// TableSelectAll throws DOM nesting error, using Checkbox instead to avoid this
<Checkbox
{...{
checked: table.getIsAllPageRowsSelected(),
indeterminate: table.getIsSomePageRowsSelected(),
onChange: () => {
const isIndeterminate = table.getIsSomeRowsSelected();
if (!isIndeterminate) {
table.toggleAllPageRowsSelected(true);
}
if (table.getIsAllPageRowsSelected()) {
table.toggleAllRowsSelected(false);
return;
}
if (isIndeterminate) {
table.toggleAllPageRowsSelected(true);
return;
}
},
id: 'batch-checkbox',
labelText: 'header checkbox',
hideLabel: true,
}}
/>
),
cell: ({ row }) => (
<Checkbox
{...{
checked: row.getIsSelected(),
disabled: !row.getCanSelect(),
indeterminate: row.getIsSomeSelected(),
onChange: row.getToggleSelectedHandler(),
id: `batch-checkbox__${row.id}`,
labelText: 'row checkbox',
hideLabel: true,
}}
/>
),
},
columnHelper.accessor((row) => row.name, {
id: 'name',
cell: (info) => <i>{info.getValue()}</i>,
Expand Down Expand Up @@ -121,6 +176,11 @@ export const FilterPanel = () => {
const [popoverOpen, setPopoverOpen] = useState(false);
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
const [localFilters, setLocalFilters] = useState<ColumnFiltersState>([]);
const [rowSelection, setRowSelection] = React.useState({});
const [pagination, setPagination] = React.useState<PaginationState>({
pageIndex: 0,
pageSize: 10,
});

const filterSummaryRef = useRef();
const measureTagRef = useRef();
Expand All @@ -143,13 +203,20 @@ export const FilterPanel = () => {
state: {
globalFilter,
columnFilters,
rowSelection,
pagination,
},
onColumnFiltersChange: setColumnFilters,
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(), //client side filtering
getFilteredRowModel: getFilteredRowModel(),
onGlobalFilterChange: setGlobalFilter,
globalFilterFn: 'fuzzy', //apply fuzzy filter to the global filter (most common use case for fuzzy filter)
getFacetedUniqueValues: getFacetedUniqueValues(),
getPaginationRowModel: getPaginationRowModel(),
onPaginationChange: setPagination,
enableRowSelection: (row) => row.original.status !== 'disabled', // conditionally disable rows
// enableRowSelection: row => row.original.age > 18, // or enable row selection
onRowSelectionChange: setRowSelection,
});

interface ExtendedColFilter extends ColumnFilter {
Expand Down Expand Up @@ -283,6 +350,16 @@ export const FilterPanel = () => {
duration: 0.25,
}
);
animate(
`#${tableId} .cds--pagination`,
{
width: '100%',
transform: 'translateX(0px)',
},
{
duration: 0.25,
}
);
} else {
animate(
`#${tableId} .panel--container`,
Expand All @@ -304,6 +381,16 @@ export const FilterPanel = () => {
duration: 0.25,
}
);
animate(
`#${tableId} .cds--pagination`,
{
width: 'calc(100% - 336px)',
transform: 'translateX(336px)',
},
{
duration: 0.25,
}
);
}
};

Expand All @@ -318,7 +405,7 @@ export const FilterPanel = () => {
));
};

console.log(table.getFilteredRowModel().rows.length === 0);
const shouldShowBatchActions = Object.keys(rowSelection).length > 0;

return (
<div ref={containerRef}>
Expand All @@ -335,6 +422,43 @@ export const FilterPanel = () => {
width: table.getCenterTotalSize(),
}}>
<TableToolbar>
<TableBatchActions
shouldShowBatchActions={shouldShowBatchActions}
totalSelected={Object.keys(rowSelection).length ?? 0}
onCancel={() => table.resetRowSelection()}
onSelectAll={() => {
table.toggleAllRowsSelected(true);
}}
totalCount={data?.length}>
<TableBatchAction
tabIndex={shouldShowBatchActions ? 0 : -1}
renderIcon={TrashCan}
onClick={() => table.resetRowSelection()}>
Delete
</TableBatchAction>
<TableBatchAction
hasIconOnly
iconDescription="Add"
tabIndex={shouldShowBatchActions ? 0 : -1}
renderIcon={Add}
onClick={() => table.resetRowSelection()}>
Delete
</TableBatchAction>
<TableBatchAction
hasIconOnly
iconDescription="Save"
tabIndex={shouldShowBatchActions ? 0 : -1}
renderIcon={Save}
onClick={() => table.resetRowSelection()}>
Save
</TableBatchAction>
<TableBatchAction
tabIndex={shouldShowBatchActions ? 0 : -1}
renderIcon={Download}
onClick={() => table.resetRowSelection()}>
Download
</TableBatchAction>
</TableBatchActions>
<TableToolbarContent>
<Layer>
<IconButton
Expand Down Expand Up @@ -553,6 +677,22 @@ export const FilterPanel = () => {
))}
</TableBody>
</Table>
<Pagination
page={table.getState().pagination.pageIndex + 1}
totalItems={data.length}
pagesUnknown={false}
pageInputDisabled={undefined}
pageSizeInputDisabled={undefined}
backwardText={'Previous page'}
forwardText={'Next page'}
pageSize={table.getState().pagination.pageSize}
pageSizes={[10, 20, 30, 40, 50]}
itemsPerPageText={'Items per page:'}
onChange={({ pageSize, page }) => {
table.setPageSize(Number(pageSize));
table.setPageIndex(page - 1);
}}
/>
</TableContainer>
</div>
);
Expand Down
16 changes: 13 additions & 3 deletions react/filterPanel/src/Example/example.scss
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

.panel--container {
min-height: calc(
var(--table-height) + 16px
var(--table-height) + 40px
); // account for x scrollbar of table

min-width: 320px;
Expand Down Expand Up @@ -124,7 +124,7 @@
overflow-y: scroll;
position: relative;
width: 100%;
height: calc(var(--table-height) + 16px);
height: calc(var(--table-height) + 40px);

.filter--panel__close-wrapper.cds--popover-container:not(
.cds--popover--auto-align
Expand Down Expand Up @@ -193,12 +193,22 @@
color: $text-inverse;
}

.tanstack-example .cds--data-table tbody.empty-table-body tr {
height: 14rem;
}

.tanstack-example .cds--data-table tbody.empty-table-body tr td {
display: flex;
align-items: start;
padding-top: 2rem;
border-block-end: 0;
}

.filter--summary-tag-and-overflow-wrapper {
display: flex;
align-items: center;
}

.cds--data-table.empty-table-wrapper td,
.cds--data-table.empty-table-wrapper tbody th,
.cds--data-table.empty-table-wrapper tbody tr:hover td {
border-block-end: 0;
Expand Down