-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathDataTable.js
More file actions
156 lines (136 loc) · 4.79 KB
/
DataTable.js
File metadata and controls
156 lines (136 loc) · 4.79 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
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
/**
* mSupply Mobile
* Sustainable Solutions (NZ) Ltd. 2019
*/
import PropTypes from 'prop-types'
import React, { useMemo, useRef, useCallback } from 'react'
import {
StyleSheet,
VirtualizedList,
VirtualizedListPropTypes,
Keyboard,
} from 'react-native'
import RefContext from './RefContext'
/**
* Base DataTable component. Wrapper around VirtualizedList, providing
* a header component, scroll to top and focus features.
* All VirtualizedList props can be passed through, however renderItem
* is renamed renderRow.
*
* Managing focus and scrolling:
* Can manage focusing and auto-scrolling for editable cells through react context API.
*
* Four parameters are passed in through the refContext:
*
* - `getRefIndex` : Gets the ref index for an editable cell given the columnkey and row index.
* - `getCellRef` : Lazily creates a ref for a cell.
* - `focusNextCell` : Focus' the next editable cell. Call during onEditingSubmit.
* - `adjustToTop` : Scrolls so the focused row is at the top of the list.
*
* @param {Func} renderRow Renaming of VirtualizedList renderItem prop.
* @param {Func} renderHeader Function which should return a header component
* @param {Object} style Style Object for this component.
* @param {Object} data Array of data objects.
* @param {Object} columns Array of column objects.
*/
const DataTable = React.memo(
({ renderRow, renderHeader, style, data, columns, ...otherProps }) => {
// Reference to the virtualized list for scroll operations.
const virtualizedListRef = useRef()
// Array of column keys for determining ref indicies.
const editableColumnKeys = useMemo(
() =>
columns.reduce((columnKeys, column) => {
const { editable } = column
if (editable) return [...columnKeys, column.key]
return columnKeys
}, []),
[columns]
)
const numberOfEditableColumns = editableColumnKeys.length
const numberOfRows = data.length
const numberOfEditableCells = numberOfEditableColumns * numberOfRows
// Array for each editable cell. Needs to be stable, but updates shouldn't cause re-renders.
const cellRefs = useRef(Array.from({ length: numberOfEditableCells }))
// Passes a cell it's ref index.
const getRefIndex = (rowIndex, columnKey) => {
const columnIndex = editableColumnKeys.findIndex(key => columnKey === key)
return rowIndex * numberOfEditableColumns + columnIndex
}
// Callback for an editable cell. Lazily creating refs.
const getCellRef = refIndex => {
if (cellRefs.current[refIndex]) return cellRefs.current[refIndex]
const newRef = React.createRef()
cellRefs.current[refIndex] = newRef
return newRef
}
// Focuses the next editable cell in the list. On the last row, dismiss the keyboard.
const focusNextCell = refIndex => {
const lastRefIndex = numberOfEditableCells - 1
if (refIndex === lastRefIndex) return Keyboard.dismiss()
const nextCellRef = (refIndex + 1) % numberOfEditableCells
const cellRef = getCellRef(nextCellRef)
return cellRef.current.focus()
}
// Adjusts the passed row to the top of the list.
const adjustToTop = useCallback(rowIndex => {
virtualizedListRef.current.scrollToIndex({ index: rowIndex })
}, [])
// Contexts values. Functions passed to rows and editable cells to control focus/scrolling.
const contextValue = useMemo(
() => ({
getRefIndex,
getCellRef,
focusNextCell,
adjustToTop,
}),
[numberOfEditableCells]
)
const renderItem = useCallback(
rowItem => renderRow(rowItem, focusNextCell, getCellRef, adjustToTop),
[renderRow]
)
return (
<RefContext.Provider value={contextValue}>
{renderHeader && renderHeader()}
<VirtualizedList
ref={virtualizedListRef}
keyboardDismissMode="none"
data={data}
keyboardShouldPersistTaps="always"
style={style}
renderItem={renderItem}
{...otherProps}
/>
</RefContext.Provider>
)
}
)
const defaultStyles = StyleSheet.create({
virtualizedList: {
flex: 1,
},
})
DataTable.propTypes = {
...VirtualizedListPropTypes,
renderRow: PropTypes.func.isRequired,
renderHeader: PropTypes.func,
getItem: PropTypes.func,
getItemCount: PropTypes.func,
initialNumToRender: PropTypes.number,
removeClippedSubviews: PropTypes.bool,
windowSize: PropTypes.number,
style: PropTypes.object,
columns: PropTypes.array,
}
DataTable.defaultProps = {
renderHeader: null,
style: defaultStyles.virtualizedList,
getItem: (items, index) => items[index],
getItemCount: items => items.length,
initialNumToRender: 10,
removeClippedSubviews: true,
windowSize: 3,
columns: [],
}
export default DataTable