| 1 | import { useState } from "react";
|
| 2 | import { View, Pressable, StyleSheet } from "react-native";
|
| 3 | import { Text } from "@/components/ui/Text";
|
| 4 | import DateTimePicker, {
|
| 5 | DateTimePickerEvent,
|
| 6 | } from "@react-native-community/datetimepicker";
|
| 7 | import { format } from "date-fns";
|
| 8 | import { MaterialIcons } from "@expo/vector-icons";
|
| 9 | import { Colors } from "@/constants/theme";
|
| 10 | import { Label } from "@/components/ui/Input";
|
| 11 |
|
| 12 | interface DatePickerProps {
|
| 13 | label: string;
|
| 14 | initialDate?: Date;
|
| 15 | onChange: (date: Date) => void;
|
| 16 | minimumDate?: Date;
|
| 17 | maximumDate?: Date;
|
| 18 | }
|
| 19 |
|
| 20 | export const DatePickerField: React.FC<DatePickerProps> = ({
|
| 21 | label,
|
| 22 | initialDate = new Date(),
|
| 23 | onChange,
|
| 24 | minimumDate,
|
| 25 | maximumDate,
|
| 26 | }) => {
|
| 27 | const [pickedDate, setPickedDate] = useState<Date | null>(null);
|
| 28 | const [show, setShow] = useState(false);
|
| 29 |
|
| 30 | const date = pickedDate ?? initialDate;
|
| 31 |
|
| 32 | const openPicker = () => setShow(true);
|
| 33 |
|
| 34 | const handleChange = (event: DateTimePickerEvent, selectedDate?: Date) => {
|
| 35 | setShow(false);
|
| 36 |
|
| 37 | if (selectedDate) {
|
| 38 | setPickedDate(selectedDate);
|
| 39 | if (onChange) onChange(selectedDate);
|
| 40 | }
|
| 41 | };
|
| 42 |
|
| 43 | const formatted = format(date, "dd/MM/yyyy");
|
| 44 |
|
| 45 | return (
|
| 46 | <View>
|
| 47 | <Label text={label} />
|
| 48 |
|
| 49 | <Pressable
|
| 50 | onPress={openPicker}
|
| 51 | style={({ pressed }) => [styles.container, pressed && styles.pressed]}
|
| 52 | >
|
| 53 | <Text style={styles.dateText}>{formatted}</Text>
|
| 54 | <MaterialIcons name="calendar-month" size={22} color="#6B7280" />
|
| 55 | </Pressable>
|
| 56 |
|
| 57 | {show && (
|
| 58 | <DateTimePicker
|
| 59 | value={date}
|
| 60 | mode="date"
|
| 61 | display={"default"}
|
| 62 | onChange={handleChange}
|
| 63 | minimumDate={minimumDate}
|
| 64 | maximumDate={maximumDate}
|
| 65 | />
|
| 66 | )}
|
| 67 | </View>
|
| 68 | );
|
| 69 | };
|
| 70 |
|
| 71 | const styles = StyleSheet.create({
|
| 72 | label: {
|
| 73 | fontWeight: "500",
|
| 74 | color: Colors.light.textSecondary,
|
| 75 | fontSize: 12,
|
| 76 | marginBottom: 8,
|
| 77 | letterSpacing: 0.5,
|
| 78 | textTransform: "uppercase",
|
| 79 | },
|
| 80 | container: {
|
| 81 | flexDirection: "row",
|
| 82 | alignItems: "center",
|
| 83 | justifyContent: "space-between",
|
| 84 | backgroundColor: "#F3F4F6", // light gray pill
|
| 85 | paddingVertical: 14,
|
| 86 | paddingHorizontal: 16,
|
| 87 | borderRadius: 10,
|
| 88 | borderWidth: 0.5,
|
| 89 | borderColor: "#E5E7EB",
|
| 90 | minWidth: 220,
|
| 91 | },
|
| 92 | pressed: {
|
| 93 | opacity: 0.8,
|
| 94 | },
|
| 95 | dateText: {
|
| 96 | color: "#111827",
|
| 97 | fontSize: 16,
|
| 98 | fontWeight: "600",
|
| 99 | },
|
| 100 | });
|