/
LinkCell.js
82 lines (74 loc) · 1.92 KB
/
LinkCell.js
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
import React from 'react';
import { Link } from 'react-router';
import { PropTypes } from 'prop-types';
import { Tooltip } from '../../tooltips';
import TableCell from './TableCell';
export default function LinkCell(props) {
const { cellIndex, column, record } = props;
const {
className = '',
hrefFn,
idKey = 'id',
textKey = 'label',
textFn,
subtitleFn,
tooltipEnabled = false,
} = column;
let children = props.children;
if (!children) {
if (textFn) {
children = textFn(record);
} else {
children = record[textKey];
}
}
const name = record[textKey];
let tooltipComponent;
let tooltipAttributes;
let tooltipEnabledClass = '';
if (tooltipEnabled) {
const tooltipId = `tooltip-${record[idKey]}-${cellIndex}`;
const idText = `ID: ${record[idKey]}`;
const tooltipText = (
<div>
<div>
{name}
</div>
{idText}
</div>
);
tooltipEnabledClass = 'TooltipEnabled';
tooltipAttributes = { 'data-tip': true, 'data-for': tooltipId };
tooltipComponent = (
<Tooltip id={tooltipId}>{tooltipText}</Tooltip>
);
}
return (
<TableCell
cellIndex={cellIndex}
className={`LinkCell ${className} ${tooltipEnabledClass}`}
column={column}
record={record}
>
<Link to={hrefFn(record)} {...tooltipAttributes}>
{children}
</Link>
{tooltipComponent}
{subtitleFn ? <div><small>{subtitleFn(record)}</small></div> : null}
</TableCell>
);
}
LinkCell.propTypes = {
cellIndex: PropTypes.number,
children: PropTypes.node,
className: PropTypes.string,
column: PropTypes.shape({
disableTooltip: PropTypes.bool,
hrefFn: PropTypes.func.isRequired,
textKey: PropTypes.string,
// TODO: consider generalizing textFn for formatting
textFn: PropTypes.func,
subtitleFn: PropTypes.func,
}).isRequired,
record: PropTypes.object.isRequired,
};