import React, { useEffect, useState } from "react"; import { StyleProp, Pressable, View } from "react-native"; import moment from "moment"; import DatePickerRN, { DatePickerProps } from "react-native-date-picker"; import { colors } from "../../assets/colors"; import styles from "./DatePickerStyles"; import AppText from "../Text/AppText"; import { BaseProps } from "../../Utils/BaseProps"; import { DatePickerEnums } from "./DatePickerEnums"; import { errorStyle } from "../../Styles/BaseStyles"; export interface DTProps extends DatePickerProps, BaseProps { datePickerStyle?: StyleProp; mode?: "date" | "time" | "datetime"; date: Date; onDateChange?: (date: Date) => void | undefined; dateFormat?: string; testID?: string; placeholderStyle?: StyleProp; } const DatePicker = (props: DTProps) => { const { containerStyle, labelStyle, datePickerStyle, mode = DatePickerEnums.DATE, disabled = false, date, maximumDate = new Date(), label, hideLabel = false, contentStyle, dateFormat = "MM-DD-YYYY", modal = true, errorMessage, errorVisibility, showErrorMessage = true, assistiveText, assistiveTextStyle, onDateChange, testID, placeholder = "MM-DD-YYYY", placeholderColor = colors.grey, placeholderStyle, } = props; // Date Picker State const [show, setShow] = useState(false); const [dateInPicker, setDateInPicker] = useState(date); const [defaultDate, setDefaultDate] = useState(new Date()); useEffect(() => { if (date) { setDateInPicker(date); } }, [date]); return ( setShow(!show)} style={[ styles.containerStyle, errorStyle(errorVisibility, errorMessage), containerStyle, ]} disabled={disabled} > {hideLabel ? null : ( {label} )} {dateInPicker ? ( moment(dateInPicker).format(dateFormat) ) : ( {placeholder} )} { if (modal) { return; } if (onDateChange) { onDateChange(changedDate); } }} onConfirm={(changedDate) => { setShow(false); setDateInPicker(changedDate); if (onDateChange) { onDateChange(changedDate); } setDefaultDate(changedDate); }} onCancel={() => { setShow(false); }} /> {assistiveText && !errorVisibility ? ( {assistiveText} ) : null} {errorVisibility && showErrorMessage && errorMessage ? ( {errorMessage} ) : null} ); }; export default DatePicker;