| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { createRef, forwardRef, useEffect, useState, useContext } from 'react'; |
| 4 |
import { css } from '@patternfly/react-styles'; |
| 5 |
import styles from '@patternfly/react-styles/css/components/Table/table.mjs'; |
| 6 |
import scrollStyles from '@patternfly/react-styles/css/components/Table/table-scrollable.mjs'; |
| 7 |
import treeViewStyles from '@patternfly/react-styles/css/components/Table/table-tree-view.mjs'; |
| 8 |
import { cellActions, selectable, collapsible, compoundExpand, cellWidth, Visibility, classNames, favoritable } from './utils'; |
| 9 |
import { draggable } from './utils/decorators/draggable'; |
| 10 |
import { treeRow } from './utils'; |
| 11 |
import { mergeProps } from './base/merge-props'; |
| 12 |
import { Tooltip } from '@patternfly/react-core/dist/esm/components/Tooltip/Tooltip'; |
| 13 |
import { TableContext } from './Table'; |
| 14 |
import cssStickyCellMinWidth from '@patternfly/react-tokens/dist/esm/c_table__sticky_cell_MinWidth'; |
| 15 |
import cssStickyCellInlineStart from '@patternfly/react-tokens/dist/esm/c_table__sticky_cell_InsetInlineStart'; |
| 16 |
import cssStickyCellInlineEnd from '@patternfly/react-tokens/dist/esm/c_table__sticky_cell_InsetInlineEnd'; |
| 17 |
const TdBase = (_a) => { |
| 18 |
var { children, className, isActionCell = false, hasAction = false, component = 'td', dataLabel, textCenter = false, modifier, select = null, actions = null, expand = null, treeRow: treeRowProp = null, compoundExpand: compoundExpandProp = null, noPadding, width, visibility, innerRef, favorites = null, draggableRow: draggableRowProp = null, tooltip = '', onMouseEnter: onMouseEnterProp = () => { }, isStickyColumn = false, hasRightBorder = false, hasLeftBorder = false, stickyMinWidth = '120px', stickyLeftOffset, stickyRightOffset } = _a, props = __rest(_a, ["children", "className", "isActionCell", "hasAction", "component", "dataLabel", "textCenter", "modifier", "select", "actions", "expand", "treeRow", "compoundExpand", "noPadding", "width", "visibility", "innerRef", "favorites", "draggableRow", "tooltip", "onMouseEnter", "isStickyColumn", "hasRightBorder", "hasLeftBorder", "stickyMinWidth", "stickyLeftOffset", "stickyRightOffset"]); |
| 19 |
const [showTooltip, setShowTooltip] = useState(false); |
| 20 |
const [truncated, setTruncated] = useState(false); |
| 21 |
const cellRef = innerRef ? innerRef : createRef(); |
| 22 |
const onMouseEnter = (event) => { |
| 23 |
if (event.target.offsetWidth < event.target.scrollWidth) { |
| 24 |
!showTooltip && setShowTooltip(true); |
| 25 |
} |
| 26 |
else { |
| 27 |
showTooltip && setShowTooltip(false); |
| 28 |
} |
| 29 |
onMouseEnterProp(event); |
| 30 |
}; |
| 31 |
const selectParams = select |
| 32 |
? selectable(children, { |
| 33 |
rowIndex: select.rowIndex, |
| 34 |
rowData: { |
| 35 |
selected: select.isSelected, |
| 36 |
disableSelection: select === null || select === void 0 ? void 0 : select.isDisabled, |
| 37 |
props: select === null || select === void 0 ? void 0 : select.props |
| 38 |
}, |
| 39 |
column: { |
| 40 |
extraParams: { |
| 41 |
onSelect: select === null || select === void 0 ? void 0 : select.onSelect, |
| 42 |
selectVariant: select.variant || 'checkbox' |
| 43 |
} |
| 44 |
} |
| 45 |
}) |
| 46 |
: null; |
| 47 |
const favoriteParams = favorites |
| 48 |
? favoritable(null, { |
| 49 |
rowIndex: favorites === null || favorites === void 0 ? void 0 : favorites.rowIndex, |
| 50 |
rowData: { |
| 51 |
favorited: favorites.isFavorited, |
| 52 |
favoritesProps: favorites === null || favorites === void 0 ? void 0 : favorites.props |
| 53 |
}, |
| 54 |
column: { |
| 55 |
extraParams: { |
| 56 |
onFavorite: favorites === null || favorites === void 0 ? void 0 : favorites.onFavorite |
| 57 |
} |
| 58 |
} |
| 59 |
}) |
| 60 |
: null; |
| 61 |
const draggableParams = draggableRowProp !== null |
| 62 |
? draggable(null, { |
| 63 |
rowData: { |
| 64 |
id: draggableRowProp.id |
| 65 |
} |
| 66 |
}) |
| 67 |
: null; |
| 68 |
const actionParamsFunc = actions ? cellActions(actions.items, null, null) : null; |
| 69 |
const actionParams = actionParamsFunc |
| 70 |
? actionParamsFunc(null, { |
| 71 |
rowIndex: actions === null || actions === void 0 ? void 0 : actions.rowIndex, |
| 72 |
rowData: { |
| 73 |
disableActions: actions === null || actions === void 0 ? void 0 : actions.isDisabled |
| 74 |
}, |
| 75 |
column: { |
| 76 |
extraParams: { |
| 77 |
dropdownPosition: actions === null || actions === void 0 ? void 0 : actions.dropdownPosition, |
| 78 |
dropdownDirection: actions === null || actions === void 0 ? void 0 : actions.dropdownDirection, |
| 79 |
menuAppendTo: actions === null || actions === void 0 ? void 0 : actions.menuAppendTo, |
| 80 |
actionsToggle: actions === null || actions === void 0 ? void 0 : actions.actionsToggle |
| 81 |
} |
| 82 |
} |
| 83 |
}) |
| 84 |
: null; |
| 85 |
const { hasAnimations, variant } = useContext(TableContext); |
| 86 |
const expandableParams = expand !== null |
| 87 |
? collapsible(null, { |
| 88 |
rowIndex: expand.rowIndex, |
| 89 |
columnIndex: expand === null || expand === void 0 ? void 0 : expand.columnIndex, |
| 90 |
rowData: { |
| 91 |
isOpen: expand.isExpanded |
| 92 |
}, |
| 93 |
column: { |
| 94 |
extraParams: { |
| 95 |
onCollapse: expand === null || expand === void 0 ? void 0 : expand.onToggle, |
| 96 |
expandId: expand === null || expand === void 0 ? void 0 : expand.expandId, |
| 97 |
variant |
| 98 |
} |
| 99 |
} |
| 100 |
}) |
| 101 |
: null; |
| 102 |
const updateAnimationClass = () => { |
| 103 |
var _a, _b; |
| 104 |
const ancestorControlRow = (_a = cellRef === null || cellRef === void 0 ? void 0 : cellRef.current) === null || _a === void 0 ? void 0 : _a.closest(`.${styles.tableTr}.${styles.tableControlRow}`); |
| 105 |
const isControlRowExpanded = ancestorControlRow.classList.contains(styles.modifiers.expanded); |
| 106 |
if (!isControlRowExpanded) { |
| 107 |
return; |
| 108 |
} |
| 109 |
const isCurrentCellExpanded = (_b = cellRef === null || cellRef === void 0 ? void 0 : cellRef.current) === null || _b === void 0 ? void 0 : _b.classList.contains(styles.modifiers.expanded); |
| 110 |
if (isCurrentCellExpanded) { |
| 111 |
ancestorControlRow.classList.remove(styles.modifiers.noAnimateExpand); |
| 112 |
} |
| 113 |
else { |
| 114 |
ancestorControlRow.classList.add(styles.modifiers.noAnimateExpand); |
| 115 |
} |
| 116 |
}; |
| 117 |
const internalCompoundOnToggle = (event, rowIndex, colIndex, isOpen, rowData, extraData) => { |
| 118 |
hasAnimations && updateAnimationClass(); |
| 119 |
compoundExpandProp === null || compoundExpandProp === void 0 ? void 0 : compoundExpandProp.onToggle(event, rowIndex, colIndex, isOpen, rowData, extraData); |
| 120 |
}; |
| 121 |
const compoundParams = compoundExpandProp !== null |
| 122 |
? compoundExpand({ |
| 123 |
title: children, |
| 124 |
props: { |
| 125 |
isOpen: compoundExpandProp.isExpanded |
| 126 |
} |
| 127 |
}, { |
| 128 |
rowIndex: compoundExpandProp === null || compoundExpandProp === void 0 ? void 0 : compoundExpandProp.rowIndex, |
| 129 |
columnIndex: compoundExpandProp === null || compoundExpandProp === void 0 ? void 0 : compoundExpandProp.columnIndex, |
| 130 |
column: { |
| 131 |
extraParams: { |
| 132 |
onExpand: internalCompoundOnToggle, |
| 133 |
expandId: compoundExpandProp === null || compoundExpandProp === void 0 ? void 0 : compoundExpandProp.expandId |
| 134 |
} |
| 135 |
} |
| 136 |
}) |
| 137 |
: null; |
| 138 |
const widthParams = width ? cellWidth(width)() : null; |
| 139 |
const visibilityParams = visibility |
| 140 |
? classNames(...visibility.map((vis) => Visibility[vis]))() |
| 141 |
: null; |
| 142 |
const treeRowParams = treeRowProp !== null |
| 143 |
? treeRow(treeRowProp.onCollapse, treeRowProp.onCheckChange, treeRowProp.onToggleRowDetails)({ |
| 144 |
title: children |
| 145 |
}, { |
| 146 |
rowIndex: treeRowProp.rowIndex, |
| 147 |
rowData: { |
| 148 |
props: treeRowProp.props |
| 149 |
} |
| 150 |
}) |
| 151 |
: null; |
| 152 |
const merged = mergeProps(selectParams, actionParams, expandableParams, compoundParams, widthParams, visibilityParams, favoriteParams, treeRowParams, draggableParams); |
| 153 |
const { |
| 154 |
// selectable adds this but we don't want it |
| 155 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 156 |
isVisible = null, children: mergedChildren = null, className: mergedClassName = '', component: MergedComponent = component } = merged, mergedProps = __rest(merged, ["isVisible", "children", "className", "component"]); |
| 157 |
const treeTableTitleCell = (className && className.includes(treeViewStyles.tableTreeViewTitleCell)) || |
| 158 |
(mergedClassName && mergedClassName.includes(treeViewStyles.tableTreeViewTitleCell)); |
| 159 |
useEffect(() => { |
| 160 |
setTruncated(cellRef.current.offsetWidth < |
| 161 |
cellRef.current.scrollWidth); |
| 162 |
}, [cellRef]); |
| 163 |
const cell = (_jsx(MergedComponent, Object.assign({ tabIndex: (select || !truncated) && modifier !== 'truncate' ? -1 : 0 }, (!treeTableTitleCell && { 'data-label': dataLabel }), { onFocus: tooltip !== null ? onMouseEnter : onMouseEnterProp, onBlur: () => setShowTooltip(false), onMouseEnter: tooltip !== null ? onMouseEnter : onMouseEnterProp, className: css(styles.tableTd, className, isActionCell && styles.tableAction, hasAction && styles.modifiers.action, textCenter && styles.modifiers.center, noPadding && styles.modifiers.noPadding, isStickyColumn && scrollStyles.tableStickyCell, hasRightBorder && scrollStyles.modifiers.borderRight, hasLeftBorder && scrollStyles.modifiers.borderLeft, styles.modifiers[modifier], draggableParams && styles.tableDraggable, mergedClassName), ref: cellRef }, mergedProps, props, (isStickyColumn && { |
| 164 |
style: Object.assign({ [cssStickyCellMinWidth.name]: stickyMinWidth ? stickyMinWidth : undefined, [cssStickyCellInlineStart.name]: stickyLeftOffset ? stickyLeftOffset : 0, [cssStickyCellInlineEnd.name]: stickyRightOffset ? stickyRightOffset : 0 }, props.style) |
| 165 |
}), { children: mergedChildren || children }))); |
| 166 |
const canMakeDefaultTooltip = tooltip === '' ? typeof children === 'string' : true; |
| 167 |
return tooltip !== null && canMakeDefaultTooltip && showTooltip ? (_jsxs(_Fragment, { children: [cell, _jsx(Tooltip, { triggerRef: cellRef, content: tooltip || (tooltip === '' && children), isVisible: true })] })) : (cell); |
| 168 |
}; |
| 169 |
export const Td = forwardRef((props, ref) => (_jsx(TdBase, Object.assign({}, props, { innerRef: ref })))); |
| 170 |
Td.displayName = 'Td'; |
| 171 |
//# sourceMappingURL=Td.js.map |