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

xwenliang's avatar
xwenliang committed
6 7 8 9 10 11 12 13
import React, {Component} from 'react'
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    constructor(props, context) {
        super(props, context);
    }

    _onPressHandle() {
        this.picker.toggle();
    }

    render() {
        return (
            <View style={{height: Dimensions.get('window').height}}>
                <TouchableOpacity style={{marginTop: 20}} onPress={this._onPressHandle.bind(this)}>
                    <Text>Click Me</Text>
                </TouchableOpacity>
                <Picker
                    ref={picker => this.picker = picker}
                    style={{height: 260}}
                    showDuration={300}
                    showMask={true}
                    pickerData={createDateData()}
                    selectedValue={['2015年', '12月', '12日']}
                    onPickerDone={(pickedValue) => {
                        alert(JSON.stringify(pickedValue));
                        console.log(pickedValue);
                    }}
                />
            </View>
        );
    }
77 78 79
}

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

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