index.js 2.28 KB
Newer Older
xwenliang's avatar
xwenliang committed
1
import {
xwenliang's avatar
xwenliang committed
2 3 4
    Platform,
    NativeModules,
    NativeAppEventEmitter
xwenliang's avatar
xwenliang committed
5 6
} from 'react-native';

xwenliang's avatar
xwenliang committed
7 8 9 10 11 12 13 14 15
let ios = Platform.OS === 'ios';
let android = Platform.OS === 'android';
let Picker = NativeModules.BEEPickerManager;

export default {

    init(options){
        let opt = {
            isLoop: false,
xwenliang's avatar
xwenliang committed
16 17 18
            pickerConfirmBtnText: 'confirm',
            pickerCancelBtnText: 'cancel',
            pickerTitleText: 'pls select',
xwenliang's avatar
xwenliang committed
19
            pickerConfirmBtnColor: [1, 186, 245, 1],
xwenliang's avatar
xwenliang committed
20 21 22 23 24 25 26
            pickerCancelBtnColor: [1, 186, 245, 1],
            pickerTitleColor: [20, 20, 20, 1],
            pickerToolBarBg: [232, 232, 232, 1],
            pickerBg: [196, 199, 206, 1],
            wheelFlex: [1, 1, 1],
            pickerData: [],
            selectedValue: [],
xwenliang's avatar
xwenliang committed
27 28 29
            onPickerConfirm(){},
            onPickerCancel(){},
            onPickerSelect(){},
xwenliang's avatar
xwenliang committed
30 31 32 33
            //4.0.12 add
            pickerToolBarFontSize: 16,
            pickerFontSize: 16,
            pickerFontColor: [31, 31 ,31, 1],
xwenliang's avatar
xwenliang committed
34 35 36 37 38 39 40 41 42
            ...options
        };
        let fnConf = {
            confirm: opt.onPickerConfirm,
            cancel: opt.onPickerCancel,
            select: opt.onPickerSelect
        };

        Picker._init(opt);
xwenliang's avatar
xwenliang committed
43 44 45
        //there are no `removeListener` for NativeAppEventEmitter & DeviceEventEmitter
        this.listener && this.listener.remove();
        this.listener = NativeAppEventEmitter.addListener('pickerEvent', event => {
xwenliang's avatar
xwenliang committed
46
            fnConf[event['type']](event['selectedValue'], event['selectedIndex']);
xwenliang's avatar
xwenliang committed
47 48 49 50 51 52 53 54 55 56 57
        });
    },

    show(){
        Picker.show();
    },

    hide(){
        Picker.hide();
    },

jinlongtao's avatar
jinlongtao committed
58 59 60 61
    select(data) {
        Picker.select(data)
    },

xwenliang's avatar
xwenliang committed
62 63 64 65 66 67 68 69 70 71 72 73
    toggle(){
        this.isPickerShow(show => {
            if(show){
                this.hide();
            }
            else{
                this.show();
            }
        });
    },

    isPickerShow(fn){
xwenliang's avatar
xwenliang committed
74 75 76 77 78 79 80 81 82 83 84
        //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);
xwenliang's avatar
xwenliang committed
85 86 87
        });
    }
};