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