index.js 1.92 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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
let ios = Platform.OS === 'ios';
let android = Platform.OS === 'android';
let Picker = NativeModules.BEEPickerManager;

export default {

    init(options){
        let opt = {
            isLoop: false,
            pickerConfirmBtnText: '确认',
            pickerCancelBtnText: '取消',
            pickerTitleText: '请选择',
            pickerBg: [196, 199, 206, 1],
            pickerToolBarBg: [232, 232, 232, 1],
            pickerTitleColor: [20, 20, 20, 1],
            pickerCancelBtnColor: [1, 186, 245, 1],
            pickerConfirmBtnColor: [1, 186, 245, 1],
            onPickerConfirm(){},
            onPickerCancel(){},
            onPickerSelect(){},
            ...options
        };
        let fnConf = {
            confirm: opt.onPickerConfirm,
            cancel: opt.onPickerCancel,
            select: opt.onPickerSelect
        };

        Picker._init(opt);
xwenliang's avatar
xwenliang committed
36 37 38
        //there are no `removeListener` for NativeAppEventEmitter & DeviceEventEmitter
        this.listener && this.listener.remove();
        this.listener = NativeAppEventEmitter.addListener('pickerEvent', event => {
xwenliang's avatar
xwenliang committed
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
            if(ios){
                fnConf[event['type']](event['selectedValue']);
            }
            else if(android){
                for (let i in event){
                    typeof fnConf[i] === 'function' && fnConf[i](event[i]);
                }
            }
        });
    },

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

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

    toggle(){
        this.isPickerShow(show => {
            if(show){
                this.hide();
            }
            else{
                this.show();
            }
        });
    },

    isPickerShow(fn){
xwenliang's avatar
xwenliang committed
70 71 72
        //android return show or not, ios return hide or not...
        Picker.isPickerShow(status => {
            fn(android ? status : !status);
xwenliang's avatar
xwenliang committed
73 74 75
        });
    }
};