DatePicker.js 1.88 KB
Newer Older
1
2
3
import React, { useState } from "react";
import { Button, View, Text, StyleSheet, Pressable } from "react-native";
import DateTimePickerModal from "react-native-modal-datetime-picker";
4
5

const DatePicker = (props) => {
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    const [isDatePickerVisible, setDatePickerVisibility] = useState(false);

    const showDatePicker = () => {
        setDatePickerVisibility(true);
    };

    const hideDatePicker = () => {
        setDatePickerVisibility(false);
    };

    const handleConfirm = (date) => {
        console.log("A date has been picked: ", String(date.toJSON()).split(/T/)[0]);
        props.setDate(String(date.toJSON()).split(/T/)[0])
        hideDatePicker();
    };

22
    return (
23
24
25
26
27
28
29
30
31
32
33
        <View style={style.container}>
            <View style={style.inputTitleArea}>
                <Text style={style.inputTitle}>{props.inputTitle}</Text>
            </View>
            <Pressable
                style={style.dateAreaStyle}
                onPress={showDatePicker}>
                <Text style={style.dateTextStyle}>{props.date}</Text>
            </Pressable>
            <DateTimePickerModal
                isVisible={isDatePickerVisible}
34
                mode="date"
35
36
37
                onConfirm={handleConfirm}
                onCancel={hideDatePicker}
            />
38
39
40
41
        </View>
    );
};

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const style = StyleSheet.create({
    container: {
        height:54,
        flexDirection: 'row',
        alignItems: "center",
        marginHorizontal: 10,
        marginVertical: 3,
        borderWidth: 1.5,
        borderStyle: "solid",
        borderColor: "#1467ff",
        borderRadius: 5,
        backgroundColor: "#f5f5f5",
    },
    inputTitleArea: {
        flex: 1,
    },
    inputTitle: {
        alignSelf: "center",
        color: "#1467ff",
        fontSize: 20,
    },
    dateAreaStyle: {
        flex: 3,
    },
    dateTextStyle: {
        fontSize: 20,
    },

70
71
72
})

export default DatePicker