1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-11-14 17:35:35 +01:00

Compare commits

..

4 Commits

Author SHA1 Message Date
Aleksandr Gumroian
e6365ecac4
Update Snapshots 2024-11-08 17:59:15 +01:00
Aleksandr Gumroian
e57722caa0
Add RebootButton and RichTable components to documentation 2024-11-08 17:59:15 +01:00
Aleksandr Gumroian
babdf92ddd
Fix import path for CustomizationContextMock in customTestRender.js 2024-11-08 17:59:02 +01:00
Aleksandr Gumroian
42294316d9
Add RichTable component with header, body, and pagination 2024-11-08 17:59:02 +01:00
11 changed files with 266 additions and 39 deletions

View File

@ -16,6 +16,11 @@ import { Modal, ModalHeader, ModalBody, ModalFooter } from "../bootstrap/Modal";
import { useAlert } from "../context/alertContext/AlertContext"; import { useAlert } from "../context/alertContext/AlertContext";
import { ForisURLs } from "../utils/forisUrls"; import { ForisURLs } from "../utils/forisUrls";
RebootButton.propTypes = {
/** Additional props to be passed to the button */
props: PropTypes.object,
};
function RebootButton(props) { function RebootButton(props) {
const [triggered, setTriggered] = useState(false); const [triggered, setTriggered] = useState(false);
const [modalShown, setModalShown] = useState(false); const [modalShown, setModalShown] = useState(false);
@ -68,7 +73,12 @@ function RebootModal({ shown, setShown, onReboot }) {
<p>{_("Are you sure you want to restart the router?")}</p> <p>{_("Are you sure you want to restart the router?")}</p>
</ModalBody> </ModalBody>
<ModalFooter> <ModalFooter>
<Button onClick={() => setShown(false)}>{_("Cancel")}</Button> <Button
className="btn-secondary"
onClick={() => setShown(false)}
>
{_("Cancel")}
</Button>
<Button className="btn-danger" onClick={onReboot}> <Button className="btn-danger" onClick={onReboot}>
{_("Confirm reboot")} {_("Confirm reboot")}
</Button> </Button>

View File

@ -0,0 +1,24 @@
RebootButton component is a button that opens a modal dialog to confirm the
reboot of the device.
## Usage
```jsx
import React, { useEffect, createContext } from "react";
import RebootButton from "./RebootButton";
import { AlertContextProvider } from "../context/alertContext/AlertContext";
window.AlertContext = React.createContext();
const RebootButtonExample = () => {
return (
<AlertContextProvider>
<div id="modal-container" />
<div id="alert-container" />
<RebootButton />
</AlertContextProvider>
);
};
<RebootButtonExample />;
```

View File

@ -14,6 +14,7 @@ import {
getPaginationRowModel, getPaginationRowModel,
useReactTable, useReactTable,
} from "@tanstack/react-table"; } from "@tanstack/react-table";
import PropTypes from "prop-types";
import RichTableBody from "./RichTableBody"; import RichTableBody from "./RichTableBody";
import RichTableHeader from "./RichTableHeader"; import RichTableHeader from "./RichTableHeader";
@ -21,15 +22,28 @@ import RichTablePagination from "./RichTablePagination";
const fallbackData = []; const fallbackData = [];
const RichTable = ({ 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,
};
function RichTable({
columns, columns,
data, data,
withPagination, withPagination,
pageSize = 5, pageSize = 5,
pageIndex = 0, pageIndex = 0,
}) => { }) {
const tableColumns = useMemo(() => columns, []); const tableColumns = useMemo(() => columns, [columns]);
const [tableData, _] = useState(data ?? fallbackData); const [tableData] = useState(data ?? fallbackData);
const [sorting, setSorting] = useState([]); const [sorting, setSorting] = useState([]);
const [pagination, setPagination] = useState({ const [pagination, setPagination] = useState({
pageIndex, pageIndex,
@ -65,6 +79,6 @@ const RichTable = ({
)} )}
</div> </div>
); );
}; }
export default RichTable; export default RichTable;

View File

@ -0,0 +1,135 @@
### Description
Rich Table is a table component based on
[Tanstack React Table](https://tanstack.com/table/). It adds some features to
the table component, such as:
- **Pagination**: The table can be paginated.
- **Sorting**: The table can be sorted by columns.
- **Row Expansion**: The table rows can be expanded. (To be implemented)
### Example
```js
import RichTable from "./RichTable";
const columns = [
{
header: "Name",
accessorKey: "name",
},
{
header: "Surname",
accessorKey: "surname",
},
{
header: "Age",
accessorKey: "age",
},
{
header: "Phone",
accessorKey: "phone",
},
];
const data = [
{
name: "John",
surname: "Coltrane",
age: 30,
phone: "123456789",
},
{
name: "Jane",
surname: "Doe",
age: 25,
phone: "987654321",
},
{
name: "Alice",
surname: "Smith",
age: 35,
phone: "123456789",
},
{
name: "Bob",
surname: "Smith",
age: 40,
phone: "987654321",
},
{
name: "Charlie",
surname: "Brown",
age: 45,
phone: "123456789",
},
{
name: "Daisy",
surname: "Brown",
age: 50,
phone: "987654321",
},
{
name: "Eve",
surname: "Johnson",
age: 55,
phone: "123456789",
},
{
name: "Frank",
surname: "Johnson",
age: 60,
phone: "987654321",
},
{
name: "Grace",
surname: "Williams",
age: 65,
phone: "123456789",
},
{
name: "Henry",
surname: "Williams",
age: 70,
phone: "987654321",
},
{
name: "Ivy",
surname: "Brown",
age: 75,
phone: "123456789",
},
{
name: "Jack",
surname: "Brown",
age: 80,
phone: "987654321",
},
{
name: "Kelly",
surname: "Johnson",
age: 85,
phone: "123456789",
},
{
name: "Liam",
surname: "Johnson",
age: 90,
phone: "987654321",
},
{
name: "Mia",
surname: "Williams",
age: 95,
phone: "123456789",
},
{
name: "Nathan",
surname: "Williams",
age: 100,
phone: "987654321",
},
];
<RichTable columns={columns} data={data} withPagination />;
```

View File

@ -7,7 +7,16 @@
import React from "react"; import React from "react";
const RichTableBody = ({ table, flexRender }) => { import propTypes from "prop-types";
RichTableBody.propTypes = {
table: propTypes.shape({
getRowModel: propTypes.func.isRequired,
}).isRequired,
flexRender: propTypes.func.isRequired,
};
function RichTableBody({ table, flexRender }) {
return ( return (
<tbody> <tbody>
{table.getRowModel().rows.map((row) => { {table.getRowModel().rows.map((row) => {
@ -34,6 +43,6 @@ const RichTableBody = ({ table, flexRender }) => {
})} })}
</tbody> </tbody>
); );
}; }
export default RichTableBody; export default RichTableBody;

View File

@ -6,21 +6,30 @@
*/ */
import React from "react"; import React from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { import {
faSquareCaretUp, faSquareCaretUp,
faSquareCaretDown, faSquareCaretDown,
} from "@fortawesome/free-solid-svg-icons"; } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import propTypes from "prop-types";
const RichTableHeader = ({ table, flexRender }) => { RichTableHeader.propTypes = {
const getThTitle = (header) => table: propTypes.shape({
header.column.getCanSort() getHeaderGroups: propTypes.func.isRequired,
? header.column.getNextSortingOrder() === "asc" }).isRequired,
? _("Sort ascending") flexRender: propTypes.func.isRequired,
: header.column.getNextSortingOrder() === "desc" };
? _("Sort descending")
: _("Clear sort") function RichTableHeader({ table, flexRender }) {
: undefined; const getThTitle = (header) => {
if (!header.column.getCanSort()) return undefined;
const nextSortingOrder = header.column.getNextSortingOrder();
if (nextSortingOrder === "asc") return _("Sort ascending");
if (nextSortingOrder === "desc") return _("Sort descending");
return _("Clear sort");
};
return ( return (
<thead className="thead-light"> <thead className="thead-light">
@ -45,6 +54,7 @@ const RichTableHeader = ({ table, flexRender }) => {
</div> </div>
) : ( ) : (
<button <button
type="button"
className={`btn btn-link text-decoration-none text-reset fw-bold p-0 d-flex align-items-center className={`btn btn-link text-decoration-none text-reset fw-bold p-0 d-flex align-items-center
${ ${
header.column.getCanSort() header.column.getCanSort()
@ -81,6 +91,6 @@ const RichTableHeader = ({ table, flexRender }) => {
))} ))}
</thead> </thead>
); );
}; }
export default RichTableHeader; export default RichTableHeader;

View File

@ -6,15 +6,33 @@
*/ */
import React, { useMemo } from "react"; import React, { useMemo } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { import {
faAngleLeft, faAngleLeft,
faAnglesLeft, faAnglesLeft,
faAngleRight, faAngleRight,
faAnglesRight, faAnglesRight,
} from "@fortawesome/free-solid-svg-icons"; } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import propTypes from "prop-types";
const RichTablePagination = ({ table, tablePageSize, allRows }) => { RichTablePagination.propTypes = {
table: propTypes.shape({
getState: propTypes.func.isRequired,
getCanPreviousPage: propTypes.func.isRequired,
getCanNextPage: propTypes.func.isRequired,
firstPage: propTypes.func.isRequired,
previousPage: propTypes.func.isRequired,
nextPage: propTypes.func.isRequired,
lastPage: propTypes.func.isRequired,
setPageSize: propTypes.func.isRequired,
getPageCount: propTypes.func.isRequired,
}).isRequired,
tablePageSize: propTypes.number,
allRows: propTypes.number,
};
function RichTablePagination({ table, tablePageSize, allRows }) {
const { pagination } = table.getState(); const { pagination } = table.getState();
const prevPagBtnDisabled = !table.getCanPreviousPage(); const prevPagBtnDisabled = !table.getCanPreviousPage();
const nextPagBtnDisabled = !table.getCanNextPage(); const nextPagBtnDisabled = !table.getCanNextPage();
@ -31,6 +49,7 @@ const RichTablePagination = ({ table, tablePageSize, allRows }) => {
style={{ cursor: disabled ? "not-allowed" : "pointer" }} style={{ cursor: disabled ? "not-allowed" : "pointer" }}
> >
<button <button
type="button"
className="page-link" className="page-link"
aria-label={ariaLabel} aria-label={ariaLabel}
onClick={onClick} onClick={onClick}
@ -104,6 +123,6 @@ const RichTablePagination = ({ table, tablePageSize, allRows }) => {
</select> </select>
</nav> </nav>
); );
}; }
export default RichTablePagination; export default RichTablePagination;

View File

@ -43,7 +43,7 @@ exports[`<RebootButton/> Render modal. 1`] = `
class="modal-footer" class="modal-footer"
> >
<button <button
class="btn btn-primary d-inline-flex justify-content-center align-items-center" class="btn btn-secondary d-inline-flex justify-content-center align-items-center"
type="button" type="button"
> >
<span> <span>

View File

@ -14,7 +14,7 @@ import { render } from "@testing-library/react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { AlertContextMock } from "./alertContextMock"; import { AlertContextMock } from "./alertContextMock";
import { CustomizationContextMock } from "./cutomizationContextMock"; import { CustomizationContextMock } from "./customizationContextMock";
Wrapper.propTypes = { Wrapper.propTypes = {
children: PropTypes.oneOfType([ children: PropTypes.oneOfType([

View File

@ -28,11 +28,11 @@ module.exports = {
content: "docs/development.md", content: "docs/development.md",
}, },
{ {
name: "Components", name: "Common Components",
description: "Set of main components.", description: "Set of main components.",
sections: [ sections: [
{ {
name: "Foris forms", name: "Foris Form",
components: [ components: [
"src/form/components/ForisForm.js", "src/form/components/ForisForm.js",
"src/form/components/alerts.js", "src/form/components/alerts.js",
@ -42,25 +42,22 @@ module.exports = {
usageMode: "expand", usageMode: "expand",
}, },
{ {
name: "Alert Context", name: "Rich Table",
components: ["src/context/alertContext/AlertContext.js"], components: ["src/common/RichTable/RichTable.js"],
exampleMode: "expand",
usageMode: "expand",
},
{
name: "Reboot Button",
components: ["src/common/RebootButton.js"],
exampleMode: "expand", exampleMode: "expand",
usageMode: "expand", usageMode: "expand",
}, },
], ],
sectionDepth: 1, sectionDepth: 1,
}, },
{ {
name: "Customization Context", name: "Bootstrap Components",
components: [
"src/context/customizationContext/CustomizationContext.js",
],
exampleMode: "expand",
usageMode: "expand",
},
{
name: "Bootstrap components",
description: "Set of bootstrap components.", description: "Set of bootstrap components.",
components: "src/bootstrap/*.js", components: "src/bootstrap/*.js",
exampleMode: "expand", exampleMode: "expand",
@ -68,13 +65,22 @@ module.exports = {
ignore: ["src/bootstrap/constants.js", "src/bootstrap/Radio.js"], ignore: ["src/bootstrap/constants.js", "src/bootstrap/Radio.js"],
sectionDepth: 0, sectionDepth: 0,
}, },
{
name: "Contexts",
components: [
"src/context/alertContext/AlertContext.js",
"src/context/customizationContext/CustomizationContext.js",
],
exampleMode: "expand",
usageMode: "expand",
},
], ],
template: { template: {
favicon: "/docs/components/logo.svg", favicon: "/docs/components/logo.svg",
}, },
require: [ require: [
"babel-polyfill", "babel-polyfill",
path.join(__dirname, "src/testUtils/mockGlobals"), path.join(__dirname, "src/testUtils/mockGlobals.js"),
path.join( path.join(
__dirname, __dirname,
"node_modules/bootstrap/dist/css/bootstrap.min.css" "node_modules/bootstrap/dist/css/bootstrap.min.css"