Skip to content

Commit

Permalink
feat: store sort state
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed Jun 23, 2024
1 parent b541d18 commit d0ca56c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/components/ConnectionTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ const sortById = { id: 'id', desc: true };
function Table({ data, columns, hiddenColumns, apiConfig }) {
const [operationId, setOperationId] = useState('');
const [showModalDisconnect, setShowModalDisconnect] = useState(false);

// 从本地存储加载排序状态
const savedSortBy = JSON.parse(localStorage.getItem('tableSortBy')) || [sortById];

const tableState = {
sortBy: [
// maintain a more stable order
sortById,
],
sortBy: savedSortBy,
hiddenColumns,
};

const table = useTable(
{
columns,
Expand All @@ -41,10 +43,12 @@ function Table({ data, columns, hiddenColumns, apiConfig }) {
);

const { getTableProps, setHiddenColumns, headerGroups, rows, prepareRow } = table;
const state = table.state;

useEffect(() => {
setHiddenColumns(hiddenColumns);
}, [setHiddenColumns, hiddenColumns]);

const { t, i18n } = useTranslation();

let locale: Locale;
Expand All @@ -67,10 +71,7 @@ function Table({ data, columns, hiddenColumns, apiConfig }) {
setShowModalDisconnect(true);
};

const renderCell = (
cell: { column: { id: string }; row: { original: { id: string } }; value: number },
locale: Locale
) => {
const renderCell = (cell, locale) => {
switch (cell.column.id) {
case 'ctrl':
return (
Expand All @@ -92,6 +93,11 @@ function Table({ data, columns, hiddenColumns, apiConfig }) {
}
};

// 当排序状态改变时,将新状态保存到本地存储
useEffect(() => {
localStorage.setItem('tableSortBy', JSON.stringify(state.sortBy));
}, [state.sortBy]);

return (
<div style={{ marginTop: '5px' }}>
<table {...getTableProps()} className={cx(s.table, 'connections-table')}>
Expand Down
1 change: 1 addition & 0 deletions src/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare module 'react-table' {
options: TableOptions,
useSortBy: useSortBy
): {
state: any;
headerGroups: HeaderGroup[];
getTableProps(): { role?: string };
setHiddenColumns: (columns: string[]) => void;
Expand Down

0 comments on commit d0ca56c

Please sign in to comment.