Debian Codegraph @

node/@patternfly/react-table/dist/js/deprecated/components/Table/Body.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.TableBody = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const base_1 = require("./base");
8 const components_1 = require("../../../components");
9 const TableContext_1 = require("./TableContext");
10 const flagVisibility = (rows) => {
11 const visibleRows = rows.filter((oneRow) => !oneRow.parent || oneRow.isExpanded);
12 if (visibleRows.length > 0) {
13 visibleRows[0].isFirstVisible = true;
14 visibleRows[visibleRows.length - 1].isLastVisible = true;
15 }
16 };
17 class ContextBody extends react_1.Component {
18 constructor() {
19 super(...arguments);
20 this.onRow = (row, rowProps) => {
21 const { onRowClick, onRow } = this.props;
22 const extendedRowProps = Object.assign(Object.assign({}, rowProps), (onRow ? onRow(row, rowProps) : {}));
23 return {
24 row,
25 rowProps: extendedRowProps,
26 onClick: (event) => {
27 const tagName = event.target.tagName;
28 const computedData = {
29 isInput: tagName === 'INPUT',
30 isButton: tagName === 'BUTTON'
31 };
32 onRowClick(event, row, rowProps, computedData);
33 },
34 onKeyDown: (event) => {
35 const targetElement = event.target;
36 const tagName = targetElement.tagName;
37 const computedData = {
38 isInput: tagName === 'INPUT',
39 isButton: tagName === 'BUTTON'
40 };
41 if (event.key === 'Enter' || event.key === ' ') {
42 onRowClick(event, row, rowProps, computedData);
43 // prevent event default if space is typed while focusing on a hoverable row
44 // so that the page does not scroll when trying to use spacebar to select a row
45 if (event.key === ' ' && !!targetElement.closest('.pf-m-hoverable')) {
46 event.preventDefault();
47 }
48 }
49 }
50 };
51 };
52 this.mapCells = (headerData, row, rowKey) => {
53 // column indexes start after generated optional columns like collapsible or select column(s)
54 const { firstUserColumnIndex } = headerData[0].extraParams;
55 const isFullWidth = row && row.fullWidth;
56 // typically you'd want to map each cell to its column header, but in the case of fullWidth
57 // the first column could be the Select and/or Expandable column
58 let additionalColsIndexShift = isFullWidth ? 0 : firstUserColumnIndex;
59 return Object.assign({}, (row &&
60 (row.cells || row).reduce((acc, cell, cellIndex) => {
61 const isCellObject = cell === Object(cell);
62 const isCellFunction = cell && typeof cell.title === 'function';
63 let formatters = [];
64 if (isCellObject && cell.formatters) {
65 // give priority to formatters specified on the cell object
66 // expandable example:
67 // rows: [{ parent: 0, fullWidth: true, cells: [{ title: 'fullWidth, child - a', formatters: [expandable]}] }]
68 formatters = cell.formatters;
69 }
70 else if (isFullWidth && cellIndex < firstUserColumnIndex) {
71 // for backwards compatibility, map the cells that are not under user columns (like Select/Expandable)
72 // to the first user column's header formatters
73 formatters = headerData[firstUserColumnIndex].cell.formatters;
74 }
75 let mappedCellTitle = cell;
76 if (isCellObject && isCellFunction) {
77 mappedCellTitle = cell.title(cell.props.value, rowKey, cellIndex, cell.props);
78 }
79 else if (isCellObject) {
80 mappedCellTitle = cell.title;
81 }
82 const mappedCell = {
83 [headerData[cellIndex + additionalColsIndexShift].property]: {
84 title: mappedCellTitle,
85 formatters,
86 props: Object.assign({ isVisible: true }, (isCellObject ? cell.props : null))
87 }
88 };
89 // increment the shift index when a cell spans multiple columns
90 if (isCellObject && cell.props && cell.props.colSpan) {
91 additionalColsIndexShift += cell.props.colSpan - 1;
92 }
93 return Object.assign(Object.assign({}, acc), mappedCell);
94 }, { secretTableRowKeyId: row.id !== undefined ? row.id : rowKey })));
95 };
96 }
97 render() {
98 // eslint-disable-next-line @typescript-eslint/no-unused-vars
99 const _a = this.props, { className, headerData, rows, rowKey, children, onRowClick } = _a, props = tslib_1.__rest(_a, ["className", "headerData", "rows", "rowKey", "children", "onRowClick"]);
100 let mappedRows;
101 if (headerData.length > 0) {
102 mappedRows = rows.map((oneRow, oneRowKey) => (Object.assign(Object.assign(Object.assign({}, oneRow), this.mapCells(headerData, oneRow, oneRowKey)), { isExpanded: (0, components_1.isRowExpanded)(oneRow, rows), isHeightAuto: oneRow.heightAuto || false, isFirst: oneRowKey === 0, isLast: oneRowKey === rows.length - 1, isFirstVisible: false, isLastVisible: false })));
103 flagVisibility(mappedRows);
104 }
105 return ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: mappedRows && ((0, jsx_runtime_1.jsx)(base_1.Body, Object.assign({}, props, { mappedRows: mappedRows, rows: mappedRows, onRow: this.onRow, rowKey: rowKey, className: className }))) }));
106 }
107 }
108 const TableBody = (_a) => {
109 var { className = '', children = null, rowKey = 'secretTableRowKeyId',
110 /* eslint-disable @typescript-eslint/no-unused-vars */
111 onRow = (...args) => ({}), onRowClick = (event, row, rowProps, computedData) =>
112 /* eslint-enable @typescript-eslint/no-unused-vars */
113 undefined } = _a, props = tslib_1.__rest(_a, ["className", "children", "rowKey", "onRow", "onRowClick"]);
114 return ((0, jsx_runtime_1.jsx)(TableContext_1.TableContext.Consumer, { children: (_a) => {
115 var { headerData = [], rows = [] } = _a, rest = tslib_1.__rest(_a, ["headerData", "rows"]);
116 return ((0, jsx_runtime_1.jsx)(ContextBody, Object.assign({ headerData: headerData, rows: rows, onRow: onRow, className: className, rowKey: rowKey, onRowClick: onRowClick }, props, rest, { children: children })));
117 } }));
118 };
119 exports.TableBody = TableBody;
120 //# sourceMappingURL=Body.js.map