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

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

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

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

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

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

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

    isPickerShow(fn){
xwenliang's avatar
xwenliang committed
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;
            }
94
            fn && fn(returnValue);
xwenliang's avatar
xwenliang committed
95 96
        });
    }
97
};