Debian Codegraph @

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

raw
1 import { Component, Fragment } from 'react';
2 import { Body as BaseBody } from './base';
3 import { RowType, RowKeyType } from '../../../components/Table/base/types';
4 import { IRow, IRowCell, IExtraRowData, isRowExpanded } from '../../../components';
5 import { TableContext } from './TableContext';
6
7 export interface IComputedData {
8 isInput: boolean;
9 isButton: boolean;
10 }
11
12 export type OnRowClick = (
13 event: React.KeyboardEvent | React.MouseEvent,
14 row: IRow,
15 rowProps: IExtraRowData,
16 computedData: IComputedData
17 ) => void;
18
19 export interface TableBodyProps {
20 /** Additional classes added to the TableBody */
21 className?: string;
22 /** @hide This prop should not be set manually */
23 children?: React.ReactNode;
24 /** @hide This prop should not be set manually */
25 headerData?: IRow[];
26 /** @hide This prop should not be set manually */
27 rows?: IRow[];
28 /** @hide This prop should not be set manually */
29 rowKey?: RowKeyType;
30 /** A click handler for the row */
31 onRowClick?: OnRowClick;
32 /** @hide This prop should not be set manually */
33 onRow?: Function;
34 /** Flag indicating the <tbody> contains oddly striped rows. */
35 isOddStriped?: boolean;
36 /** Flag indicating the <tbody> contains evenly striped rows. */
37 isEvenStriped?: boolean;
38 }
39
40 const flagVisibility = (rows: IRow[]) => {
41 const visibleRows = (rows as IRow[]).filter((oneRow: IRow) => !oneRow.parent || oneRow.isExpanded) as IRow[];
42 if (visibleRows.length > 0) {
43 visibleRows[0].isFirstVisible = true;
44 visibleRows[visibleRows.length - 1].isLastVisible = true;
45 }
46 };
47
48 interface IMappedCell {
49 [name: string]: IRowCell;
50 }
51
52 class ContextBody extends Component<TableBodyProps, {}> {
53 onRow = (row: IRow, rowProps: any) => {
54 const { onRowClick, onRow } = this.props;
55 const extendedRowProps = {
56 ...rowProps,
57 ...(onRow ? onRow(row, rowProps) : {})
58 };
59 return {
60 row,
61 rowProps: extendedRowProps,
62 onClick: (event: React.MouseEvent) => {
63 const tagName = (event.target as HTMLElement).tagName;
64 const computedData = {
65 isInput: tagName === 'INPUT',
66 isButton: tagName === 'BUTTON'
67 };
68
69 onRowClick(event, row, rowProps, computedData);
70 },
71 onKeyDown: (event: React.KeyboardEvent) => {
72 const targetElement = event.target as HTMLElement;
73 const tagName = targetElement.tagName;
74 const computedData = {
75 isInput: tagName === 'INPUT',
76 isButton: tagName === 'BUTTON'
77 };
78 if (event.key === 'Enter' || event.key === ' ') {
79 onRowClick(event, row, rowProps, computedData);
80
81 // prevent event default if space is typed while focusing on a hoverable row
82 // so that the page does not scroll when trying to use spacebar to select a row
83 if (event.key === ' ' && !!targetElement.closest('.pf-m-hoverable')) {
84 event.preventDefault();
85 }
86 }
87 }
88 };
89 };
90
91 mapCells = (headerData: IRow[], row: IRow, rowKey: number) => {
92 // column indexes start after generated optional columns like collapsible or select column(s)
93 const { firstUserColumnIndex } = headerData[0].extraParams;
94 const isFullWidth = row && row.fullWidth;
95 // typically you'd want to map each cell to its column header, but in the case of fullWidth
96 // the first column could be the Select and/or Expandable column
97 let additionalColsIndexShift = isFullWidth ? 0 : firstUserColumnIndex;
98 return {
99 ...(row &&
100 (row.cells || row).reduce(
101 (acc: object, cell: IRowCell, cellIndex: number) => {
102 const isCellObject = cell === Object(cell);
103 const isCellFunction = cell && typeof cell.title === 'function';
104 let formatters: any = [];
105 if (isCellObject && cell.formatters) {
106 // give priority to formatters specified on the cell object
107 // expandable example:
108 // rows: [{ parent: 0, fullWidth: true, cells: [{ title: 'fullWidth, child - a', formatters: [expandable]}] }]
109 formatters = cell.formatters;
110 } else if (isFullWidth && cellIndex < firstUserColumnIndex) {
111 // for backwards compatibility, map the cells that are not under user columns (like Select/Expandable)
112 // to the first user column's header formatters
113 formatters = headerData[firstUserColumnIndex].cell.formatters;
114 }
115 let mappedCellTitle: IRowCell | Function | IRowCell['title'] = cell;
116 if (isCellObject && isCellFunction) {
117 mappedCellTitle = (cell.title as Function)(cell.props.value, rowKey, cellIndex, cell.props);
118 } else if (isCellObject) {
119 mappedCellTitle = cell.title;
120 }
121 const mappedCell: IMappedCell = {
122 [headerData[cellIndex + additionalColsIndexShift].property]: {
123 title: mappedCellTitle as React.ReactNode,
124 formatters,
125 props: {
126 isVisible: true,
127 ...(isCellObject ? cell.props : null)
128 }
129 }
130 };
131
132 // increment the shift index when a cell spans multiple columns
133 if (isCellObject && cell.props && cell.props.colSpan) {
134 additionalColsIndexShift += cell.props.colSpan - 1;
135 }
136 return {
137 ...acc,
138 ...mappedCell
139 };
140 },
141 { secretTableRowKeyId: row.id !== undefined ? row.id : rowKey }
142 ))
143 };
144 };
145
146 render() {
147 // eslint-disable-next-line @typescript-eslint/no-unused-vars
148 const { className, headerData, rows, rowKey, children, onRowClick, ...props } = this.props;
149
150 let mappedRows: IRow[];
151 if (headerData.length > 0) {
152 mappedRows = (rows as IRow[]).map((oneRow: IRow, oneRowKey: number) => ({
153 ...oneRow,
154 ...this.mapCells(headerData, oneRow, oneRowKey),
155 isExpanded: isRowExpanded(oneRow, rows),
156 isHeightAuto: oneRow.heightAuto || false,
157 isFirst: oneRowKey === 0,
158 isLast: oneRowKey === rows.length - 1,
159 isFirstVisible: false,
160 isLastVisible: false
161 })) as IRow[];
162 flagVisibility(mappedRows);
163 }
164
165 return (
166 <Fragment>
167 {mappedRows && (
168 <BaseBody
169 {...props}
170 mappedRows={mappedRows}
171 rows={mappedRows as RowType[]}
172 onRow={this.onRow}
173 rowKey={rowKey}
174 className={className}
175 />
176 )}
177 </Fragment>
178 );
179 }
180 }
181
182 export const TableBody = ({
183 className = '' as string,
184 children = null as React.ReactNode,
185 rowKey = 'secretTableRowKeyId' as string,
186 /* eslint-disable @typescript-eslint/no-unused-vars */
187 onRow = (...args: any) => ({}),
188 onRowClick = (
189 event: React.MouseEvent | React.KeyboardEvent,
190 row: IRow,
191 rowProps: IExtraRowData,
192 computedData: IComputedData
193 ) =>
194 /* eslint-enable @typescript-eslint/no-unused-vars */
195 undefined as OnRowClick,
196 ...props
197 }: TableBodyProps) => (
198 <TableContext.Consumer>
199 {({ headerData = [], rows = [], ...rest }) => (
200 <ContextBody
201 headerData={headerData}
202 rows={rows as IRow[]}
203 onRow={onRow}
204 className={className}
205 rowKey={rowKey}
206 onRowClick={onRowClick}
207 {...props}
208 {...rest}
209 >
210 {children}
211 </ContextBody>
212 )}
213 </TableContext.Consumer>
214 );