import React, { useState } from "react"; import { Modal, StyleSheet, Text, TouchableHighlight, View, Platform, TouchableOpacity, } from "react-native"; import { FontAwesome5 } from "@expo/vector-icons"; import DateTimePicker from "@react-native-community/datetimepicker"; import moment from "moment"; import colors from "../../config/colors"; const FormDatePicker = (props) => { const { textStyle, defaultDate } = props; const [date, setDate] = useState(moment(defaultDate)); const [show, setShow] = useState(false); const [auxDate, setAuxDate] = useState(moment()); const onChange = (e, selectedDate) => { setAuxDate(moment(selectedDate)); // variavel auxiliar para não alterar a data com a rolagem, apenas com o done }; const androidOnChange = (e, selectedDate) => { setShow(false); if (selectedDate) { setDate(moment(selectedDate)); props.onDateChange(selectedDate); } }; const onCancelPress = () => { //setDate(moment(defaultDate)); setShow(false); //console.log(moment(defaultDate).format("DD-MM-YYYY")) }; const onDonePress = () => { setDate(moment(auxDate)); //atualizar a data com a variável auxiliar props.onDateChange(auxDate); setShow(false); //console.log("done"); //console.log(date.format("DD-MM-YYYY")); }; const renderDatePicker = () => { return ( { return moment(selectedDate).format("DD/MM/YYYY"); }} //formatar a data do selected date display={Platform.OS === "ios" ? "spinner" : "default"} onChange={Platform.OS === "ios" ? onChange : androidOnChange} /> ); }; return ( setShow(true)}> {date.format("DD/MM/YYYY")} {Platform.OS !== "ios" && show && renderDatePicker()} {Platform.OS === "ios" && show && ( setShow(false)} > setShow(false)} > console.log("datepicker clicked")} > {renderDatePicker()} onCancelPress()} style={[styles.btnText, styles.btnCancel]} > Cancelar onDonePress()} style={[styles.btnText, styles.btnDone]} > Confirmar )} ); }; FormDatePicker.defaultProps = { textStyle: {}, defaultDate: moment(), onDateChange: () => { }, }; const styles = StyleSheet.create({ btnCancel: { left: 0, }, btnDone: { right: 0, }, btnText: { position: "absolute", top: 0, height: 42, paddingHorizontal: 20, flexDirection: "row", alignItems: "center", justifyContent: "center", }, dateInput: { flex: 0.97, backgroundColor: colors.light, borderRadius: 25, padding: 18, marginVertical: 10, } }); export default FormDatePicker;