1.1.0 • Published 6 years ago

react-native-pickdate v1.1.0

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

react-native-pickdate

A simple way to pick date through native date-picker and time-picker inside a modal.

usage

import pickDate from 'react-native-pickdate';

export default class App extends Component<Props> {

    constructor(props) {
        super(props);
        this.state = {
            curSel: '未选择'
        };
    }

    createUpdate(type){
        let formatStr;
        switch (type){
            case 'date':formatStr = 'YYYY-MM-dd';break;
            case 'time':formatStr = 'hh:mm';break;
            case 'datetime':formatStr = 'YYYY-MM-dd hh:mm';break;
        }
        let self = this;
        return (date)=>{
            console.log('date',date);
            let curSel = format(date,formatStr);
            self.setState({curSel});
        }
    }

    render() {
        let {curSel} = this.state;
        return (
            <View style={styles.container}>
                <TouchableOpacity
                    onPress={()=>pickDate(this.createUpdate('date'))}>
                    <View style={styles.button}>
                        <Text>Pick date</Text>
                    </View>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>pickDate(this.createUpdate('time'),pickDate.TYPE.TIME)}>
                    <View style={styles.button}>
                        <Text>Pick time</Text>
                    </View>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>pickDate(this.createUpdate('datetime'),pickDate.TYPE.DATETIME)}>
                    <View style={styles.button}>
                        <Text>Pick datetime</Text>
                    </View>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>pickDate({
                            onConfirm:this.createUpdate('datetime'),
                            type:pickDate.TYPE.DATETIME,
                            is24Hour:false,
                            date:new Date('2000-10-10')
                        })}>
                    <View style={styles.button}>
                        <Text>Pick datetime by options</Text>
                    </View>
                </TouchableOpacity>
                <Text style={styles.text}>{curSel}</Text>
            </View>
        );
    }
}

screenshot

Android ios

Install

npm install --save react-native-pickdate or yarn add react-native-pickdate.

Available options

NameTypeDefaultDescription
cancelTextstring'Cancel'The text on the cancel button on iOS
confirmTextstring'Confirm'The text on the confirm button on iOS
titleTextstring'Pick a date'/'Pick a time'The title text on iOS
onConfirmfuncconfirm handler
onCancelfunccancel handler
is24HourbooltrueIf false, the picker shows an AM/PM chooser on Android
dateDatenew Date()default show date
typestringpickDate.TYPE.DATEdate / time /datetime

Thanks

react-native-root-siblings

react-native-modal-datetime-picker