| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.Th = 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 react_styles_1 = require("@patternfly/react-styles"); |
| 8 |
const table_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Table/table")); |
| 9 |
const table_scrollable_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Table/table-scrollable")); |
| 10 |
const accessibility_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/utilities/Accessibility/accessibility")); |
| 11 |
const utils_1 = require("./utils"); |
| 12 |
const merge_props_1 = require("./base/merge-props"); |
| 13 |
const Tooltip_1 = require('@patternfly/react-core/dist/js/components/Tooltip'); |
| 14 |
const Table_1 = require("./Table"); |
| 15 |
const c_table__sticky_cell_MinWidth_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_table__sticky_cell_MinWidth')); |
| 16 |
const c_table__sticky_cell_InsetInlineStart_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_table__sticky_cell_InsetInlineStart')); |
| 17 |
const c_table__sticky_cell_InsetInlineEnd_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_table__sticky_cell_InsetInlineEnd')); |
| 18 |
const ThBase = (_a) => { |
| 19 |
var _b; |
| 20 |
var { children, className, component = 'th', dataLabel, scope = 'col', textCenter = false, sort = null, modifier, select = null, expand: collapse = null, tooltip = '', tooltipProps, onMouseEnter: onMouseEnterProp = () => { }, width, visibility, innerRef, info: infoProps, isStickyColumn = false, hasRightBorder = false, hasLeftBorder = false, stickyMinWidth = '120px', stickyLeftOffset, stickyRightOffset, isSubheader = false, screenReaderText, 'aria-label': ariaLabel, additionalContent } = _a, props = tslib_1.__rest(_a, ["children", "className", "component", "dataLabel", "scope", "textCenter", "sort", "modifier", "select", "expand", "tooltip", "tooltipProps", "onMouseEnter", "width", "visibility", "innerRef", "info", "isStickyColumn", "hasRightBorder", "hasLeftBorder", "stickyMinWidth", "stickyLeftOffset", "stickyRightOffset", "isSubheader", "screenReaderText", 'aria-label', "additionalContent"]); |
| 21 |
const { variant } = (0, react_1.useContext)(Table_1.TableContext); |
| 22 |
if (!children && !screenReaderText && !ariaLabel) { |
| 23 |
// eslint-disable-next-line no-console |
| 24 |
console.warn('Th: Table headers must have an accessible name. If the Th is intended to be visually empty, pass in screenReaderText. If the Th contains only non-text, interactive content such as a checkbox or expand toggle, pass in an aria-label.'); |
| 25 |
} |
| 26 |
const [showTooltip, setShowTooltip] = (0, react_1.useState)(false); |
| 27 |
const [truncated, setTruncated] = (0, react_1.useState)(false); |
| 28 |
const cellRef = innerRef ? innerRef : (0, react_1.createRef)(); |
| 29 |
const onMouseEnter = (event) => { |
| 30 |
if (event.target.offsetWidth < event.target.scrollWidth) { |
| 31 |
!showTooltip && setShowTooltip(true); |
| 32 |
} |
| 33 |
else { |
| 34 |
showTooltip && setShowTooltip(false); |
| 35 |
} |
| 36 |
onMouseEnterProp(event); |
| 37 |
}; |
| 38 |
let sortParams = null; |
| 39 |
if (sort) { |
| 40 |
if (sort.isFavorites) { |
| 41 |
sortParams = (0, utils_1.sortableFavorites)({ |
| 42 |
onSort: sort.onSort, |
| 43 |
columnIndex: sort.columnIndex, |
| 44 |
sortBy: sort.sortBy, |
| 45 |
tooltip: tooltip, |
| 46 |
tooltipProps, |
| 47 |
ariaLabel: sort['aria-label'], |
| 48 |
favoriteButtonProps: sort.favoriteButtonProps |
| 49 |
})(); |
| 50 |
} |
| 51 |
else { |
| 52 |
sortParams = (0, utils_1.sortable)(children, { |
| 53 |
columnIndex: sort.columnIndex, |
| 54 |
column: { |
| 55 |
extraParams: { |
| 56 |
sortBy: sort.sortBy, |
| 57 |
onSort: sort === null || sort === void 0 ? void 0 : sort.onSort |
| 58 |
} |
| 59 |
}, |
| 60 |
tooltip: tooltip, |
| 61 |
tooltipProps |
| 62 |
}); |
| 63 |
} |
| 64 |
} |
| 65 |
const selectParams = select |
| 66 |
? (0, utils_1.selectable)(children, { |
| 67 |
rowData: { |
| 68 |
selected: select.isSelected, |
| 69 |
disableSelection: select === null || select === void 0 ? void 0 : select.isDisabled, |
| 70 |
props: select === null || select === void 0 ? void 0 : select.props |
| 71 |
}, |
| 72 |
column: { |
| 73 |
extraParams: { |
| 74 |
onSelect: select === null || select === void 0 ? void 0 : select.onSelect, |
| 75 |
selectVariant: 'checkbox', |
| 76 |
allRowsSelected: select.isSelected, |
| 77 |
isHeaderSelectDisabled: !!select.isHeaderSelectDisabled, |
| 78 |
isIndeterminate: select === null || select === void 0 ? void 0 : select.isIndeterminate |
| 79 |
} |
| 80 |
}, |
| 81 |
tooltip: tooltip, |
| 82 |
tooltipProps |
| 83 |
}) |
| 84 |
: null; |
| 85 |
const collapseParams = collapse |
| 86 |
? (0, utils_1.collapsible)(children, { |
| 87 |
column: { |
| 88 |
extraParams: { |
| 89 |
onCollapse: collapse === null || collapse === void 0 ? void 0 : collapse.onToggle, |
| 90 |
allRowsExpanded: !collapse.areAllExpanded, |
| 91 |
collapseAllAriaLabel: '', |
| 92 |
variant |
| 93 |
} |
| 94 |
} |
| 95 |
}) |
| 96 |
: null; |
| 97 |
const widthParams = width ? (0, utils_1.cellWidth)(width)() : null; |
| 98 |
const visibilityParams = visibility |
| 99 |
? (0, utils_1.classNames)(...visibility.map((vis) => utils_1.Visibility[vis]))() |
| 100 |
: null; |
| 101 |
let transformedChildren = (sortParams === null || sortParams === void 0 ? void 0 : sortParams.children) || (selectParams === null || selectParams === void 0 ? void 0 : selectParams.children) || (collapseParams === null || collapseParams === void 0 ? void 0 : collapseParams.children) || children; |
| 102 |
// info can wrap other transformedChildren |
| 103 |
let infoParams = null; |
| 104 |
if (infoProps) { |
| 105 |
infoParams = (0, utils_1.info)(infoProps)(transformedChildren); |
| 106 |
transformedChildren = infoParams.children; |
| 107 |
} |
| 108 |
const merged = (0, merge_props_1.mergeProps)(sortParams, selectParams, collapseParams, widthParams, visibilityParams, infoParams); |
| 109 |
const { |
| 110 |
// ignore the merged children since we transform them ourselves so we can wrap it with info |
| 111 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 112 |
children: mergedChildren = null, |
| 113 |
// selectable adds this but we don't want it |
| 114 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 115 |
isVisible = null, className: mergedClassName = '', component: MergedComponent = component } = merged, mergedProps = tslib_1.__rest(merged, ["children", "isVisible", "className", "component"]); |
| 116 |
(0, react_1.useEffect)(() => { |
| 117 |
setTruncated(cellRef.current.offsetWidth < |
| 118 |
cellRef.current.scrollWidth); |
| 119 |
}, [cellRef]); |
| 120 |
const cell = ((0, jsx_runtime_1.jsxs)(MergedComponent, Object.assign({ tabIndex: sort || select || !truncated ? -1 : 0, onFocus: tooltip !== null ? onMouseEnter : onMouseEnterProp, onBlur: () => setShowTooltip(false), "data-label": dataLabel, onMouseEnter: tooltip !== null ? onMouseEnter : onMouseEnterProp, scope: component === 'th' ? scope : null, ref: cellRef, "aria-label": ariaLabel, className: (0, react_styles_1.css)(table_1.default.tableTh, className, textCenter && table_1.default.modifiers.center, isSubheader && table_1.default.tableSubhead, isStickyColumn && table_scrollable_1.default.tableStickyCell, hasRightBorder && table_scrollable_1.default.modifiers.borderRight, hasLeftBorder && table_scrollable_1.default.modifiers.borderLeft, modifier && table_1.default.modifiers[modifier], ((_b = sort === null || sort === void 0 ? void 0 : sort.favoriteButtonProps) === null || _b === void 0 ? void 0 : _b.favorited) && table_1.default.modifiers.favorited, mergedClassName) }, mergedProps, props, (isStickyColumn && { |
| 121 |
style: Object.assign({ [c_table__sticky_cell_MinWidth_1.default.name]: stickyMinWidth ? stickyMinWidth : undefined, [c_table__sticky_cell_InsetInlineStart_1.default.name]: stickyLeftOffset ? stickyLeftOffset : 0, [c_table__sticky_cell_InsetInlineEnd_1.default.name]: stickyRightOffset ? stickyRightOffset : 0 }, props.style) |
| 122 |
}), { children: [transformedChildren || |
| 123 |
(screenReaderText && (0, jsx_runtime_1.jsx)("span", { className: accessibility_1.default.screenReader, children: screenReaderText })), additionalContent && additionalContent] }))); |
| 124 |
const canMakeDefaultTooltip = tooltip === '' ? typeof transformedChildren === 'string' : true; |
| 125 |
const childControlsTooltip = sortParams || selectParams; |
| 126 |
return tooltip !== null && canMakeDefaultTooltip && !childControlsTooltip && showTooltip ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [cell, (0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, Object.assign({ triggerRef: cellRef, content: tooltip || (tooltip === '' && children), isVisible: true }, tooltipProps))] })) : (cell); |
| 127 |
}; |
| 128 |
exports.Th = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(ThBase, Object.assign({}, props, { innerRef: ref })))); |
| 129 |
exports.Th.displayName = 'Th'; |
| 130 |
//# sourceMappingURL=Th.js.map |