forked from beefe/react-native-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (89 loc) · 2.39 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
import {
Platform,
NativeModules,
NativeAppEventEmitter
} from 'react-native';
const ios = Platform.OS === 'ios';
const android = Platform.OS === 'android';
const Picker = NativeModules.BEEPickerManager;
const options = {
isLoop: false,
pickerConfirmBtnText: 'confirm',
pickerCancelBtnText: 'cancel',
pickerTitleText: 'pls select',
pickerConfirmBtnColor: [1, 186, 245, 1],
pickerCancelBtnColor: [1, 186, 245, 1],
pickerTitleColor: [20, 20, 20, 1],
pickerToolBarBg: [232, 232, 232, 1],
pickerTextEllipsisLen: 6,
pickerBg: [196, 199, 206, 1],
pickerRowHeight: 24,
wheelFlex: [1, 1, 1],
pickerData: [],
selectedValue: [],
onPickerConfirm(){},
onPickerCancel(){},
onPickerSelect(){},
pickerToolBarFontSize: 16,
pickerFontSize: 16,
pickerFontColor: [31, 31 ,31, 1]
};
export default {
init(params){
const opt = {
...options,
...params
};
const fnConf = {
confirm: opt.onPickerConfirm,
cancel: opt.onPickerCancel,
select: opt.onPickerSelect
};
Picker._init(opt);
//there are no `removeListener` for NativeAppEventEmitter & DeviceEventEmitter
this.listener && this.listener.remove();
this.listener = NativeAppEventEmitter.addListener('pickerEvent', event => {
fnConf[event['type']](event['selectedValue'], event['selectedIndex']);
});
},
show(){
Picker.show();
},
hide(){
Picker.hide();
},
select(arr, fn) {
if(ios){
Picker.select(arr);
}
else if(android){
Picker.select(arr, err => {
typeof fn === 'function' && fn(err);
});
}
},
toggle(){
this.isPickerShow(show => {
if(show){
this.hide();
}
else{
this.show();
}
});
},
isPickerShow(fn){
//android return two params: err(error massage) and status(show or not)
//ios return only one param: hide or not...
Picker.isPickerShow((err, status) => {
let returnValue = null;
if(android){
returnValue = err ? false : status;
}
else if(ios){
returnValue = !err;
}
fn(returnValue);
});
}
};