1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2025-05-05 09:40:54 +02:00

Compare commits

..

No commits in common. "bf0b2ce70ce1a3a11135fae4b46db04f958b3910" and "31cb8e2ae02fc1e8ef34cdc2bc45c88917d7b667" have entirely different histories.

2 changed files with 74 additions and 64 deletions

View File

@ -1,11 +1,16 @@
/*
* Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/)
* Copyright (C) 2019-2024 CZ.NIC z.s.p.o. (https://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import React, { useMemo, useState } from "react";
import React, {
useMemo,
useState,
useImperativeHandle,
forwardRef,
} from "react";
import {
flexRender,
@ -20,35 +25,24 @@ import RichTableBody from "./RichTableBody";
import RichTableHeader from "./RichTableHeader";
import RichTablePagination from "./RichTablePagination";
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 fallbackData = [];
export default function RichTable({
columns,
data,
withPagination,
pageSize = 5,
pageIndex = 0,
}) {
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,
data: tableData,
columns: tableColumns,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
@ -61,7 +55,8 @@ export default function RichTable({
},
});
const paginationIsNeeded = data.length > pageSize && withPagination;
const paginationIsNeeded =
tableData.length > pageSize && withPagination;
return (
<div className="table-responsive">
@ -73,9 +68,27 @@ export default function RichTable({
<RichTablePagination
table={table}
tablePageSize={pageSize}
allRows={data.length}
allRows={tableData.length}
/>
)}
</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;

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/)
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
@ -20,15 +20,13 @@ export const STATES = {
SubmitButton.propTypes = {
disabled: PropTypes.bool,
state: PropTypes.oneOf(Object.keys(STATES).map((key) => STATES[key])),
label: PropTypes.string,
};
export function SubmitButton({ disabled, state, label, ...props }) {
export function SubmitButton({ disabled, state, ...props }) {
const disableSubmitButton = disabled || state !== STATES.READY;
const loadingSubmitButton = state !== STATES.READY;
let labelSubmitButton = label;
if (!labelSubmitButton) {
let labelSubmitButton;
switch (state) {
case STATES.SAVING:
labelSubmitButton = _("Updating");
@ -39,7 +37,6 @@ export function SubmitButton({ disabled, state, label, ...props }) {
default:
labelSubmitButton = _("Save");
}
}
return (
<Button