Skip to content

Commit

Permalink
Merge pull request #231 from yazhouio/fix/table-3
Browse files Browse the repository at this point in the history
fix: Fix table bugs
  • Loading branch information
yazhouio authored May 30, 2024
2 parents c7f3d3b + 684883c commit 6138e0d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-rocks-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kubed/components': patch
---

fix: Fix table bugs
1 change: 1 addition & 0 deletions packages/components/src/Table/DataTable/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare module '@tanstack/react-table' {
extends StorageStateOptions,
FeaturesHandlersOptions {
loading?: boolean;
autoResetFiltersWhenPageChange?: boolean;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
7 changes: 4 additions & 3 deletions packages/components/src/Table/DataTable/TableHead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,14 @@ export function TableHead<T>({ header, table }: PropsWithChildren<TableHeadProps
const {
options: {
manualSorting,
manualFiltering,
meta: {
enableDefault: { th: enableTh = true } = {},
getProps: { th: getThProps, filters: getFiltersProps } = {},
} = {},
} = {},
} = table;

const searchKey = manualFiltering ? _searchKey ?? id : id;
const searchKey = _searchKey ?? id;

const isCheckbox = selectType
? selectType === 'multiple'
Expand All @@ -86,7 +85,9 @@ export function TableHead<T>({ header, table }: PropsWithChildren<TableHeadProps
const filterOptions =
suggestions.find((suggestion) => suggestion.key === searchKey)?.options ?? [];

const sortable = !manualSorting ? header.column.getCanSort() : _sortable;
const sortable =
header.column.columnDef.enableSorting ??
(!manualSorting ? header.column.getCanSort() : _sortable);

const { enableMultiSort = false } = table.options;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { TableFeature, TableOptionsResolved, getPaginationRowModel } from '@tanstack/react-table';
import {
PaginationState,
TableFeature,
TableOptionsResolved,
Updater,
functionalUpdate,
getPaginationRowModel,
} from '@tanstack/react-table';

const getPaginationOptions = <TData>(options: TableOptionsResolved<TData>) => {
const {
Expand All @@ -15,6 +22,7 @@ const getPaginationOptions = <TData>(options: TableOptionsResolved<TData>) => {
manualPagination: true,
rowCount: 0,
autoResetPageIndex: false,
autoResetFiltersWhenPageChange: true,
...options,
};
};
Expand All @@ -32,4 +40,19 @@ export const InitPaginationFeature: TableFeature<any> = {
getDefaultOptionsResolved: (options) => {
return getPaginationOptions(options);
},
createTable: (table) => {
// eslint-disable-next-line no-param-reassign
table.setPagination = (updater) => {
const safeUpdater: Updater<PaginationState> = (old) => {
const newState = functionalUpdate(updater, old);
return newState;
};

const { autoResetFiltersWhenPageChange = false } = table.options;
if (autoResetFiltersWhenPageChange) {
table.setRowSelection({});
}
return table.options.onPaginationChange?.(safeUpdater);
};
},
};
16 changes: 14 additions & 2 deletions packages/components/src/Table/DataTable/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ export function getDefaultToolbarProps<T>(
if (enableSettingMenu === undefined) {
enableSettingMenu = visibilityColumns.length > 0;
}
const enableBatchActions = table.getIsAllPageRowsSelected() || table.getIsSomeRowsSelected();
const enableBatchActions =
table.getIsAllRowsSelected() ||
table.getIsAllPageRowsSelected() ||
table.getIsSomeRowsSelected() ||
table.getIsSomePageRowsSelected();

let filterProps;
let defaultFilterProps;
Expand Down Expand Up @@ -106,6 +110,14 @@ export function getDefaultToolbarProps<T>(
const { id: menuItemKey } = column;
const isVisible = column.getIsVisible();
const icon = isVisible ? <Eye /> : <EyeClosed />;
let title = column.columnDef.header;
if (typeof column.columnDef.header === 'function') {
title = column.columnDef.header({
table,
header: undefined,
column,
});
}
return (
<MenuItem
key={menuItemKey}
Expand All @@ -114,7 +126,7 @@ export function getDefaultToolbarProps<T>(
column.toggleVisibility(!isVisible);
}}
>
{column.id}
{title}
</MenuItem>
);
})}
Expand Down
20 changes: 17 additions & 3 deletions packages/components/src/Table/Table.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ const defaultColumns: ColumnDef<Person>[] = [
{
accessorKey: 'firstName',
cell: (info) => info.getValue(),
enableSorting: true,
footer: (props) => props.column.id,
},
{
Expand All @@ -424,8 +425,11 @@ const defaultColumns: ColumnDef<Person>[] = [
columns: [
{
accessorKey: 'age',
header: () => 'Age',
header: () => {
return 'Age';
},
footer: (props) => props.column.id,
enableHiding: true,
meta: {
sortable: true,
// filterOptions: [
Expand All @@ -452,6 +456,8 @@ const defaultColumns: ColumnDef<Person>[] = [
accessorKey: 'status',
header: 'Status',
footer: (props) => props.column.id,

enableHiding: true,
meta: {
searchKey: 'status1',
},
Expand Down Expand Up @@ -633,7 +639,7 @@ export const DataTableWithSelected = () => {
id: 'selection',
header: ({ table }) => (
<Checkbox
checked={table.getIsAllRowsSelected()}
checked={table.getIsAllRowsSelected() || table.getIsAllPageRowsSelected()}
indeterminate={table.getIsSomeRowsSelected()}
onChange={table.getToggleAllRowsSelectedHandler()} //or getToggleAllPageRowsSelectedHandler
/>
Expand Down Expand Up @@ -729,7 +735,15 @@ export const DataTableWithSelected = () => {
}),
toolbar: () => {
return {
batchActions: <Button> Delete </Button>,
batchActions: (
<Button
onClick={() => {
console.log(rowSelection);
}}
>
Delete
</Button>
),
toolbarLeft: <Select />,
toolbarRight: (
<>
Expand Down

0 comments on commit 6138e0d

Please sign in to comment.