Debian Codegraph @

node/@patternfly/react-table/src/deprecated/components/Table/Table.tsx

raw
1 import { Component } from 'react';
2 import { OUIAProps, getDefaultOUIAId } from '@patternfly/react-core/dist/esm/helpers/OUIA/ouia';
3 import inlineStyles from '@patternfly/react-styles/css/components/InlineEdit/inline-edit';
4 import { css } from '@patternfly/react-styles';
5 import { Provider } from '../../../components/Table/base/provider';
6 import { BodyCell } from './BodyCell';
7 import { HeaderCell } from './HeaderCell';
8 import { RowWrapper, RowWrapperProps } from '../../../components';
9 import { BodyWrapper } from './BodyWrapper';
10 import { calculateColumns, RowSelectVariant } from '../../../components';
11 import { TableContext } from './TableContext';
12 import {
13 ISortBy,
14 OnCollapse,
15 OnExpand,
16 OnSelect,
17 OnRowEdit,
18 OnSort,
19 IActions,
20 IActionsResolver,
21 IAreActionsDisabled,
22 IRow,
23 ICell,
24 TableVariant,
25 TableGridBreakpoint,
26 IHeaderRow,
27 OnFavorite,
28 TreeRowWrapper,
29 CustomActionsToggleProps
30 } from '../../../components';
31
32 export interface TableProps extends OUIAProps {
33 /** Adds an accessible name for the Table */
34 'aria-label'?: string;
35 /** Content rendered inside the Table */
36 children?: React.ReactNode;
37 /** Additional classes added to the Table */
38 className?: string;
39 /** Style variant for the Table */
40 variant?: 'compact';
41 /**
42 * Render borders
43 * Borders can only currently be disabled if the variant is set to 'compact'
44 * https://github.com/patternfly/patternfly/issues/3650
45 */
46 borders?: boolean;
47 /** Specifies the grid breakpoints */
48 gridBreakPoint?: '' | 'grid' | 'grid-md' | 'grid-lg' | 'grid-xl' | 'grid-2xl';
49 /** Specifies the initial sorting pattern for the table - asc/desc and the index of the column to sort by */
50 sortBy?: ISortBy;
51 /** Function triggered when an expandable content is collapsed. When this is used, one expandable toggle button will be positioned in the first cell of a non-expandable row, preceding an expandable row */
52 onCollapse?: OnCollapse;
53 /** Function triggered when a compound expandable item is clicked */
54 onExpand?: OnExpand;
55 /** Function triggered when a row's checkbox is selected. When this is used, one checkbox/radio button will be positioned in the first or second cell of a non-expandable row */
56 onSelect?: OnSelect;
57 /** Enables or disables the ability to select all - this is mutually exclusive with radio button select variant */
58 canSelectAll?: boolean;
59 /** Enables or disables the ability to expand all */
60 canCollapseAll?: boolean;
61 /** Flag indicating the select all checkbox is disabled */
62 isHeaderSelectDisabled?: boolean;
63 /** Specifies the type of the select element variant - can be one of checkbox or radio button */
64 selectVariant?: 'checkbox' | 'radio';
65 /** An optional alternative aria label for the expand collapse all table header */
66 collapseAllAriaLabel?: string;
67 /** Function triggered when a row's inline edit is activated. Adds a column for inline edit when present. */
68 onRowEdit?: OnRowEdit;
69 /** Function triggered when sort icon is clicked */
70 onSort?: OnSort;
71 /** Actions to add to the Table */
72 actions?: IActions;
73 /** Resolver for the given action */
74 actionResolver?: IActionsResolver;
75 /** Specifies if the Kebab for actions is disabled */
76 areActionsDisabled?: IAreActionsDisabled;
77 /** Component to place in the header */
78 header?: React.ReactNode;
79 /** Component used for caption*/
80 caption?: React.ReactNode;
81 /** label for row */
82 rowLabeledBy?: string;
83 /** ID for expand */
84 expandId?: string;
85 /** ID for content */
86 contentId?: string;
87 /** The desired position to show the dropdown when clicking on the actions Kebab. Can only be used together with `actions` property */
88 dropdownPosition?: 'right' | 'left';
89 /** The desired direction to show the dropdown when clicking on the actions Kebab. Can only be used together with `actions` property */
90 dropdownDirection?: 'up' | 'down';
91 /** The container to append the dropdown menu to. Defaults to 'inline'.
92 * If your menu is being cut off you can append it to an element higher up the DOM tree.
93 * Some examples:
94 * actionsMenuAppendTo="parent"
95 * actionsMenuAppendTo={() => document.body}
96 * actionsMenuAppendTo={document.getElementById('target')}
97 */
98 actionsMenuAppendTo?: HTMLElement | (() => HTMLElement) | 'inline' | 'parent';
99 /** The toggle of the actions menu dropdown. A KebabToggle or DropdownToggle component */
100 actionsToggle?: (props: CustomActionsToggleProps) => React.ReactNode;
101 /** Row data */
102 rows: (IRow | string[])[];
103 /** Cell/column data */
104 cells: (ICell | string)[];
105 /** Wrapper for the body */
106 bodyWrapper?: Function;
107 /** Wrapper for the row */
108 rowWrapper?: (props: RowWrapperProps) => React.JSX.Element;
109 /** A valid WAI-ARIA role to be applied to the table element */
110 role?: string;
111 /** If set to true, the table header sticks to the top of its container */
112 isStickyHeader?: boolean;
113 /**
114 * Enables favorites column
115 * Callback triggered when a row is favorited/unfavorited
116 */
117 onFavorite?: OnFavorite;
118 /** Along with the onSort prop, enables favorites sorting, defaults to true */
119 canSortFavorites?: boolean;
120 /** Flag indicating table is a tree table */
121 isTreeTable?: boolean;
122 /** Flag indicating this table is nested within another table */
123 isNested?: boolean;
124 /** Flag indicating this table is striped */
125 isStriped?: boolean;
126 /** Flag indicating this table contains expandable rows to maintain proper striping */
127 isExpandable?: boolean;
128 /** Value to overwrite the randomly generated data-ouia-component-id.*/
129 ouiaId?: number | string;
130 /** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
131 ouiaSafe?: boolean;
132 }
133
134 class Table extends Component<TableProps, {}> {
135 static displayName = 'Table';
136 static hasWarnBeta = false;
137 static defaultProps: Partial<TableProps> = {
138 children: null as React.ReactNode,
139 className: '',
140 variant: null as TableVariant,
141 borders: true,
142 rowLabeledBy: 'simple-node',
143 expandId: 'expandable-toggle',
144 contentId: 'expanded-content',
145 dropdownPosition: 'right',
146 dropdownDirection: 'down',
147 actionsMenuAppendTo: 'inline',
148 header: undefined as React.ReactNode,
149 caption: undefined as React.ReactNode,
150 'aria-label': undefined as string,
151 gridBreakPoint: TableGridBreakpoint.gridMd,
152 role: 'grid',
153 canSelectAll: true,
154 canCollapseAll: false,
155 isHeaderSelectDisabled: false,
156 selectVariant: 'checkbox',
157 collapseAllAriaLabel: '',
158 ouiaSafe: true,
159 isStickyHeader: false,
160 canSortFavorites: true,
161 isTreeTable: false,
162 isNested: false
163 };
164 state = {
165 ouiaStateId: getDefaultOUIAId(Table.displayName)
166 };
167
168 isSelected = (row: IRow) => row.selected === true;
169
170 areAllRowsSelected = (rows: IRow[]) => {
171 if (rows === undefined || rows.length === 0) {
172 return false;
173 }
174 return rows.every(
175 (row) => this.isSelected(row) || row.disableSelection || (row.hasOwnProperty('parent') && !row.showSelect)
176 );
177 };
178
179 areAllExpanded = (rows: IRow[]) => {
180 if (rows === undefined || rows.length === 0) {
181 return false;
182 }
183 return rows.every((row) => row.isOpen === undefined || row.isOpen);
184 };
185
186 render() {
187 const {
188 'aria-label': ariaLabel,
189 caption,
190 header,
191 onSort,
192 onSelect,
193 canSelectAll,
194 canCollapseAll,
195 isHeaderSelectDisabled,
196 selectVariant,
197 collapseAllAriaLabel,
198 sortBy,
199 children,
200 actions,
201 actionResolver,
202 areActionsDisabled,
203 onCollapse,
204 onExpand,
205 onRowEdit,
206 rowLabeledBy,
207 dropdownPosition,
208 dropdownDirection,
209 actionsMenuAppendTo: menuAppendTo,
210 actionsToggle,
211 contentId,
212 expandId,
213 variant,
214 rows,
215 cells,
216 bodyWrapper,
217 rowWrapper,
218 role,
219 borders,
220 onFavorite,
221 canSortFavorites,
222 ...props
223 } = this.props;
224
225 if (!ariaLabel && !caption && !header && role !== 'presentation') {
226 // eslint-disable-next-line no-console
227 console.error('Table: Specify at least one of: header, caption, aria-label');
228 }
229
230 const headerData = calculateColumns(cells, {
231 sortBy,
232 onSort,
233 onSelect,
234 canSelectAll: selectVariant === RowSelectVariant.radio ? false : canSelectAll,
235 canCollapseAll,
236 isHeaderSelectDisabled,
237 selectVariant,
238 collapseAllAriaLabel,
239 allRowsSelected: onSelect ? this.areAllRowsSelected(rows as IRow[]) : false,
240 allRowsExpanded: onCollapse ? this.areAllExpanded(rows as IRow[]) : false,
241 actions,
242 actionResolver,
243 areActionsDisabled,
244 onCollapse,
245 onRowEdit,
246 onExpand,
247 rowLabeledBy,
248 expandId,
249 contentId,
250 dropdownPosition,
251 dropdownDirection,
252 menuAppendTo,
253 actionsToggle,
254 onFavorite,
255 canSortFavorites,
256 // order of columns: Collapsible | Selectable | Favoritable
257 firstUserColumnIndex: [onCollapse, onSelect, onFavorite].filter((callback) => callback).length
258 });
259
260 const table = (
261 <TableContext.Provider
262 value={{
263 headerData,
264 headerRows: null as IHeaderRow[],
265 rows
266 }}
267 >
268 {header}
269 <Provider
270 {...props}
271 aria-label={ariaLabel}
272 renderers={{
273 body: {
274 wrapper: bodyWrapper || BodyWrapper,
275 row: rowWrapper || (this.props.isTreeTable ? TreeRowWrapper : RowWrapper),
276 cell: BodyCell
277 },
278 header: {
279 cell: HeaderCell
280 }
281 }}
282 columns={headerData}
283 role={role}
284 variant={variant}
285 borders={borders}
286 >
287 {caption && <caption>{caption}</caption>}
288 {children}
289 </Provider>
290 </TableContext.Provider>
291 );
292
293 if (onRowEdit) {
294 return <form className={css(inlineStyles.inlineEdit)}>{table}</form>;
295 }
296
297 return table;
298 }
299 }
300
301 export { Table };