1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2025-02-22 09:44:18 +01:00

Refactor RichTable component to remove forwardRef and simplify data handling

This commit is contained in:
Aleksandr Gumroian 2025-02-19 16:15:07 +01:00
parent 31cb8e2ae0
commit c7d0655771
No known key found for this signature in database
GPG Key ID: 9E77849C64F0A733

View File

@ -1,16 +1,11 @@
/* /*
* Copyright (C) 2019-2024 CZ.NIC z.s.p.o. (https://www.nic.cz/) * Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/)
* *
* This is free software, licensed under the GNU General Public License v3. * This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information. * See /LICENSE for more information.
*/ */
import React, { import React, { useMemo, useState } from "react";
useMemo,
useState,
useImperativeHandle,
forwardRef,
} from "react";
import { import {
flexRender, flexRender,
@ -25,24 +20,35 @@ import RichTableBody from "./RichTableBody";
import RichTableHeader from "./RichTableHeader"; import RichTableHeader from "./RichTableHeader";
import RichTablePagination from "./RichTablePagination"; import RichTablePagination from "./RichTablePagination";
const fallbackData = []; RichTable.propTypes = {
/** Columns to be displayed in the table */
columns: PropTypes.array.isRequired,
/** Data to be displayed in the table, must be passed as a stable reference, for example, useState */
data: PropTypes.array.isRequired,
/** Whether to display pagination */
withPagination: PropTypes.bool,
/** Number of rows per page, the default is 5 */
pageSize: PropTypes.number,
/** Index of the current page */
pageIndex: PropTypes.number,
};
const RichTable = forwardRef( export default function RichTable({
({ columns, data, withPagination, pageSize = 5, pageIndex = 0 }, ref) => { columns,
data,
withPagination,
pageSize = 5,
pageIndex = 0,
}) {
const tableColumns = useMemo(() => columns, [columns]); const tableColumns = useMemo(() => columns, [columns]);
const [tableData, setTableData] = useState(data ?? fallbackData);
const [sorting, setSorting] = useState([]); const [sorting, setSorting] = useState([]);
const [pagination, setPagination] = useState({ const [pagination, setPagination] = useState({
pageIndex, pageIndex,
pageSize, pageSize,
}); });
useImperativeHandle(ref, () => ({
setTableData,
}));
const table = useReactTable({ const table = useReactTable({
data: tableData, data,
columns: tableColumns, columns: tableColumns,
getCoreRowModel: getCoreRowModel(), getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(), getSortedRowModel: getSortedRowModel(),
@ -55,8 +61,7 @@ const RichTable = forwardRef(
}, },
}); });
const paginationIsNeeded = const paginationIsNeeded = data.length > pageSize && withPagination;
tableData.length > pageSize && withPagination;
return ( return (
<div className="table-responsive"> <div className="table-responsive">
@ -68,27 +73,9 @@ const RichTable = forwardRef(
<RichTablePagination <RichTablePagination
table={table} table={table}
tablePageSize={pageSize} tablePageSize={pageSize}
allRows={tableData.length} allRows={data.length}
/> />
)} )}
</div> </div>
); );
} }
);
RichTable.propTypes = {
/** Columns to be displayed in the table */
columns: PropTypes.array.isRequired,
/** Data to be displayed in the table */
data: PropTypes.array.isRequired,
/** Whether to display pagination */
withPagination: PropTypes.bool,
/** Number of rows per page */
pageSize: PropTypes.number,
/** Index of the current page */
pageIndex: PropTypes.number,
};
RichTable.displayName = "RichTable";
export default RichTable;