Skip to content

feat: Collapse expanded rows on each action #24

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

Merged
merged 1 commit into from
May 19, 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
12 changes: 9 additions & 3 deletions src/components/MultiLevelTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useState } from "react";
import React, { useEffect, useMemo, useState } from "react";

import {
type Row,
Expand Down Expand Up @@ -136,7 +136,7 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({
);
},
Filter: col.filterable
? ({ column }: { column: { setFilter: (value: string) => void } }) => (
? ({ column }: { column: { setFilter: (value: string) => void; filterValue?: string } }) => (
<input
value={filterInput}
onChange={(e) => {
Expand Down Expand Up @@ -164,7 +164,7 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({
nextPage,
previousPage,
setPageSize,
state: { pageIndex, pageSize: currentPageSize },
state: { pageIndex, pageSize: currentPageSize, sortBy, filters },
} = useTable(
{
columns: tableColumns,
Expand Down Expand Up @@ -208,6 +208,12 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({

const [expandedRows, setExpandedRows] = useState<Set<string | number>>(new Set());

// Collapse expanded rows when filtering or sorting occurs
useEffect(() => {
if (expandedRows.size > 0) setExpandedRows(new Set());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sortBy, filters]);

const toggleRow = (rowId: string | number) => {
setExpandedRows((prev) => {
const newSet = new Set(prev);
Expand Down
2 changes: 1 addition & 1 deletion src/components/TableHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const TableHeader: React.FC<TableHeaderProps> = ({
)}
<span
style={{ display: 'inline-flex', alignItems: 'center', cursor: isColumnSortable ? 'pointer' : 'default', userSelect: 'none' }}
onClick={isColumnSortable ? (e => { e.stopPropagation(); (sortProps.onClick as any)?.(e); }) : undefined}
onClick={isColumnSortable ? (e: React.MouseEvent) => { e.stopPropagation(); (sortProps.onClick as (e: React.MouseEvent) => void)?.(e); } : undefined}
>
{column.render('Header')}
<span className="sort-icon" style={{ marginLeft: 4 }}>
Expand Down
2 changes: 2 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface DataItem {
export interface TableStateWithPagination<T extends object> extends TableState<T> {
pageIndex: number;
pageSize: number;
sortBy: Array<{ id: string; desc: boolean }>;
filters: Array<{ id: string; value: string }>;
}

export interface TableInstanceWithHooks<T extends object> extends TableInstance<T> {
Expand Down