date_input.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { useState } from "react";
  2. import { StatusBar } from "expo-status-bar";
  3. import { StyleSheet, Text, TouchableOpacity, View, Modal, Pressable, TextInput } from "react-native";
  4. import DatePicker from "react-native-modern-datepicker";
  5. type DateModalProps = {
  6. onDateChange: (date: string) => void;
  7. };
  8. const DateModal: React.FC<DateModalProps> = ({ onDateChange }) => {
  9. const [open, setOpen] = useState(false);
  10. const [date, setDate] = useState("");
  11. //open or close date calendar modal
  12. function openOrCloseModal() {
  13. setOpen(!open);
  14. }
  15. //turn date from yyyy/mm/dd to dd/mm/yyyy && send info back to parent component
  16. function handleChange(propDate: string) {
  17. const parts = propDate.split("/");
  18. const rearrangedParts = [parts[2], parts[1], parts[0].slice(-2)];
  19. const formattedDate = rearrangedParts.join("/");
  20. setDate(formattedDate);
  21. onDateChange(formattedDate);
  22. }
  23. return (
  24. <View style={styles.container}>
  25. <Pressable onPress={openOrCloseModal} style={[styles.inputContainer]}>
  26. <Text style={styles.placeholder}>{date ? date : "DD/MM/YY"}</Text>
  27. </Pressable>
  28. <Modal animationType="slide" transparent={true} visible={open}>
  29. <View style={styles.centeredView}>
  30. <View style={styles.modalView}>
  31. <DatePicker mode="calendar" selected={date} onDateChange={handleChange} />
  32. <TouchableOpacity onPress={openOrCloseModal}>
  33. <Text style={styles.closeButton}>Close</Text>
  34. </TouchableOpacity>
  35. </View>
  36. </View>
  37. </Modal>
  38. <StatusBar style="auto" />
  39. </View>
  40. );
  41. };
  42. const styles = StyleSheet.create({
  43. container: {
  44. flex: 1,
  45. alignItems: "center",
  46. justifyContent: "center",
  47. },
  48. inputContainer: {
  49. width: "100%",
  50. height: 60,
  51. maxWidth: "100%",
  52. flexDirection: "row",
  53. alignItems: "center",
  54. borderWidth: 1,
  55. borderColor: "#bbbbbb",
  56. borderRadius: 10,
  57. padding: 15,
  58. margin: 10,
  59. },
  60. placeholder: {
  61. fontSize: 16,
  62. color: "#888",
  63. },
  64. centeredView: {
  65. flex: 1,
  66. justifyContent: "center",
  67. alignItems: "center",
  68. marginTop: 22,
  69. },
  70. modalView: {
  71. margin: 20,
  72. backgroundColor: "white",
  73. borderRadius: 20,
  74. width: "90%",
  75. padding: 20,
  76. alignItems: "center",
  77. shadowColor: "#000",
  78. shadowOffset: {
  79. width: 0,
  80. height: 2,
  81. },
  82. shadowOpacity: 0.25,
  83. shadowRadius: 4,
  84. elevation: 5,
  85. },
  86. closeButton: { color: "#3f4f5c", padding: 20, borderWidth: 1, borderColor: "#bbbbbb", borderRadius: 15 },
  87. });
  88. export default DateModal;