/
cell.tsx
184 lines (157 loc) · 5.88 KB
/
cell.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
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
* Licensed under the terms of the LICENSE file distributed with this project.
*/
import classNames from "classnames";
import * as React from "react";
import * as Classes from "../common/classes";
import { Classes as CoreClasses, IIntentProps, IProps, Utils as CoreUtils } from "@blueprintjs/core";
import { LoadableContent } from "../common/loadableContent";
import { JSONFormat } from "./formats/jsonFormat";
import { TruncatedFormat } from "./formats/truncatedFormat";
export interface ICellProps extends IIntentProps, IProps {
key?: string;
style?: React.CSSProperties;
/**
* The column index of the cell. If provided, this will be passed as an argument to any callbacks
* when they are invoked.
*/
columnIndex?: number;
/**
* If `true`, the cell will be rendered above overlay layers to enable mouse
* interactions within the cell.
* @default false
*/
interactive?: boolean;
/**
* An optional native tooltip that is displayed on hover.
* If `true`, content will be replaced with a fixed-height skeleton.
* @default false
*/
loading?: boolean;
/**
* The row index of the cell. If provided, this will be passed as an argument to any callbacks
* when they are invoked.
*/
rowIndex?: number;
/**
* An optional native tooltip that is displayed on hover.
*/
tooltip?: string;
/**
* If `true`, the cell contents will be wrapped in a `div` with
* styling that will prevent the content from overflowing the cell.
* @default true
*/
truncated?: boolean;
/**
* If `true`, the cell contents will be wrapped in a `div` with
* styling that will cause text to wrap, rather than displaying it on a single line.
* @default false
*/
wrapText?: boolean;
/**
* Allows for setting a tab index on the cell, so the cell can be browser-focusable.
*/
tabIndex?: number;
/**
* Callback invoked when the cell is focused and a key is pressed down.
*/
onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
/**
* Callback invoked when the cell is focused and a key is released.
*/
onKeyUp?: React.KeyboardEventHandler<HTMLElement>;
/**
* Callback invoked when a character-key is pressed.
*/
onKeyPress?: React.KeyboardEventHandler<HTMLElement>;
/**
* A ref handle to capture the outer div of this cell. Used internally.
*/
cellRef?: (ref: HTMLElement | null) => void;
}
export type ICellRenderer = (rowIndex: number, columnIndex: number) => React.ReactElement<ICellProps>;
export const emptyCellRenderer = () => <Cell />;
export class Cell extends React.Component<ICellProps, {}> {
public static defaultProps = {
truncated: true,
wrapText: false,
};
public shouldComponentUpdate(nextProps: ICellProps) {
// deeply compare "style," because a new but identical object might have been provided.
return (
!CoreUtils.shallowCompareKeys(this.props, nextProps, { exclude: ["style"] }) ||
!CoreUtils.deepCompareKeys(this.props.style, nextProps.style)
);
}
public render() {
const {
cellRef,
tabIndex,
onKeyDown,
onKeyUp,
onKeyPress,
style,
intent,
interactive,
loading,
tooltip,
truncated,
className,
wrapText,
} = this.props;
const classes = classNames(
Classes.TABLE_CELL,
CoreClasses.intentClass(intent),
{
[Classes.TABLE_CELL_INTERACTIVE]: interactive,
[CoreClasses.LOADING]: loading,
[Classes.TABLE_TRUNCATED_CELL]: truncated,
},
className,
);
const textClasses = classNames({
[Classes.TABLE_TRUNCATED_TEXT]: truncated,
[Classes.TABLE_NO_WRAP_TEXT]: !wrapText,
});
// add width and height to the children, for use in shouldComponentUpdate in truncatedFormat
// note: these aren't actually used by truncated format, just in shouldComponentUpdate
const modifiedChildren = React.Children.map(this.props.children, child => {
if (style != null && React.isValidElement(child)) {
const childType = child.type;
// can't get prototype of "string" child, so treat those separately
if (typeof child === "string" || typeof childType === "string") {
return child;
} else {
const isTruncatedFormat =
childType.prototype === TruncatedFormat.prototype ||
TruncatedFormat.prototype.isPrototypeOf(childType) ||
childType.prototype === JSONFormat.prototype ||
JSONFormat.prototype.isPrototypeOf(childType);
// only add props if child is truncated format
if (isTruncatedFormat) {
return React.cloneElement(child as React.ReactElement<any>, {
parentCellHeight: parseInt(style.height, 10),
parentCellWidth: parseInt(style.width, 10),
});
}
}
}
return child;
});
const content = <div className={textClasses}>{modifiedChildren}</div>;
return (
<div
className={classes}
title={tooltip}
ref={cellRef}
{...{ style, tabIndex, onKeyDown, onKeyUp, onKeyPress }}
>
<LoadableContent loading={loading} variableLength={true}>
{content}
</LoadableContent>
</div>
);
}
}