index.js 2.36 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
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],
    pickerBg: [196, 199, 206, 1],
20
    pickerRowHeight: 24,
xwenliang's avatar
xwenliang committed
21 22 23 24 25 26 27 28 29 30
    wheelFlex: [1, 1, 1],
    pickerData: [],
    selectedValue: [],
    onPickerConfirm(){},
    onPickerCancel(){},
    onPickerSelect(){},
    pickerToolBarFontSize: 16,
    pickerFontSize: 16,
    pickerFontColor: [31, 31 ,31, 1]
};
xwenliang's avatar
xwenliang committed
31 32

export default {
xwenliang's avatar
xwenliang committed
33 34 35 36
    init(params){
        const opt = {
            ...options,
            ...params
xwenliang's avatar
xwenliang committed
37
        };
xwenliang's avatar
xwenliang committed
38
        const fnConf = {
xwenliang's avatar
xwenliang committed
39 40 41 42 43 44
            confirm: opt.onPickerConfirm,
            cancel: opt.onPickerCancel,
            select: opt.onPickerSelect
        };

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

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

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

xwenliang's avatar
xwenliang committed
60
    select(arr, fn) {
61 62 63 64 65 66 67 68
        if(ios){
            Picker.select(arr);
        }
        else if(android){
            Picker.select(arr, err => {
                typeof fn === 'function' && fn(err);
            });
        }
jinlongtao's avatar
jinlongtao committed
69 70
    },

xwenliang's avatar
xwenliang committed
71 72 73 74 75 76 77 78 79 80 81 82
    toggle(){
        this.isPickerShow(show => {
            if(show){
                this.hide();
            }
            else{
                this.show();
            }
        });
    },

    isPickerShow(fn){
xwenliang's avatar
xwenliang committed
83 84 85 86 87 88 89 90 91 92 93
        //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
94 95 96
        });
    }
};