-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.js
114 lines (104 loc) · 2.99 KB
/
Cell.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
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
import React, {useCallback, useContext} from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import {getPixelsOrString, getStyleForPaddings, isFunction} from '../../util/helpers';
import RowContext from './RowContext';
const Cell = function(props) {
const {
children = null,
onClick = null,
onDoubleClick = null,
width = null,
height = null,
textAlign = null,
verticalAlign = null,
colSpan = null,
rowSpan = null,
style: userStyle = null,
as = 'td',
p = null,
pt = null,
pr = null,
pb = null,
pl = null,
...otherProps
} = props;
const {onClick: onRowClick, onDoubleClick: onRowDoubleClick, hasClick: hasRowClick, hasDoubleClick: hasRowDoubleClick} = useContext(
RowContext
);
const handleClick = useCallback(
(e) => {
if (isFunction(onClick)) {
e.preventDefault();
e.stopPropagation();
onClick();
} else if (isFunction(onRowClick)) {
e.preventDefault();
e.stopPropagation();
onRowClick();
}
},
[onClick, onRowClick]
);
const handleDoubleClick = useCallback(
(e) => {
if (isFunction(onDoubleClick)) {
e.preventDefault();
e.stopPropagation();
onDoubleClick();
} else if (isFunction(onRowDoubleClick)) {
e.preventDefault();
e.stopPropagation();
onRowDoubleClick();
}
},
[onDoubleClick, onRowDoubleClick]
);
const style = {
width: getPixelsOrString(width),
height: getPixelsOrString(height),
...getStyleForPaddings({p, pt, pr, pb, pl}),
...userStyle
};
return (
<StyledCell
onClick={isFunction(onClick) || hasRowClick ? handleClick : undefined}
onDoubleClick={isFunction(onDoubleClick) || hasRowDoubleClick ? handleDoubleClick : undefined}
textAlign={textAlign}
verticalAlign={verticalAlign}
colSpan={colSpan}
rowSpan={rowSpan}
style={style}
{...otherProps}
as={as}
>
{children}
</StyledCell>
);
};
Cell.propTypes = {
children: PropTypes.node,
onClick: PropTypes.func,
onDoubleClick: PropTypes.func,
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
textAlign: PropTypes.oneOf(['left', 'center', 'right', 'justify', 'inherit', 'initial']),
verticalAlign: PropTypes.oneOf(['baseline', 'sub', 'super', 'text-top', 'text-bottom', 'middle', 'top', 'bottom']),
colSpan: PropTypes.number,
rowSpan: PropTypes.number,
style: PropTypes.object,
as: PropTypes.string, // because we're passing this internally
// padding:
p: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
pt: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
pr: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
pb: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
pl: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
};
const StyledCell = styled.td`
display: table-cell;
text-align: ${({textAlign}) => textAlign || 'inherit'};
vertical-align: ${({verticalAlign}) => verticalAlign || 'middle'};
box-sizing: border-box;
`;
export default Cell;