-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (100 loc) · 2.4 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
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types';
import {
TouchableOpacity,
TouchableNativeFeedback,
StyleSheet,
Image,
Text,
Platform,
View,
} from 'react-native'
const styles = StyleSheet.create({
checkboxContainer: {
flexDirection: 'row',
alignItems: 'center'
},
checkbox: {
width: 30,
height: 30
},
labelContainer: {
marginLeft: 10,
marginRight: 10
},
label: {
fontSize: 16,
color: '#222'
}
});
class Checkboxes extends PureComponent {
state = {
checked: []
}
static Container = Platform.OS === 'ios' ? TouchableOpacity : TouchableNativeFeedback
static defaultProps = {
labels: [],
checkedImage: require('./img/checked.png'),
uncheckedImage: require('./img/unchecked.png')
}
static propTypes = {
labels: PropTypes.arrayOf(PropTypes.string)
}
componentDidMount() {
const checked = []
this.props.labels.forEach(() => checked.push(false))
this.setState({ checked })
}
onCheck = (index) => () => {
this.setState(previousState => {
previousState.checked[index] = !previousState.checked[index]
return { previousState }
},
() => {
const checkboxes = this.state.checked.map((checked, index) => checked && this.props.labels[index]).filter(labels => labels)
this.props.onChange && this.props.onChange({ checkboxes })
}
)
}
checkbox = (label, index) => {
const { checked } = this.state
const CheckboxContainer = Checkboxes.Container
const {
checkedImage,
uncheckedImage
} = this.props
return (
<CheckboxContainer
key={ index }
style={ styles.checkboxContainer }
onPress={ this.onCheck(index) }
>
<View>
<Image
style={ styles.checkbox }
source={ checked[index] ? checkedImage : uncheckedImage }
/>
</View>
<View style={ styles.labelContainer }>
<Text style={ styles.label }>
{ label }
</Text>
</View>
</CheckboxContainer>
);
};
renderCheckboxes = () => {
const { labels } = this.props;
const checkboxes = []
labels.forEach((label, index) => checkboxes.push(this.checkbox(label, index)))
return checkboxes
}
render() {
return (
<View>
{ this.renderCheckboxes() }
</View>
);
}
}
export default Checkboxes;