date_input.tsx 2.6 KB

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