-
Notifications
You must be signed in to change notification settings - Fork 5
/
MultiSelect.js
136 lines (119 loc) · 3.47 KB
/
MultiSelect.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
import React, { Component, PropTypes } from 'react';
import { ListView, FlatList, SectionList } from 'react-native';
import MultiSelectRow from './MultiSelectRow';
class MultiSelect extends Component {
constructor(props) {
super(props);
this.renderRow = this.renderRow.bind(this);
this.selectRow = this.selectRow.bind(this);
this.generateDataSource = this.generateDataSource.bind(this);
this.state = {
selectedRows: props.selectedOptions || [],
};
}
componentWillReceiveProps(props) {
const { selectedOptions } = props;
// `selectedOptions` is a list of keys that are also used in `options`
if (selectedOptions) {
this.setState({
selectedRows: selectedOptions,
});
}
}
shouldComponentUpdate(nextProps) {
// This is a performance check as this sometimes gets updated often
if (nextProps.selectedOptions.length === this.props.selectedOptions.length) {
return false;
}
return true;
}
generateDataSource(options) { // eslint-disable-line class-methods-use-this
return new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
}).cloneWithRows(options);
}
selectRow(row) {
const { selectedRows } = this.state;
const indexToRemove = selectedRows.indexOf(row.key);
this.setState(
(() => {
if (indexToRemove !== -1) {
return {
selectedRows: [
...selectedRows.slice(0, indexToRemove),
...selectedRows.slice(indexToRemove + 1),
],
};
}
return {
selectedRows: [...selectedRows].concat([row.key]),
};
})(),
() => this.props.onSelectionChange(row, this.state.selectedRows),
);
}
renderRow(row) {
if (!row.item) {
return false;
}
const {
renderRow: _renderRow,
rowStyle,
selectedOptions,
underlayColor,
activeOpacity,
} = this.props;
const { selectedRows } = this.state;
const isSelected = (
selectedRows.indexOf(row.item.key) !== -1 ||
(selectedOptions && selectedOptions.indexOf(row.item.key) !== -1)
);
return (
<MultiSelectRow
row={row.item}
isSelected={isSelected}
renderRow={_renderRow}
rowStyle={rowStyle}
selectRow={this.selectRow}
activeOpacity={activeOpacity}
underlayColor={underlayColor}
/>
);
}
render() {
const { options, listProps, useSections } = this.props;
let view = null;
if (useSections) {
view = (
<SectionList
sections={options}
renderItem={this.renderRow}
{...listProps}
/>
);
} else {
view = (
<FlatList
data={options}
renderItem={this.renderRow}
{...listProps}
/>
);
}
return view;
}
}
MultiSelect.propTypes = {
options: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
renderRow: PropTypes.func.isRequired,
listProps: PropTypes.object, // eslint-disable-line react/forbid-prop-types
// Passes `selectedRow, allSelectedRows`
onSelectionChange: PropTypes.func,
// `selectedOptions` is an array of keys that are also found in `options`
selectedOptions: PropTypes.array, // eslint-disable-line react/forbid-prop-types
rowStyle: PropTypes.object, // eslint-disable-line react/forbid-prop-types
activeOpacity: PropTypes.number,
underlayColor: PropTypes.string,
useSections: PropTypes.bool,
};
export default MultiSelect;