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

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

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

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

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

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

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

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

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