index.js 1.77 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 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
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);
        if(this.inited){
            return;
        }
        this.inited = true;

        NativeAppEventEmitter.addListener('pickerEvent', event => {
            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){
        Picker.isPickerShow(hide => {
            fn(!hide);
        });
    }
};