mirror of
				https://gitlab.nic.cz/turris/reforis/foris-js.git
				synced 2025-10-30 22:20:31 +01:00 
			
		
		
		
	Merge branch 'refactor-richtable' into 'dev'
Refactor RichTable See merge request turris/reforis/foris-js!269
This commit is contained in:
		| @@ -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; |  | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| /* | /* | ||||||
|  * Copyright (C) 2019 CZ.NIC z.s.p.o. (http://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. | ||||||
| @@ -20,13 +20,15 @@ export const STATES = { | |||||||
| SubmitButton.propTypes = { | SubmitButton.propTypes = { | ||||||
|     disabled: PropTypes.bool, |     disabled: PropTypes.bool, | ||||||
|     state: PropTypes.oneOf(Object.keys(STATES).map((key) => STATES[key])), |     state: PropTypes.oneOf(Object.keys(STATES).map((key) => STATES[key])), | ||||||
|  |     label: PropTypes.string, | ||||||
| }; | }; | ||||||
|  |  | ||||||
| export function SubmitButton({ disabled, state, ...props }) { | export function SubmitButton({ disabled, state, label, ...props }) { | ||||||
|     const disableSubmitButton = disabled || state !== STATES.READY; |     const disableSubmitButton = disabled || state !== STATES.READY; | ||||||
|     const loadingSubmitButton = state !== STATES.READY; |     const loadingSubmitButton = state !== STATES.READY; | ||||||
|  |  | ||||||
|     let labelSubmitButton; |     let labelSubmitButton = label; | ||||||
|  |     if (!labelSubmitButton) { | ||||||
|         switch (state) { |         switch (state) { | ||||||
|             case STATES.SAVING: |             case STATES.SAVING: | ||||||
|                 labelSubmitButton = _("Updating"); |                 labelSubmitButton = _("Updating"); | ||||||
| @@ -37,6 +39,7 @@ export function SubmitButton({ disabled, state, ...props }) { | |||||||
|             default: |             default: | ||||||
|                 labelSubmitButton = _("Save"); |                 labelSubmitButton = _("Save"); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     return ( |     return ( | ||||||
|         <Button |         <Button | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user