| 1 |
import { createRef, forwardRef, useEffect, useState, useContext } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Table/table'; |
| 4 |
import scrollStyles from '@patternfly/react-styles/css/components/Table/table-scrollable'; |
| 5 |
import treeViewStyles from '@patternfly/react-styles/css/components/Table/table-tree-view'; |
| 6 |
import { BaseCellProps } from './Table'; |
| 7 |
import { |
| 8 |
cellActions, |
| 9 |
selectable, |
| 10 |
collapsible, |
| 11 |
compoundExpand, |
| 12 |
cellWidth, |
| 13 |
Visibility, |
| 14 |
classNames, |
| 15 |
favoritable |
| 16 |
} from './utils'; |
| 17 |
import { IRowData, IExtraData } from './TableTypes'; |
| 18 |
import { draggable } from './utils/decorators/draggable'; |
| 19 |
import { treeRow } from './utils'; |
| 20 |
import { mergeProps } from './base/merge-props'; |
| 21 |
import { IVisibility } from './utils/decorators/classNames'; |
| 22 |
import { Tooltip } from '@patternfly/react-core/dist/esm/components/Tooltip/Tooltip'; |
| 23 |
import { IFormatterValueType, IExtra } from './TableTypes'; |
| 24 |
import { |
| 25 |
TdActionsType, |
| 26 |
TdCompoundExpandType, |
| 27 |
TdDraggableType, |
| 28 |
TdExpandType, |
| 29 |
TdFavoritesType, |
| 30 |
TdSelectType, |
| 31 |
TdTreeRowType |
| 32 |
} from './base/types'; |
| 33 |
import { TableContext } from './Table'; |
| 34 |
import cssStickyCellMinWidth from '@patternfly/react-tokens/dist/esm/c_table__sticky_cell_MinWidth'; |
| 35 |
import cssStickyCellInlineStart from '@patternfly/react-tokens/dist/esm/c_table__sticky_cell_InsetInlineStart'; |
| 36 |
import cssStickyCellInlineEnd from '@patternfly/react-tokens/dist/esm/c_table__sticky_cell_InsetInlineEnd'; |
| 37 |
|
| 38 |
export interface TdProps extends BaseCellProps, Omit<React.HTMLProps<HTMLTableDataCellElement>, 'onSelect' | 'width'> { |
| 39 |
/** |
| 40 |
* The column header the cell corresponds to. |
| 41 |
* This attribute replaces table header in mobile viewport. It is rendered by ::before pseudo element. |
| 42 |
*/ |
| 43 |
dataLabel?: string; |
| 44 |
/** Renders a checkbox or radio select */ |
| 45 |
select?: TdSelectType; |
| 46 |
/** Turns the cell into an actions cell. Recommended to use an ActionsColumn component as a child of the Td rather than this prop. */ |
| 47 |
actions?: TdActionsType; |
| 48 |
/** Indicates the cell contains an interactive element and prevents that element's padding from increasing row height. Recommended when other cells in the same row contains text. */ |
| 49 |
hasAction?: boolean; |
| 50 |
/** Turns the cell into an expansion toggle and determines if the corresponding expansion row is open */ |
| 51 |
expand?: TdExpandType; |
| 52 |
/** Turns the cell into a compound expansion toggle */ |
| 53 |
compoundExpand?: TdCompoundExpandType; |
| 54 |
/** Turns the cell into a favorites cell with a star button */ |
| 55 |
favorites?: TdFavoritesType; |
| 56 |
/** Turns the cell into the first cell in a tree table row */ |
| 57 |
treeRow?: TdTreeRowType; |
| 58 |
/** Turns the cell into the first cell in a draggable row*/ |
| 59 |
draggableRow?: TdDraggableType; |
| 60 |
/** True to remove padding */ |
| 61 |
noPadding?: boolean; |
| 62 |
/** Applies pf-v6-c-table__action to td */ |
| 63 |
isActionCell?: boolean; |
| 64 |
/** |
| 65 |
* Tooltip to show on the body cell. |
| 66 |
* Note: If the body cell is truncated and has simple string content, it will already attempt to display the cell text. |
| 67 |
* If you want to show a tooltip that differs from the cell text, you can set it here. |
| 68 |
* To disable it completely you can set it to null. |
| 69 |
*/ |
| 70 |
tooltip?: React.ReactNode; |
| 71 |
/** Callback on mouse enter */ |
| 72 |
onMouseEnter?: (event: any) => void; |
| 73 |
/** Indicates the column should be sticky */ |
| 74 |
isStickyColumn?: boolean; |
| 75 |
/** Adds a border to the right side of the cell */ |
| 76 |
hasRightBorder?: boolean; |
| 77 |
/** Adds a border to the left side of the cell */ |
| 78 |
hasLeftBorder?: boolean; |
| 79 |
/** Minimum width for a sticky column */ |
| 80 |
stickyMinWidth?: string; |
| 81 |
/** Left offset of a sticky column. This will typically be equal to the combined value set by stickyMinWidth of any sticky columns that precede the current sticky column. */ |
| 82 |
stickyLeftOffset?: string; |
| 83 |
/** Right offset of a sticky column. This will typically be equal to the combined value set by stickyMinWidth of any sticky columns that come after the current sticky column. */ |
| 84 |
stickyRightOffset?: string; |
| 85 |
} |
| 86 |
|
| 87 |
const TdBase: React.FunctionComponent<TdProps> = ({ |
| 88 |
children, |
| 89 |
className, |
| 90 |
isActionCell = false, |
| 91 |
hasAction = false, |
| 92 |
component = 'td', |
| 93 |
dataLabel, |
| 94 |
textCenter = false, |
| 95 |
modifier, |
| 96 |
select = null, |
| 97 |
actions = null, |
| 98 |
expand = null, |
| 99 |
treeRow: treeRowProp = null, |
| 100 |
compoundExpand: compoundExpandProp = null, |
| 101 |
noPadding, |
| 102 |
width, |
| 103 |
visibility, |
| 104 |
innerRef, |
| 105 |
favorites = null, |
| 106 |
draggableRow: draggableRowProp = null, |
| 107 |
tooltip = '', |
| 108 |
onMouseEnter: onMouseEnterProp = () => {}, |
| 109 |
isStickyColumn = false, |
| 110 |
hasRightBorder = false, |
| 111 |
hasLeftBorder = false, |
| 112 |
stickyMinWidth = '120px', |
| 113 |
stickyLeftOffset, |
| 114 |
stickyRightOffset, |
| 115 |
...props |
| 116 |
}: TdProps) => { |
| 117 |
const [showTooltip, setShowTooltip] = useState(false); |
| 118 |
const [truncated, setTruncated] = useState(false); |
| 119 |
const cellRef = innerRef ? innerRef : createRef(); |
| 120 |
const onMouseEnter = (event: any) => { |
| 121 |
if (event.target.offsetWidth < event.target.scrollWidth) { |
| 122 |
!showTooltip && setShowTooltip(true); |
| 123 |
} else { |
| 124 |
showTooltip && setShowTooltip(false); |
| 125 |
} |
| 126 |
|
| 127 |
onMouseEnterProp(event); |
| 128 |
}; |
| 129 |
|
| 130 |
const selectParams = select |
| 131 |
? selectable(children as IFormatterValueType, { |
| 132 |
rowIndex: select.rowIndex, |
| 133 |
rowData: { |
| 134 |
selected: select.isSelected, |
| 135 |
disableSelection: select?.isDisabled, |
| 136 |
props: select?.props |
| 137 |
}, |
| 138 |
column: { |
| 139 |
extraParams: { |
| 140 |
onSelect: select?.onSelect, |
| 141 |
selectVariant: select.variant || 'checkbox' |
| 142 |
} |
| 143 |
} |
| 144 |
}) |
| 145 |
: null; |
| 146 |
const favoriteParams = favorites |
| 147 |
? favoritable(null, { |
| 148 |
rowIndex: favorites?.rowIndex, |
| 149 |
rowData: { |
| 150 |
favorited: favorites.isFavorited, |
| 151 |
favoritesProps: favorites?.props |
| 152 |
}, |
| 153 |
column: { |
| 154 |
extraParams: { |
| 155 |
onFavorite: favorites?.onFavorite |
| 156 |
} |
| 157 |
} |
| 158 |
}) |
| 159 |
: null; |
| 160 |
const draggableParams = |
| 161 |
draggableRowProp !== null |
| 162 |
? draggable(null, { |
| 163 |
rowData: { |
| 164 |
id: draggableRowProp.id |
| 165 |
} |
| 166 |
}) |
| 167 |
: null; |
| 168 |
const actionParamsFunc = actions ? cellActions(actions.items, null, null) : null; |
| 169 |
const actionParams = actionParamsFunc |
| 170 |
? actionParamsFunc(null, { |
| 171 |
rowIndex: actions?.rowIndex, |
| 172 |
rowData: { |
| 173 |
disableActions: actions?.isDisabled |
| 174 |
}, |
| 175 |
column: { |
| 176 |
extraParams: { |
| 177 |
dropdownPosition: actions?.dropdownPosition, |
| 178 |
dropdownDirection: actions?.dropdownDirection, |
| 179 |
menuAppendTo: actions?.menuAppendTo, |
| 180 |
actionsToggle: actions?.actionsToggle |
| 181 |
} |
| 182 |
} |
| 183 |
}) |
| 184 |
: null; |
| 185 |
|
| 186 |
const { hasAnimations, variant } = useContext(TableContext); |
| 187 |
|
| 188 |
const expandableParams = |
| 189 |
expand !== null |
| 190 |
? collapsible(null, { |
| 191 |
rowIndex: expand.rowIndex, |
| 192 |
columnIndex: expand?.columnIndex, |
| 193 |
rowData: { |
| 194 |
isOpen: expand.isExpanded |
| 195 |
}, |
| 196 |
column: { |
| 197 |
extraParams: { |
| 198 |
onCollapse: expand?.onToggle, |
| 199 |
expandId: expand?.expandId, |
| 200 |
variant |
| 201 |
} |
| 202 |
} |
| 203 |
}) |
| 204 |
: null; |
| 205 |
const updateAnimationClass = () => { |
| 206 |
const ancestorControlRow = (cellRef as React.RefObject<HTMLElement | null>)?.current?.closest( |
| 207 |
`.${styles.tableTr}.${styles.tableControlRow}` |
| 208 |
); |
| 209 |
const isControlRowExpanded = ancestorControlRow.classList.contains(styles.modifiers.expanded); |
| 210 |
if (!isControlRowExpanded) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
const isCurrentCellExpanded = (cellRef as React.RefObject<HTMLElement | null>)?.current?.classList.contains( |
| 215 |
styles.modifiers.expanded |
| 216 |
); |
| 217 |
if (isCurrentCellExpanded) { |
| 218 |
ancestorControlRow.classList.remove(styles.modifiers.noAnimateExpand); |
| 219 |
} else { |
| 220 |
ancestorControlRow.classList.add(styles.modifiers.noAnimateExpand); |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
const internalCompoundOnToggle = ( |
| 225 |
event: React.MouseEvent, |
| 226 |
rowIndex: number, |
| 227 |
colIndex: number, |
| 228 |
isOpen: boolean, |
| 229 |
rowData: IRowData, |
| 230 |
extraData: IExtraData |
| 231 |
) => { |
| 232 |
hasAnimations && updateAnimationClass(); |
| 233 |
compoundExpandProp?.onToggle(event, rowIndex, colIndex, isOpen, rowData, extraData); |
| 234 |
}; |
| 235 |
|
| 236 |
const compoundParams = |
| 237 |
compoundExpandProp !== null |
| 238 |
? compoundExpand( |
| 239 |
{ |
| 240 |
title: children, |
| 241 |
props: { |
| 242 |
isOpen: compoundExpandProp.isExpanded |
| 243 |
} |
| 244 |
} as IFormatterValueType, |
| 245 |
{ |
| 246 |
rowIndex: compoundExpandProp?.rowIndex, |
| 247 |
columnIndex: compoundExpandProp?.columnIndex, |
| 248 |
column: { |
| 249 |
extraParams: { |
| 250 |
onExpand: internalCompoundOnToggle, |
| 251 |
expandId: compoundExpandProp?.expandId |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
) |
| 256 |
: null; |
| 257 |
const widthParams = width ? cellWidth(width)() : null; |
| 258 |
const visibilityParams = visibility |
| 259 |
? classNames(...visibility.map((vis: keyof IVisibility) => Visibility[vis]))() |
| 260 |
: null; |
| 261 |
const treeRowParams = |
| 262 |
treeRowProp !== null |
| 263 |
? treeRow( |
| 264 |
treeRowProp.onCollapse, |
| 265 |
treeRowProp.onCheckChange, |
| 266 |
treeRowProp.onToggleRowDetails |
| 267 |
)( |
| 268 |
{ |
| 269 |
title: children |
| 270 |
} as IFormatterValueType, |
| 271 |
{ |
| 272 |
rowIndex: treeRowProp.rowIndex, |
| 273 |
rowData: { |
| 274 |
props: treeRowProp.props |
| 275 |
} |
| 276 |
} as IExtra |
| 277 |
) |
| 278 |
: null; |
| 279 |
const merged = mergeProps( |
| 280 |
selectParams, |
| 281 |
actionParams, |
| 282 |
expandableParams, |
| 283 |
compoundParams, |
| 284 |
widthParams, |
| 285 |
visibilityParams, |
| 286 |
favoriteParams, |
| 287 |
treeRowParams, |
| 288 |
draggableParams |
| 289 |
); |
| 290 |
const { |
| 291 |
// selectable adds this but we don't want it |
| 292 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 293 |
isVisible = null, |
| 294 |
children: mergedChildren = null, |
| 295 |
className: mergedClassName = '', |
| 296 |
component: MergedComponent = component, |
| 297 |
...mergedProps |
| 298 |
} = merged; |
| 299 |
|
| 300 |
const treeTableTitleCell = |
| 301 |
(className && className.includes(treeViewStyles.tableTreeViewTitleCell)) || |
| 302 |
(mergedClassName && mergedClassName.includes(treeViewStyles.tableTreeViewTitleCell)); |
| 303 |
|
| 304 |
useEffect(() => { |
| 305 |
setTruncated( |
| 306 |
(cellRef as React.RefObject<HTMLElement | null>).current.offsetWidth < |
| 307 |
(cellRef as React.RefObject<HTMLElement | null>).current.scrollWidth |
| 308 |
); |
| 309 |
}, [cellRef]); |
| 310 |
|
| 311 |
const cell = ( |
| 312 |
<MergedComponent |
| 313 |
tabIndex={(select || !truncated) && modifier !== 'truncate' ? -1 : 0} |
| 314 |
{...(!treeTableTitleCell && { 'data-label': dataLabel })} |
| 315 |
onFocus={tooltip !== null ? onMouseEnter : onMouseEnterProp} |
| 316 |
onBlur={() => setShowTooltip(false)} |
| 317 |
onMouseEnter={tooltip !== null ? onMouseEnter : onMouseEnterProp} |
| 318 |
className={css( |
| 319 |
styles.tableTd, |
| 320 |
className, |
| 321 |
isActionCell && styles.tableAction, |
| 322 |
hasAction && styles.modifiers.action, |
| 323 |
textCenter && styles.modifiers.center, |
| 324 |
noPadding && styles.modifiers.noPadding, |
| 325 |
isStickyColumn && scrollStyles.tableStickyCell, |
| 326 |
hasRightBorder && scrollStyles.modifiers.borderRight, |
| 327 |
hasLeftBorder && scrollStyles.modifiers.borderLeft, |
| 328 |
styles.modifiers[modifier as 'breakWord' | 'fitContent' | 'nowrap' | 'truncate' | 'wrap' | undefined], |
| 329 |
draggableParams && styles.tableDraggable, |
| 330 |
mergedClassName |
| 331 |
)} |
| 332 |
ref={cellRef} |
| 333 |
{...mergedProps} |
| 334 |
{...props} |
| 335 |
{...(isStickyColumn && { |
| 336 |
style: { |
| 337 |
[cssStickyCellMinWidth.name]: stickyMinWidth ? stickyMinWidth : undefined, |
| 338 |
[cssStickyCellInlineStart.name]: stickyLeftOffset ? stickyLeftOffset : 0, |
| 339 |
[cssStickyCellInlineEnd.name]: stickyRightOffset ? stickyRightOffset : 0, |
| 340 |
...props.style |
| 341 |
} as React.CSSProperties |
| 342 |
})} |
| 343 |
> |
| 344 |
{mergedChildren || children} |
| 345 |
</MergedComponent> |
| 346 |
); |
| 347 |
|
| 348 |
const canMakeDefaultTooltip = tooltip === '' ? typeof children === 'string' : true; |
| 349 |
return tooltip !== null && canMakeDefaultTooltip && showTooltip ? ( |
| 350 |
<> |
| 351 |
{cell} |
| 352 |
<Tooltip |
| 353 |
triggerRef={cellRef as React.RefObject<any>} |
| 354 |
content={tooltip || (tooltip === '' && children)} |
| 355 |
isVisible |
| 356 |
/> |
| 357 |
</> |
| 358 |
) : ( |
| 359 |
cell |
| 360 |
); |
| 361 |
}; |
| 362 |
|
| 363 |
export const Td = forwardRef((props: TdProps, ref: React.Ref<HTMLTableDataCellElement>) => ( |
| 364 |
<TdBase {...props} innerRef={ref} /> |
| 365 |
)); |
| 366 |
Td.displayName = 'Td'; |