-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
198 lines (182 loc) · 5.2 KB
/
index.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import React, { Component } from 'react';
import { FlatList, View, ViewPropTypes } from 'react-native';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import lodash from 'lodash';
import PropTypes from 'prop-types';
import TouchableWrapper from './TouchableWrapper';
import styles from './Styles/styles';
export const STATE = {
DEFAULT: 'default',
EDIT: 'edit'
}
class SelectableFlatlist extends Component {
static propTypes = {
...ViewPropTypes,
data: PropTypes.array.isRequired,
state: PropTypes.oneOf([STATE.DEFAULT, STATE.EDIT]).isRequired,
cellItemComponent: PropTypes.any,
touchStyles: PropTypes.any,
renderCheck: PropTypes.any,
checkUncheckContainerStyle: PropTypes.any,
checkColor: PropTypes.string,
checkUncheckSize: PropTypes.number,
checkIcon: PropTypes.any,
renderUncheck: PropTypes.any,
uncheckColor: PropTypes.string,
uncheckIcon: PropTypes.any,
initialSelectedIndex: PropTypes.array.isRequired,
itemsSelected: PropTypes.func.isRequired,
multiSelect: PropTypes.bool.isRequired
};
static defaultProps = {
data: [],
state: STATE.EDIT,
multiSelect: false
};
constructor(props) {
super(props);
this.state = {
selectedItems: []
};
}
componentDidMount() {
this.prepareInitialList();
}
componentDidUpdate(prevProps, prevState) {
if (this.props.data && !lodash.isEqual(this.props.data, prevProps.data)) {
this.prepareInitialList();
} else if (this.props.state && !lodash.isEqual(this.props.state, prevProps.state)) {
this.prepareInitialList();
}
}
onItemSelected = (item) => {
const { multiSelect, itemsSelected } = this.props;
let selectedItems = this.state.selectedItems.slice();
if (!multiSelect) {
selectedItems = [item];
this.setState({ selectedItems }, () => {
itemsSelected(selectedItems);
});
} else {
const itemIndexFound = lodash.find(selectedItems,
(element) => (element === item));
if (itemIndexFound) {
lodash.pull(selectedItems, item);
} else {
selectedItems.push(item);
}
this.setState({ selectedItems }, () => {
itemsSelected(selectedItems);
});
}
}
prepareInitialList = () => {
const { data, initialSelectedIndex, state, itemsSelected } = this.props;
if (state === STATE.DEFAULT) {
this.setState({ selectedItems: [] }, () => {
itemsSelected([]);
});
} else {
const itemSelected = [];
data.forEach((element, i) => {
const itemIndexFound = lodash.find(initialSelectedIndex,
(item) => (item === i));
if (typeof itemIndexFound === 'number') {
itemSelected.push(element);
}
});
this.setState({ selectedItems: itemSelected }, () => {
itemsSelected(itemSelected);
});
}
}
isItemSelected = (item) => {
const { selectedItems } = this.state;
const itemSelected = lodash.find(selectedItems,
(element) => (element === item));
if (itemSelected) {
return true;
}
return false;
}
keyExtractor = (item, index) => `${index}`;
renderUncheck = () => {
const { renderUncheck, checkUncheckContainerStyle, uncheckColor, checkUncheckSize, uncheckIcon } = this.props;
if (renderUncheck) {
return renderUncheck();
}
return (
<View
style={[styles.defaultContainer, checkUncheckContainerStyle]}>
{
uncheckIcon ?
uncheckIcon()
:
<MaterialCommunityIcons
name={'check-circle-outline'}
color={uncheckColor ? uncheckColor : '#dfdfdf'}
size={checkUncheckSize ? checkUncheckSize : 30}
/>
}
</View>
)
}
renderCheck = () => {
const { renderCheck, checkUncheckContainerStyle, checkColor, checkUncheckSize, checkIcon } = this.props;
if (renderCheck) {
return renderCheck();
}
return (
<View
style={[styles.defaultContainer, checkUncheckContainerStyle]}>
{
checkIcon ?
checkIcon()
:
<MaterialCommunityIcons
name={'check-circle'}
color={checkColor ? checkColor : '#00FF00'}
size={checkUncheckSize ? checkUncheckSize : 30}
/>
}
</View>
)
}
renderItem = ({ item }) => {
const { state, cellItemComponent, touchStyles } = this.props;
if (state === STATE.DEFAULT) {
return (
cellItemComponent(item, { ...this.props })
);
}
return (
<TouchableWrapper
touchStyles={[styles.defaultTouch, touchStyles]}
onPress={() => {
this.onItemSelected(item);
}}
>
{
this.isItemSelected(item) ?
this.renderCheck()
:
this.renderUncheck()
}
{
cellItemComponent(item, { ...this.props })
}
</TouchableWrapper>
);
};
render() {
return (
<FlatList
data={this.props.data}
extraData={this.state.selectedItems}
keyExtractor={this.keyExtractor}
renderItem={this.renderItem}
/>
);
}
}
export default SelectableFlatlist;