-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiplePicker.js
120 lines (111 loc) · 3.48 KB
/
MultiplePicker.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
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
} from 'react-native';
export default class MultiplePicker extends Component {
state = {
open: false,
data: this.props.data || [],
count: 0,
};
onOpen() {
this.setState({
open: !this.state.open
});
}
onSelect(item) {
const newState = Object.assign([], this.state.data);
let {count} = this.state;
let {onChange} = this.props;
const index = newState.findIndex(menu => menu.id === item.id)
newState.splice(index, 1, {
...item,
selected: item.selected ? false : true,
});
this.setState({
data: newState,
count: item.selected ? count - 1 : count + 1,
}, () => onChange && onChange(this.getActiveItems()));
}
getActiveItems() {
return this.state.data.filter(item =>
item.selected === true
).map(item => item.id);
}
render() {
const {open, data, count} = this.state;
return (
<View style={{flex: 1}}>
<View>
<TouchableOpacity
activeOpacity={.7}
onPress={() => this.onOpen()}>
<View style={styles.button}>
<Text>{`Selected ${count}`}</Text>
</View>
</TouchableOpacity>
<View style={[styles.dropdown, open ? {} : {opacity:0}]}>
{
data.map((item, index) =>
<TouchableOpacity
key={index}
activeOpacity={.7}
onPress={() => this.onSelect(item)}>
<Text style={[styles.title, item.selected ? {color:'red'} : {}]}>
{item.title}
</Text>
</TouchableOpacity>
)
}
<TouchableOpacity
activeOpacity={.75}
onPress={() => this.onOpen()}
>
<View style={[styles.buttonClose, count > 0 ? {backgroundColor: 'green'}: {}]}>
<Text style={styles.buttonCloseText}>
{count > 0 ? 'Select' : 'Close'}
</Text>
</View>
</TouchableOpacity>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
button: {
width: '100%',
height: 50,
// backgroundColor:'red',
borderBottomWidth: 1,
borderBottomColor: '#ccc',
justifyContent: 'center',
//paddingHorizontal: vertical(15)
},
dropdown: {
width: '100%',
backgroundColor: '#fff',
position: 'absolute',
top: 50,
zIndex: 3,
// opacity: 1,
},
title: {
padding: 15,
flexGrow: 1,
},
buttonClose: {
flex:1,
justifyContent:'center',
alignItems:'center',
height: 50,
backgroundColor:'red'
},
buttonCloseText: {
color: '#fff',
}
});