Debian Codegraph @

install/freeipa-webui/src/components/layouts/SettingsTableLayout/SettingsTableLayout.tsx

raw
1 import React from "react";
2 // PatternFly
3 import {
4 EmptyState,
5 EmptyStateActions,
6 EmptyStateBody,
7 EmptyStateVariant,
8 Flex,
9 FlexItem,
10 SearchInput,
11 } from "@patternfly/react-core";
12 import { TableVariant } from "@patternfly/react-table";
13 // Layout
14 import SecondaryButton from "../SecondaryButton";
15 import TableLayout from "../TableLayout";
16 import PaginationLayout from "../PaginationLayout";
17
18 interface PropsToSettingsTableLayout {
19 // Table
20 ariaLabel: string;
21 name?: string;
22 variant: TableVariant | "compact";
23 hasBorders: boolean;
24 tableClasses?: string;
25 tableId: string;
26 isStickyHeader: boolean;
27 tableHeader?: JSX.Element;
28 tableBody: JSX.Element[] | JSX.Element;
29 onDeleteModal: () => void;
30 isDeleteDisabled?: boolean;
31 onAddModal: () => void;
32 isAddDisabled?: boolean;
33 onSearchChange?: (value: string) => void;
34 searchValue?: string;
35 // pagination
36 paginationData: PaginationData;
37 // eslint-disable-next-line @typescript-eslint/no-explicit-any
38 list: any[];
39 entryCount?: number;
40 entryType: string;
41 extraID?: string;
42 }
43
44 interface PaginationData {
45 page: number;
46 perPage: number;
47 updatePage: (newPage: number) => void;
48 updatePerPage: (newSetPerPage: number) => void;
49 updateSelectedPerPage: (selected: number) => void;
50 // eslint-disable-next-line @typescript-eslint/no-explicit-any
51 updateShownElementsList: (newShownElementsList: any[]) => void;
52 totalCount: number;
53 }
54
55 // The settings table is meant to be used in the "settings" page for users,
56 // hosts, etc. We need smaller sized pagination, and other differences that
57 // are ideal in a crowded page where space is limited
58
59 const SettingsTableLayout = (props: PropsToSettingsTableLayout) => {
60 const extraId = props.extraID ? props.extraID : "";
61 const entryCount = props.entryCount ?? props.list.length;
62 // Prepare ID for the Add buttons
63 // - Should contain the entry type in lowercase and spaces replaced by dashes
64 const addButtonId = extraId
65 ? "add-" + extraId + "-" + props.entryType.toLowerCase().replace(" ", "-")
66 : "add-" + props.entryType.toLowerCase().replace(" ", "-");
67
68 return (
69 <>
70 <Flex>
71 <FlexItem>
72 {entryCount > 0 && props.onSearchChange !== undefined && (
73 <SearchInput
74 data-cy={"search-" + props.name}
75 placeholder={"Filter by ..."}
76 value={props.searchValue}
77 onChange={(_event, value: string) =>
78 props.onSearchChange && props.onSearchChange(value)
79 }
80 onClear={() => props.onSearchChange && props.onSearchChange("")}
81 />
82 )}
83 </FlexItem>
84 {props.paginationData.totalCount > 0 && (
85 <>
86 <FlexItem>
87 <SecondaryButton
88 dataCy={"settings-button-delete-" + props.name}
89 classname="pf-v6-u-mr-sm"
90 isDisabled={props.isDeleteDisabled || false}
91 onClickHandler={props.onDeleteModal}
92 >
93 Delete
94 </SecondaryButton>
95 <SecondaryButton
96 dataCy={"settings-button-add-" + props.name}
97 classname="pf-v6-u-mr-sm"
98 isDisabled={props.isAddDisabled || false}
99 onClickHandler={props.onAddModal}
100 id={addButtonId}
101 >
102 Add {props.entryType.toLowerCase()}s
103 </SecondaryButton>
104 </FlexItem>
105 <FlexItem align={{ default: "alignRight" }}>
106 <PaginationLayout
107 list={props.list}
108 paginationData={props.paginationData}
109 widgetId="pagination-options-menu-bottom"
110 className="pf-v6-u-pb-0 pf-v6-u-pr-md"
111 perPageSize="sm"
112 />
113 </FlexItem>
114 </>
115 )}
116 </Flex>
117 {props.paginationData.totalCount > 0 ? (
118 <>
119 <TableLayout
120 ariaLabel={props.ariaLabel}
121 name={props.name}
122 variant={props.variant}
123 hasBorders={props.hasBorders}
124 classes={props.tableClasses}
125 tableId={props.tableId}
126 isStickyHeader={props.isStickyHeader}
127 tableHeader={props.tableHeader}
128 tableBody={props.tableBody}
129 />
130 </>
131 ) : (
132 <EmptyState
133 headingLevel="h6"
134 titleText={
135 "No " +
136 props.entryType.toLowerCase() +
137 "s " +
138 (entryCount > 0 ? " found" : "")
139 }
140 variant={EmptyStateVariant.xs}
141 >
142 <EmptyStateBody>
143 <EmptyStateActions>
144 <SecondaryButton
145 dataCy={"settings-button-add-" + props.name}
146 onClickHandler={props.onAddModal}
147 isDisabled={props.isAddDisabled || false}
148 id={addButtonId}
149 >
150 Add {props.entryType.toLowerCase()}s
151 </SecondaryButton>
152 </EmptyStateActions>
153 </EmptyStateBody>
154 </EmptyState>
155 )}
156 </>
157 );
158 };
159
160 export default SettingsTableLayout;