-
Notifications
You must be signed in to change notification settings - Fork 349
/
Td.tsx
289 lines (283 loc) · 9.35 KB
/
Td.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import * as React from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/Table/table';
import scrollStyles from '@patternfly/react-styles/css/components/Table/table-scrollable';
import { BaseCellProps } from './TableComposable';
import { cellActions } from '../Table/utils/decorators/cellActions';
import { selectable } from '../Table/utils/decorators/selectable';
import { collapsible } from '../Table/utils/decorators/collapsible';
import { compoundExpand } from '../Table/utils/decorators/compoundExpand';
import { cellWidth } from '../Table/utils/decorators/cellWidth';
import { Visibility, classNames } from './../Table/utils/decorators/classNames';
import { favoritable } from '../Table/utils/decorators/favoritable';
import { draggable } from '../Table/utils/decorators/draggable';
import { treeRow } from '../Table/utils/decorators/treeRow';
import { mergeProps } from '../Table/base/merge-props';
import { IVisibility } from '../Table/utils/decorators/classNames';
import { Tooltip } from '@patternfly/react-core/dist/esm/components/Tooltip/Tooltip';
import { IFormatterValueType, IExtra } from '../Table/TableTypes';
import {
TdActionsType,
TdCompoundExpandType,
TdDraggableType,
TdExpandType,
TdFavoritesType,
TdSelectType,
TdTreeRowType
} from '../Table/base';
export interface TdProps extends BaseCellProps, Omit<React.HTMLProps<HTMLTableDataCellElement>, 'onSelect' | 'width'> {
/**
* The column header the cell corresponds to.
* This attribute replaces table header in mobile viewport. It is rendered by ::before pseudo element.
*/
dataLabel?: string;
/** Renders a checkbox or radio select */
select?: TdSelectType;
/** Turns the cell into an actions cell */
actions?: TdActionsType;
/** Turns the cell into an expansion toggle and determines if the corresponding expansion row is open */
expand?: TdExpandType;
/** Turns the cell into a compound expansion toggle */
compoundExpand?: TdCompoundExpandType;
/** Turns the cell into a favorites cell with a star button */
favorites?: TdFavoritesType;
/** Turns the cell into the first cell in a tree table row */
treeRow?: TdTreeRowType;
/** Turns the cell into the first cell in a draggable row*/
draggableRow?: TdDraggableType;
/** True to remove padding */
noPadding?: boolean;
/** Applies pf-c-table__action to td */
isActionCell?: boolean;
/**
* Tooltip to show on the body cell.
* Note: If the body cell is truncated and has simple string content, it will already attempt to display the cell text.
* If you want to show a tooltip that differs from the cell text, you can set it here.
* To disable it completely you can set it to null.
*/
tooltip?: React.ReactNode;
/** Callback on mouse enter */
onMouseEnter?: (event: any) => void;
/** Indicates the column should be sticky */
isStickyColumn?: boolean;
/** Adds a border to the right side of the cell */
hasRightBorder?: boolean;
/** Minimum width for a sticky column */
stickyMinWidth?: string;
/** 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. */
stickyLeftOffset?: string;
}
const TdBase: React.FunctionComponent<TdProps> = ({
children,
className,
isActionCell = 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,
stickyMinWidth = '120px',
stickyLeftOffset,
...props
}: TdProps) => {
const [showTooltip, setShowTooltip] = React.useState(false);
const onMouseEnter = (event: any) => {
if (event.target.offsetWidth < event.target.scrollWidth) {
!showTooltip && setShowTooltip(true);
} else {
showTooltip && setShowTooltip(false);
}
onMouseEnterProp(event);
};
const selectParams = select
? selectable(children as IFormatterValueType, {
rowIndex: select.rowIndex,
rowData: {
selected: select.isSelected,
disableSelection: select?.disable,
props: select?.props
},
column: {
extraParams: {
onSelect: select?.onSelect,
selectVariant: select.variant || 'checkbox'
}
}
})
: null;
const favoriteParams = favorites
? favoritable(null, {
rowIndex: favorites?.rowIndex,
rowData: {
favorited: favorites.isFavorited,
favoritesProps: favorites?.props
},
column: {
extraParams: {
onFavorite: favorites?.onFavorite
}
}
})
: null;
const draggableParams =
draggableRowProp !== null
? draggable(null, {
rowData: {
id: draggableRowProp.id
}
})
: null;
const actionParamsFunc = actions ? cellActions(actions.items, null, null) : null;
const actionParams = actionParamsFunc
? actionParamsFunc(null, {
rowIndex: actions?.rowIndex,
rowData: {
disableActions: actions?.disable
},
column: {
extraParams: {
dropdownPosition: actions?.dropdownPosition,
dropdownDirection: actions?.dropdownDirection,
actionsToggle: actions?.actionsToggle
}
}
})
: null;
const expandableParams =
expand !== null
? collapsible(null, {
rowIndex: expand.rowIndex,
columnIndex: expand?.columnIndex,
rowData: {
isOpen: expand.isExpanded
},
column: {
extraParams: {
onCollapse: expand?.onToggle,
expandId: expand?.expandId
}
}
})
: null;
const compoundParams =
compoundExpandProp !== null
? compoundExpand(
{
title: children,
props: {
isOpen: compoundExpandProp.isExpanded
}
} as IFormatterValueType,
{
rowIndex: compoundExpandProp?.rowIndex,
columnIndex: compoundExpandProp?.columnIndex,
column: {
extraParams: {
onExpand: compoundExpandProp?.onToggle,
expandId: compoundExpandProp?.expandId
}
}
}
)
: null;
const widthParams = width ? cellWidth(width)() : null;
const visibilityParams = visibility
? classNames(...visibility.map((vis: keyof IVisibility) => Visibility[vis]))()
: null;
const treeRowParams =
treeRowProp !== null
? treeRow(
treeRowProp.onCollapse,
treeRowProp.onCheckChange,
treeRowProp.onToggleRowDetails
)(
{
title: children
} as IFormatterValueType,
{
rowIndex: treeRowProp.rowIndex,
rowData: {
props: treeRowProp.props
}
} as IExtra
)
: null;
const merged = mergeProps(
selectParams,
actionParams,
expandableParams,
compoundParams,
widthParams,
visibilityParams,
favoriteParams,
treeRowParams,
draggableParams
);
const {
// selectable adds this but we don't want it
// eslint-disable-next-line @typescript-eslint/no-unused-vars
isVisible = null,
children: mergedChildren = null,
className: mergedClassName = '',
component: MergedComponent = component,
...mergedProps
} = merged;
const treeTableTitleCell =
(className && className.includes('pf-c-table__tree-view-title-cell')) ||
(mergedClassName && mergedClassName.includes('pf-c-table__tree-view-title-cell'));
const cell = (
<MergedComponent
{...(!treeTableTitleCell && { 'data-label': dataLabel })}
onMouseEnter={tooltip !== null ? onMouseEnter : onMouseEnterProp}
className={css(
className,
isActionCell && styles.tableAction,
textCenter && styles.modifiers.center,
noPadding && styles.modifiers.noPadding,
isStickyColumn && scrollStyles.tableStickyColumn,
hasRightBorder && scrollStyles.modifiers.borderRight,
styles.modifiers[modifier as 'breakWord' | 'fitContent' | 'nowrap' | 'truncate' | 'wrap' | undefined],
draggableParams && styles.tableDraggable,
mergedClassName
)}
ref={innerRef}
{...mergedProps}
{...props}
{...(isStickyColumn && {
style: {
'--pf-c-table__sticky-column--MinWidth': stickyMinWidth ? stickyMinWidth : undefined,
'--pf-c-table__sticky-column--Left': stickyLeftOffset ? stickyLeftOffset : undefined,
...props.style
} as React.CSSProperties
})}
>
{mergedChildren || children}
</MergedComponent>
);
const canMakeDefaultTooltip = tooltip === '' ? typeof children === 'string' : true;
return tooltip !== null && canMakeDefaultTooltip && showTooltip ? (
<Tooltip content={tooltip || (tooltip === '' && children)} isVisible>
{cell}
</Tooltip>
) : (
cell
);
};
export const Td = React.forwardRef((props: TdProps, ref: React.Ref<HTMLTableDataCellElement>) => (
<TdBase {...props} innerRef={ref} />
));
Td.displayName = 'Td';