-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathTouchableCell.js
More file actions
103 lines (94 loc) · 3.27 KB
/
TouchableCell.js
File metadata and controls
103 lines (94 loc) · 3.27 KB
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
import React from 'react'
import PropTypes from 'prop-types'
import { Text, TouchableOpacity, TouchableOpacityPropTypes } from 'react-native'
import TouchableNoFeedback from './TouchableNoFeedback'
import { getAdjustedStyle } from './utilities'
/**
* Renders a cell with value (or renderChildren) that is touchable
*
* @param {string|number} value The value to render in cell
* @param {string|number} rowKey Unique key associated to row cell is in
* @param {string|number} columnKey Unique key associated to column cell is in
* @param {func} onPressAction Action creator for handling focusing of this cell.
* `(rowKey, columnKey) => {...}`
* @param {func} dispatch Reducer dispatch callback for handling actions
* @param {func} renderChildren Reducer dispatch callback for handling actions
* @param {func} TouchableComponent Override containing element of TouchableCell
* Additional props spread into TouchableComponent
* @param {object} containerStyle Style object for the containing Touchable component
* @param {object} textStyle Style object for the inner Text component
* @param {Number} width Optional flex property to inject into styles.
* @param {Bool} isLastCell Indicator for if this cell is the last
* in a row. Removing the borderRight if true.
*/
const TouchableCell = React.memo(
({
value,
rowKey,
columnKey,
onPressAction,
dispatch,
renderChildren,
TouchableComponent,
containerStyle,
width,
textStyle,
isLastCell,
debug,
isDisabled,
...otherProps
}) => {
if (debug) console.log(`- TouchableCell: ${rowKey},${columnKey}`)
const onPress = () => dispatch(onPressAction(rowKey, columnKey))
const internalContainerStyle = getAdjustedStyle(
containerStyle,
width,
isLastCell
)
const Container = isDisabled
? TouchableNoFeedback
: TouchableComponent || TouchableOpacity
const content = renderChildren ? (
renderChildren(value)
) : (
<Text style={textStyle}>{value}</Text>
)
return (
<Container
style={internalContainerStyle}
onPress={onPress}
{...otherProps}
>
{content}
</Container>
)
}
)
TouchableCell.propTypes = {
...TouchableOpacityPropTypes,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
columnKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
onPressAction: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
renderChildren: PropTypes.func.isRequired,
TouchableComponent: PropTypes.node,
containerStyle: PropTypes.object,
textStyle: PropTypes.object,
isLastCell: PropTypes.bool,
width: PropTypes.number,
debug: PropTypes.bool,
isDisabled: PropTypes.bool,
}
TouchableCell.defaultProps = {
value: '',
containerStyle: {},
textStyle: {},
TouchableComponent: null,
isLastCell: false,
width: 0,
debug: false,
isDisabled: false,
}
export default TouchableCell