Skip to content

Commit dcb5de5

Browse files
committed
Initial
1 parent 6703cbf commit dcb5de5

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
package-lock.json

example/example.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import React, {Component} from 'react';
2+
import {StyleSheet, Text, View, Button, TouchableOpacity} from 'react-native';
3+
import Picker from 'react-native-simple-modal-picker'
4+
5+
export default class Example extends Component {
6+
constructor(props){
7+
super(props)
8+
this.state={
9+
selectedIndex: 0
10+
}
11+
this.data=[
12+
{
13+
name:'Option 1',
14+
value: '1'
15+
},
16+
{
17+
name:'Option 2',
18+
value: '2'
19+
},
20+
{
21+
name:'Option 3',
22+
value: '3'
23+
},
24+
{
25+
name:'Option 4',
26+
value: '4'
27+
},
28+
{
29+
name:'Option 5',
30+
value: '5'
31+
}
32+
]
33+
}
34+
render() {
35+
return (
36+
<View style={styles.container}>
37+
{this.simplePickerView()}
38+
{this.customRowPickerView()}
39+
{this.dropDownView()}
40+
</View>
41+
);
42+
}
43+
44+
simplePickerView(){
45+
return(
46+
<View>
47+
<Picker
48+
ref={instance => this.simplePicker = instance}
49+
data={this.data}
50+
label={'name'}
51+
value={'value'}
52+
onValueChange={(value) => alert(value + ' selected')} />
53+
54+
<View style={styles.subContainer}>
55+
<Button
56+
title={'Open Simple Picker'}
57+
onPress={() => this.simplePicker.setModalVisible(true)} />
58+
</View>
59+
</View>
60+
)
61+
}
62+
63+
customRowPickerView(){
64+
return(
65+
<View>
66+
<Picker
67+
ref={instance => this.customRowPicker = instance}
68+
data={this.data}
69+
label={'name'}
70+
value={'value'}
71+
onValueChange={(value) => alert(value + ' selected')}
72+
renderRow={(rowData) => <Text style={styles.rowStyle}>{rowData.name}</Text>} />
73+
<View style={styles.subContainer}>
74+
<Button
75+
title={'Open Cutome Row Picker'}
76+
onPress={() => this.customRowPicker.setModalVisible(true)} />
77+
</View>
78+
</View>
79+
)
80+
}
81+
82+
dropDownView(){
83+
return(
84+
<View>
85+
<Picker
86+
ref={instance => this.dropDownPicker = instance}
87+
data={this.data}
88+
label={'name'}
89+
value={'value'}
90+
onValueChange={(value, selectedIndex) => this.setState({selectedIndex})} />
91+
<View style={styles.subContainer}>
92+
<TouchableOpacity style={styles.dropDownContainer} onPress={() => this.dropDownPicker.setModalVisible(true)}>
93+
<Text style={styles.dropDownText}>{this.data[this.state.selectedIndex].name}</Text>
94+
</TouchableOpacity>
95+
</View>
96+
</View>
97+
)
98+
}
99+
}
100+
101+
const styles = StyleSheet.create({
102+
container: {
103+
flex: 1,
104+
justifyContent: 'center',
105+
backgroundColor: '#F5FCFF',
106+
},
107+
subContainer:{
108+
margin: 8
109+
},
110+
rowStyle:{
111+
backgroundColor: '#FFF',
112+
color: '#333',
113+
padding: 8,
114+
fontSize: 20
115+
},
116+
dropDownContainer:{
117+
borderBottomWidth: 1,
118+
padding: 8
119+
},
120+
dropDownText:{
121+
fontSize: 20,
122+
margin: 8
123+
}
124+
});

index.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import React, { Component } from "react";
2+
import {
3+
Modal,
4+
Text,
5+
TouchableHighlight,
6+
View,
7+
StyleSheet,
8+
ListView
9+
} from "react-native";
10+
import PropTypes from "prop-types";
11+
12+
export default class ModalPicker extends Component {
13+
constructor(props) {
14+
super(props);
15+
const ds = new ListView.DataSource({
16+
rowHasChanged: (r1, r2) => r1 !== r2
17+
});
18+
this.state = {
19+
dataSource: ds.cloneWithRows(this.props.data),
20+
modalVisible: false
21+
};
22+
}
23+
24+
setModalVisible(visible) {
25+
this.setState({ modalVisible: visible });
26+
}
27+
28+
render() {
29+
return (
30+
<View
31+
style={{
32+
backgroundColor: "transparent",
33+
flex: 1,
34+
position: "absolute"
35+
}}
36+
>
37+
<Modal
38+
animationType="fade"
39+
transparent={true}
40+
visible={this.state.modalVisible}
41+
onRequestClose={() => {
42+
this.setModalVisible(false);
43+
}}
44+
>
45+
<TouchableHighlight
46+
style={styles.container}
47+
onPress={() => this.setModalVisible(false)}
48+
underlayColor={"#333333cc"}
49+
>
50+
<View>
51+
<ListView
52+
dataSource={this.state.dataSource}
53+
renderRow={(rowData, sectionID, rowID, higlightRow) => {
54+
return (
55+
<TouchableHighlight
56+
underlayColor={"transparent"}
57+
onPress={() => {
58+
this.setModalVisible(false);
59+
this.props.onValueChange(rowData[this.props.value], rowID);
60+
}}
61+
>
62+
{this.props.renderRow ? (
63+
this.props.renderRow(rowData, rowID)
64+
) : (
65+
<Text style={styles.itemText}>
66+
{rowData[this.props.label]}
67+
</Text>
68+
)}
69+
</TouchableHighlight>
70+
);
71+
}}
72+
/>
73+
</View>
74+
</TouchableHighlight>
75+
</Modal>
76+
</View>
77+
);
78+
}
79+
}
80+
81+
ModalPicker.propTypes = {
82+
data: PropTypes.array.isRequired,
83+
value: PropTypes.string.isRequired,
84+
label: PropTypes.string.isRequired,
85+
onValueChange: PropTypes.func,
86+
renderRow: PropTypes.func
87+
};
88+
89+
const styles = StyleSheet.create({
90+
container: {
91+
flex: 1,
92+
justifyContent: "center",
93+
backgroundColor: "#333333cc",
94+
padding: 16
95+
},
96+
itemText: {
97+
backgroundColor: "#fff",
98+
padding: 16,
99+
fontSize: 18,
100+
color: "#222",
101+
borderTopWidth: 1,
102+
borderColor: "#CCC"
103+
}
104+
});

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "react-native-simple-modal-picker",
3+
"version": "0.1.0",
4+
"description": "Simple Picker/DropDown/Chooser/Selector for React Native",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/binbytes/react-native-simple-modal-picker.git"
12+
},
13+
"keywords": [
14+
"react-native",
15+
"react",
16+
"picker",
17+
"modal-picker",
18+
"dropdown",
19+
"chooser",
20+
"selector",
21+
"modal-selector",
22+
"modal-dropdown"
23+
],
24+
"author": "Bhavik Charola <bhavikcharola@gmail.com>",
25+
"license": "ISC",
26+
"bugs": {
27+
"url": "https://github.com/binbytes/react-native-simple-modal-picker/issues"
28+
},
29+
"homepage": "https://github.com/binbytes/react-native-simple-modal-picker#readme",
30+
"dependencies": {
31+
"prop-types": "^15.6.2",
32+
"react": "^16.4.2"
33+
}
34+
}

0 commit comments

Comments
 (0)