mirror of
				https://gitlab.nic.cz/turris/reforis/foris-js.git
				synced 2025-11-03 23:00:31 +01:00 
			
		
		
		
	Refactor RichTable component to use forwardRef
And useImperativeHandle for improved data handling.
This commit is contained in:
		@@ -5,7 +5,12 @@
 | 
				
			|||||||
 * See /LICENSE for more information.
 | 
					 * See /LICENSE for more information.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import React, { useMemo, useState } from "react";
 | 
					import React, {
 | 
				
			||||||
 | 
					    useMemo,
 | 
				
			||||||
 | 
					    useState,
 | 
				
			||||||
 | 
					    useImperativeHandle,
 | 
				
			||||||
 | 
					    forwardRef,
 | 
				
			||||||
 | 
					} from "react";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
    flexRender,
 | 
					    flexRender,
 | 
				
			||||||
@@ -22,6 +27,55 @@ import RichTablePagination from "./RichTablePagination";
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const fallbackData = [];
 | 
					const fallbackData = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const RichTable = forwardRef(
 | 
				
			||||||
 | 
					    ({ columns, data, withPagination, pageSize = 5, pageIndex = 0 }, ref) => {
 | 
				
			||||||
 | 
					        const tableColumns = useMemo(() => columns, [columns]);
 | 
				
			||||||
 | 
					        const [tableData, setTableData] = useState(data ?? fallbackData);
 | 
				
			||||||
 | 
					        const [sorting, setSorting] = useState([]);
 | 
				
			||||||
 | 
					        const [pagination, setPagination] = useState({
 | 
				
			||||||
 | 
					            pageIndex,
 | 
				
			||||||
 | 
					            pageSize,
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        useImperativeHandle(ref, () => ({
 | 
				
			||||||
 | 
					            setTableData,
 | 
				
			||||||
 | 
					        }));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const table = useReactTable({
 | 
				
			||||||
 | 
					            data: tableData,
 | 
				
			||||||
 | 
					            columns: tableColumns,
 | 
				
			||||||
 | 
					            getCoreRowModel: getCoreRowModel(),
 | 
				
			||||||
 | 
					            getSortedRowModel: getSortedRowModel(),
 | 
				
			||||||
 | 
					            getPaginationRowModel: getPaginationRowModel(),
 | 
				
			||||||
 | 
					            onPaginationChange: setPagination,
 | 
				
			||||||
 | 
					            onSortingChange: setSorting,
 | 
				
			||||||
 | 
					            state: {
 | 
				
			||||||
 | 
					                sorting,
 | 
				
			||||||
 | 
					                pagination,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const paginationIsNeeded =
 | 
				
			||||||
 | 
					            tableData.length > pageSize && withPagination;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return (
 | 
				
			||||||
 | 
					            <div className="table-responsive">
 | 
				
			||||||
 | 
					                <table className="table table-hover text-nowrap">
 | 
				
			||||||
 | 
					                    <RichTableHeader table={table} flexRender={flexRender} />
 | 
				
			||||||
 | 
					                    <RichTableBody table={table} flexRender={flexRender} />
 | 
				
			||||||
 | 
					                </table>
 | 
				
			||||||
 | 
					                {paginationIsNeeded && (
 | 
				
			||||||
 | 
					                    <RichTablePagination
 | 
				
			||||||
 | 
					                        table={table}
 | 
				
			||||||
 | 
					                        tablePageSize={pageSize}
 | 
				
			||||||
 | 
					                        allRows={tableData.length}
 | 
				
			||||||
 | 
					                    />
 | 
				
			||||||
 | 
					                )}
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
RichTable.propTypes = {
 | 
					RichTable.propTypes = {
 | 
				
			||||||
    /** Columns to be displayed in the table */
 | 
					    /** Columns to be displayed in the table */
 | 
				
			||||||
    columns: PropTypes.array.isRequired,
 | 
					    columns: PropTypes.array.isRequired,
 | 
				
			||||||
@@ -35,52 +89,6 @@ RichTable.propTypes = {
 | 
				
			|||||||
    pageIndex: PropTypes.number,
 | 
					    pageIndex: PropTypes.number,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function RichTable({
 | 
					RichTable.displayName = "RichTable";
 | 
				
			||||||
    columns,
 | 
					 | 
				
			||||||
    data,
 | 
					 | 
				
			||||||
    withPagination,
 | 
					 | 
				
			||||||
    pageSize = 5,
 | 
					 | 
				
			||||||
    pageIndex = 0,
 | 
					 | 
				
			||||||
}) {
 | 
					 | 
				
			||||||
    const tableColumns = useMemo(() => columns, [columns]);
 | 
					 | 
				
			||||||
    const [tableData] = useState(data ?? fallbackData);
 | 
					 | 
				
			||||||
    const [sorting, setSorting] = useState([]);
 | 
					 | 
				
			||||||
    const [pagination, setPagination] = useState({
 | 
					 | 
				
			||||||
        pageIndex,
 | 
					 | 
				
			||||||
        pageSize,
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const table = useReactTable({
 | 
					 | 
				
			||||||
        data: tableData,
 | 
					 | 
				
			||||||
        columns: tableColumns,
 | 
					 | 
				
			||||||
        getCoreRowModel: getCoreRowModel(),
 | 
					 | 
				
			||||||
        getSortedRowModel: getSortedRowModel(),
 | 
					 | 
				
			||||||
        getPaginationRowModel: getPaginationRowModel(),
 | 
					 | 
				
			||||||
        onPaginationChange: setPagination,
 | 
					 | 
				
			||||||
        onSortingChange: setSorting,
 | 
					 | 
				
			||||||
        state: {
 | 
					 | 
				
			||||||
            sorting,
 | 
					 | 
				
			||||||
            pagination,
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const paginationIsNeeded = tableData.length > pageSize && withPagination;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return (
 | 
					 | 
				
			||||||
        <div className="table-responsive">
 | 
					 | 
				
			||||||
            <table className="table table-hover text-nowrap">
 | 
					 | 
				
			||||||
                <RichTableHeader table={table} flexRender={flexRender} />
 | 
					 | 
				
			||||||
                <RichTableBody table={table} flexRender={flexRender} />
 | 
					 | 
				
			||||||
            </table>
 | 
					 | 
				
			||||||
            {paginationIsNeeded && (
 | 
					 | 
				
			||||||
                <RichTablePagination
 | 
					 | 
				
			||||||
                    table={table}
 | 
					 | 
				
			||||||
                    tablePageSize={pageSize}
 | 
					 | 
				
			||||||
                    allRows={tableData.length}
 | 
					 | 
				
			||||||
                />
 | 
					 | 
				
			||||||
            )}
 | 
					 | 
				
			||||||
        </div>
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default RichTable;
 | 
					export default RichTable;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user