index.ios.js 2.45 KB
Newer Older
1 2 3 4 5
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */

xwenliang's avatar
xwenliang committed
6
import React, {Component} from 'react';
xwenliang's avatar
xwenliang committed
7 8 9 10 11 12 13
import {
    View,
    Text,
    TouchableOpacity,
    Dimensions,
    StyleSheet,
    AppRegistry
14 15 16 17 18
} from 'react-native';

import Picker from 'react-native-picker';

function createDateData(){
xwenliang's avatar
xwenliang committed
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
    let date = {};
    for(let i=1950;i<2050;i++){
        let month = {};
        for(let j = 1;j<13;j++){
            let day = [];
            if(j === 2){
                for(let k=1;k<29;k++){
                    day.push(k+'');
                }
            }
            else if(j in {1:1, 3:1, 5:1, 7:1, 8:1, 10:1, 12:1}){
                for(let k=1;k<32;k++){
                    day.push(k+'');
                }
            }
            else{
                for(let k=1;k<31;k++){
                    day.push(k+'');
                }
            }
            month[j+''] = day;
        }
        date[i+''] = month;
    }
    return date;
44 45 46 47
};

class PickerTest extends Component {

xwenliang's avatar
xwenliang committed
48 49 50 51 52
    constructor(props, context) {
        super(props, context);
    }

    _onPressHandle() {
xwenliang's avatar
xwenliang committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        Picker.init({
            pickerData: createDateData(),
            selectedValue: ['2015年', '12月', '12日'],
            onPickerConfirm: pickedValue => {
                alert(JSON.stringify(pickedValue));
                console.log(pickedValue);
            },
            onPickerCancel: pickedValue => {
                alert(JSON.stringify(pickedValue));
                console.log(pickedValue);
            },
            onPickerSelect: pickedValue => {
                alert(JSON.stringify(pickedValue));
                console.log(pickedValue);
            }
        });
        Picker.show();
xwenliang's avatar
xwenliang committed
70 71
    }

xwenliang's avatar
xwenliang committed
72
    render() {
xwenliang's avatar
xwenliang committed
73 74 75 76 77 78 79 80
        return (
            <View style={{height: Dimensions.get('window').height}}>
                <TouchableOpacity style={{marginTop: 20}} onPress={this._onPressHandle.bind(this)}>
                    <Text>Click Me</Text>
                </TouchableOpacity>
            </View>
        );
    }
81 82 83
}

const styles = StyleSheet.create({
xwenliang's avatar
xwenliang committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
100 101 102
});

AppRegistry.registerComponent('PickerTest', () => PickerTest);