-
Notifications
You must be signed in to change notification settings - Fork 37
/
defaultRowFormatter.js
66 lines (54 loc) · 1.54 KB
/
defaultRowFormatter.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
import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import { isFunction } from 'lodash';
const propTypes = {
cells: PropTypes.arrayOf(PropTypes.element),
labelStrings: PropTypes.oneOf([PropTypes.arrayOf(PropTypes.string), PropTypes.func]),
rowClass: PropTypes.string,
rowData: PropTypes.object,
rowIndex: PropTypes.number,
rowProps: PropTypes.object,
rowWidth: PropTypes.number,
};
function defaultRowFormatter(row) {
const {
rowIndex,
rowClass,
rowData,
cells,
rowProps: initialRowProps,
} = row;
// Default row element
const rowProps = { ...initialRowProps };
let Element = 'div';
// Render a <Link> if a "to"-prop is provided (react-router API)
if (rowProps.to) {
Element = Link;
if (isFunction(rowProps.to)) {
rowProps.to = rowProps.to(rowData.id);
}
}
// Render an anchor tag if an "href"-prop is provided
if (rowProps.href) {
Element = 'a';
if (isFunction(rowProps.href)) {
rowProps.href = rowProps.href(rowData.id);
}
}
const labelStrings = rowProps.labelStrings && rowProps.labelStrings(row);
delete rowProps.labelStrings; // We don't want to spread this onto the DOM element.
return (
<Element
key={`row-${rowIndex}`}
className={rowClass}
aria-label={labelStrings && labelStrings.join('...')}
tabIndex={rowProps.onClick ? '0' : undefined}
{...rowProps}
>
{cells}
</Element>
);
}
defaultRowFormatter.propTypes = propTypes;
export default defaultRowFormatter;