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