eletrotupi / tcc/ frontend/components/ui/TimePicker.tsxc901c35af9c84b6a29e5185545ced1129f33b8f4
2.0 KB | 85 lines Raw
1
import { useState } from "react";
2
import { TouchableOpacity, View, StyleSheet } from "react-native";
3
import { Text } from "@/components/ui/Text";
4
import DateTimePicker, {
5
  DateTimePickerEvent,
6
} from "@react-native-community/datetimepicker";
7
import { Ionicons } from "@expo/vector-icons";
8

9
type Props = {
10
  value: Date;
11
  onChange: (date: Date) => void;
12
};
13

14
function formatTime(date: Date): string {
15
  const hours = date.getHours();
16
  const minutes = date.getMinutes();
17
  const ampm = hours >= 12 ? "PM" : "AM";
18
  const h = hours % 12 || 12;
19
  const m = minutes.toString().padStart(2, "0");
20
  return `${h.toString().padStart(2, "0")}:${m}  ${ampm}`;
21
}
22

23
export const TimePicker: React.FC<Props> = ({ value, onChange }) => {
24
  const [show, setShow] = useState(false);
25

26
  const handleChange = (_: DateTimePickerEvent, selected?: Date) => {
27
    setShow(false);
28

29
    if (selected) {
30
      onChange(selected);
31
    }
32
  };
33

34
  return (
35
    <View>
36
      <TouchableOpacity
37
        style={styles.input}
38
        onPress={() => setShow(true)}
39
        activeOpacity={0.7}
40
      >
41
        <Text style={styles.timeText}>{formatTime(value)}</Text>
42
        <Ionicons
43
          name="time-outline"
44
          size={20}
45
          color="#8E8E9A"
46
          strokeWidth={1.5}
47
        />
48
      </TouchableOpacity>
49

50
      {show && (
51
        <DateTimePicker
52
          value={value}
53
          mode="time"
54
          display="default"
55
          onChange={handleChange}
56
          onTouchCancel={() => setShow(false)}
57
        />
58
      )}
59
    </View>
60
  );
61
};
62

63
const styles = StyleSheet.create({
64
  input: {
65
    flexDirection: "row",
66
    alignItems: "center",
67
    justifyContent: "space-between",
68
    backgroundColor: "#FFFFFF",
69
    borderRadius: 12,
70
    paddingHorizontal: 16,
71
    paddingVertical: 14,
72
    shadowColor: "#000",
73
    shadowOffset: { width: 0, height: 1 },
74
    shadowOpacity: 0.06,
75
    shadowRadius: 4,
76
    elevation: 1,
77
  },
78
  timeText: {
79
    fontSize: 16,
80
    fontWeight: "500",
81
    color: "#1C1C1E",
82
    letterSpacing: 0.5,
83
    fontVariant: ["tabular-nums"],
84
  },
85
});