-
Notifications
You must be signed in to change notification settings - Fork 41
/
index.js
180 lines (147 loc) · 4.79 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
'use strict';
import React, { PropTypes } from 'react'
import {
Text,
TouchableOpacity,
View,
Image,
ListView
} from 'react-native';
import BaseComponent from './BaseComponent'
import Styles from './styles'
const propTypes = {
options: React.PropTypes.array.isRequired,
selectedOptions: React.PropTypes.array,
maxSelectedOptions: React.PropTypes.number,
onSelection: React.PropTypes.func,
renderIndicator: React.PropTypes.func,
renderSeparator: React.PropTypes.func,
renderRow: React.PropTypes.func,
renderText: React.PropTypes.func,
style: View.propTypes.style,
optionStyle: View.propTypes.style,
disabled: PropTypes.bool
};
const defaultProps = {
options: [],
selectedOptions: [],
onSelection(option){},
style:{},
optionStyle:{},
disabled: false
};
class MultipleChoice extends BaseComponent {
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => true});
this.ds = ds;
this.state = {
dataSource: ds.cloneWithRows(this.props.options),
selectedOptions: this.props.selectedOptions || [],
disabled: this.props.disabled
};
this._bind(
'_renderRow',
'_selectOption',
'_isSelected',
'_updateSelectedOptions'
);
}
componentWillReceiveProps(nextProps) {
this._updateSelectedOptions(nextProps.selectedOptions);
this.setState({
disabled: nextProps.disabled
});
}
_updateSelectedOptions(selectedOptions) {
this.setState({
selectedOptions,
dataSource: this.ds.cloneWithRows(this.props.options)
});
}
_validateMaxSelectedOptions() {
const maxSelectedOptions = this.props.maxSelectedOptions;
const selectedOptions = this.state.selectedOptions;
if (maxSelectedOptions && selectedOptions.length > 0 && selectedOptions.length >= maxSelectedOptions) {
selectedOptions.splice(0, 1);
}
this._updateSelectedOptions(selectedOptions);
}
_selectOption(selectedOption) {
let selectedOptions = this.state.selectedOptions;
const index = selectedOptions.indexOf(selectedOption);
if (index === -1) {
this._validateMaxSelectedOptions();
selectedOptions.push(selectedOption);
} else {
selectedOptions.splice(index, 1);
}
this._updateSelectedOptions(selectedOptions);
//run callback
this.props.onSelection(selectedOption);
}
_isSelected(option) {
return this.state.selectedOptions.indexOf(option) !== -1;
}
_renderIndicator(option) {
if (this._isSelected(option)) {
if(typeof this.props.renderIndicator === 'function') {
return this.props.renderIndicator(option);
}
return (
<Image
style={Styles.optionIndicatorIcon}
source={require('./assets/images/check.png')}
/>
);
}
}
_renderSeparator(option) {
if(typeof this.props.renderSeparator === 'function') {
return this.props.renderSeparator(option);
}
return (<View style={Styles.separator}></View>);
}
_renderText(option) {
if(typeof this.props.renderText === 'function') {
return this.props.renderText(option);
}
return (<Text>{option}</Text>);
}
_renderRow(option) {
if(typeof this.props.renderRow === 'function') {
return this.props.renderRow(option);
}
const disabled = this.state.disabled;
return (
<View style={this.props.optionStyle}>
<TouchableOpacity
activeOpacity={disabled ? 1 : 0.7}
onPress={!disabled ? ()=>{this._selectOption(option)} : null}
>
<View>
<View
style={Styles.row}
>
<View style={Styles.optionLabel}>{this._renderText(option)}</View>
<View style={Styles.optionIndicator}>{this._renderIndicator(option)}</View>
</View>
</View>
</TouchableOpacity>
{this._renderSeparator(option)}
</View>
);
}
render() {
return (
<ListView
style={[Styles.list, this.props.style]}
dataSource={this.state.dataSource}
renderRow={this._renderRow}
/>
);
}
};
MultipleChoice.propTypes = propTypes;
MultipleChoice.defaultProps = defaultProps;
module.exports = MultipleChoice;