import React, { useState } from "react"; import { Modal, StyleSheet, Text, TouchableHighlight, View, Platform, TouchableOpacity, } from "react-native"; import { MaterialCommunityIcons } from "@expo/vector-icons"; import DateTimePicker from "@react-native-community/datetimepicker"; import moment from "moment"; import colors from "../../config/colors"; import { dimensions } from "../../config/dimensions"; const FormDatePicker = (props) => { const { textStyle, defaultDate } = props; const [date, setDate] = useState(moment(defaultDate)); const [show, setShow] = useState(false); const [auxDate, setAuxDate] = useState(moment()); const [mode, setMode] = useState("date"); const [time, setTime] = useState(moment(defaultDate)); //-------------------------- Time picker ainda não está habilitado pra ios -------------------------------- 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) { if (mode == "date") { setDate(moment(selectedDate)); props.onDateChange(selectedDate); setMode("time"); setShow(true); // to show the picker again in time mode } else { setTime(moment(selectedDate)); props.onTimeChange(selectedDate); setShow(false); setMode("date"); } } }; const onCancelPress = () => { setShow(false); }; const onDonePress = () => { setDate(moment(auxDate)); //atualizar a data com a variável auxiliar props.onDateChange(auxDate); setShow(false); }; const formatDate = (date, time) => { time.getHours(); time.getMinuts(); }; const renderDatePicker = () => { return ( { if (mode == "date") { return moment(selectedDate).format("DD/MM/YYYY"); } else { return moment(selectedDate).format("HH:mm"); } }} //formatar a data e hora do selected date display={Platform.OS === "ios" ? "spinner" : "default"} onChange={Platform.OS === "ios" ? onChange : androidOnChange} /> ); }; return ( setShow(true)}> {" "} {date.format("DD/MM/YYYY")} {"\n "} {time.format("HH:mm")} {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: () => {}, onTimeChange: () => {}, }; 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.8, backgroundColor: colors.white, borderRadius: 25, marginVertical: 10, }, dateIcon: { backgroundColor: colors.primary, padding: 8, width: 20, alignItems: "center", borderRadius: 5, flex: 0.2, }, }); export default FormDatePicker;