@izonnet-web-solution/react-native-date-time-picker-kit 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DateTimePickerKit.podspec +22 -0
- package/LICENSE +20 -0
- package/README.md +652 -2
- package/android/build.gradle +36 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/datetimepickerkit/DatePickerHelper.kt +185 -0
- package/android/src/main/java/com/datetimepickerkit/DateTimePickerHelper.kt +214 -0
- package/android/src/main/java/com/datetimepickerkit/DateTimePickerKitModule.kt +258 -0
- package/android/src/main/java/com/datetimepickerkit/DateTimePickerKitPackage.kt +30 -0
- package/android/src/main/java/com/datetimepickerkit/TimePickerHelper.kt +208 -0
- package/ios/DateTimePickerKit.h +11 -0
- package/ios/DateTimePickerKit.mm +84 -0
- package/ios/DateTimePickerModule.swift +474 -0
- package/lib/module/NativeDateTimePickerKit.js +5 -0
- package/lib/module/NativeDateTimePickerKit.js.map +1 -0
- package/lib/module/components/CalendarPicker.js +419 -0
- package/lib/module/components/CalendarPicker.js.map +1 -0
- package/lib/module/components/ClockPicker.js +273 -0
- package/lib/module/components/ClockPicker.js.map +1 -0
- package/lib/module/components/DatePicker.js +181 -0
- package/lib/module/components/DatePicker.js.map +1 -0
- package/lib/module/components/DateRangePicker.js +158 -0
- package/lib/module/components/DateRangePicker.js.map +1 -0
- package/lib/module/components/DateTimePicker.js +674 -0
- package/lib/module/components/DateTimePicker.js.map +1 -0
- package/lib/module/components/MonthPicker.js +145 -0
- package/lib/module/components/MonthPicker.js.map +1 -0
- package/lib/module/components/TimePicker.js +142 -0
- package/lib/module/components/TimePicker.js.map +1 -0
- package/lib/module/components/TimeSlotPicker.js +118 -0
- package/lib/module/components/TimeSlotPicker.js.map +1 -0
- package/lib/module/components/WheelPicker.js +159 -0
- package/lib/module/components/WheelPicker.js.map +1 -0
- package/lib/module/components/YearPicker.js +151 -0
- package/lib/module/components/YearPicker.js.map +1 -0
- package/lib/module/components/common/ModalWrapper.js +136 -0
- package/lib/module/components/common/ModalWrapper.js.map +1 -0
- package/lib/module/components/common/PickerHeader.js +82 -0
- package/lib/module/components/common/PickerHeader.js.map +1 -0
- package/lib/module/components/common/PresetsBar.js +66 -0
- package/lib/module/components/common/PresetsBar.js.map +1 -0
- package/lib/module/errors/DateTimePickerError.js +12 -0
- package/lib/module/errors/DateTimePickerError.js.map +1 -0
- package/lib/module/hooks/useDateTimePicker.js +73 -0
- package/lib/module/hooks/useDateTimePicker.js.map +1 -0
- package/lib/module/index.js +40 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/localization/index.js +6 -0
- package/lib/module/localization/index.js.map +1 -0
- package/lib/module/localization/locales.js +186 -0
- package/lib/module/localization/locales.js.map +1 -0
- package/lib/module/localization/types.js +2 -0
- package/lib/module/localization/types.js.map +1 -0
- package/lib/module/localization/useLocale.js +176 -0
- package/lib/module/localization/useLocale.js.map +1 -0
- package/lib/module/native/NativeDateTimePicker.js +110 -0
- package/lib/module/native/NativeDateTimePicker.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/presets/datePresets.js +75 -0
- package/lib/module/presets/datePresets.js.map +1 -0
- package/lib/module/presets/index.js +4 -0
- package/lib/module/presets/index.js.map +1 -0
- package/lib/module/slots/index.js +4 -0
- package/lib/module/slots/index.js.map +1 -0
- package/lib/module/slots/timeSlots.js +59 -0
- package/lib/module/slots/timeSlots.js.map +1 -0
- package/lib/module/theme/defaultTheme.js +91 -0
- package/lib/module/theme/defaultTheme.js.map +1 -0
- package/lib/module/theme/index.js +6 -0
- package/lib/module/theme/index.js.map +1 -0
- package/lib/module/theme/types.js +2 -0
- package/lib/module/theme/types.js.map +1 -0
- package/lib/module/theme/useTheme.js +81 -0
- package/lib/module/theme/useTheme.js.map +1 -0
- package/lib/module/types/events.js +4 -0
- package/lib/module/types/events.js.map +1 -0
- package/lib/module/types/index.js +7 -0
- package/lib/module/types/index.js.map +1 -0
- package/lib/module/types/native.js +2 -0
- package/lib/module/types/native.js.map +1 -0
- package/lib/module/types/presentation.js +2 -0
- package/lib/module/types/presentation.js.map +1 -0
- package/lib/module/types/props.js +4 -0
- package/lib/module/types/props.js.map +1 -0
- package/lib/module/utils/constants.js +14 -0
- package/lib/module/utils/constants.js.map +1 -0
- package/lib/module/utils/dateConversion.js +224 -0
- package/lib/module/utils/dateConversion.js.map +1 -0
- package/lib/module/utils/dateFormatting.js +102 -0
- package/lib/module/utils/dateFormatting.js.map +1 -0
- package/lib/module/utils/dateValidation.js +161 -0
- package/lib/module/utils/dateValidation.js.map +1 -0
- package/lib/module/utils/index.js +7 -0
- package/lib/module/utils/index.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeDateTimePickerKit.d.ts +17 -0
- package/lib/typescript/src/NativeDateTimePickerKit.d.ts.map +1 -0
- package/lib/typescript/src/components/CalendarPicker.d.ts +51 -0
- package/lib/typescript/src/components/CalendarPicker.d.ts.map +1 -0
- package/lib/typescript/src/components/ClockPicker.d.ts +22 -0
- package/lib/typescript/src/components/ClockPicker.d.ts.map +1 -0
- package/lib/typescript/src/components/DatePicker.d.ts +44 -0
- package/lib/typescript/src/components/DatePicker.d.ts.map +1 -0
- package/lib/typescript/src/components/DateRangePicker.d.ts +56 -0
- package/lib/typescript/src/components/DateRangePicker.d.ts.map +1 -0
- package/lib/typescript/src/components/DateTimePicker.d.ts +7 -0
- package/lib/typescript/src/components/DateTimePicker.d.ts.map +1 -0
- package/lib/typescript/src/components/MonthPicker.d.ts +21 -0
- package/lib/typescript/src/components/MonthPicker.d.ts.map +1 -0
- package/lib/typescript/src/components/TimePicker.d.ts +31 -0
- package/lib/typescript/src/components/TimePicker.d.ts.map +1 -0
- package/lib/typescript/src/components/TimeSlotPicker.d.ts +18 -0
- package/lib/typescript/src/components/TimeSlotPicker.d.ts.map +1 -0
- package/lib/typescript/src/components/WheelPicker.d.ts +17 -0
- package/lib/typescript/src/components/WheelPicker.d.ts.map +1 -0
- package/lib/typescript/src/components/YearPicker.d.ts +14 -0
- package/lib/typescript/src/components/YearPicker.d.ts.map +1 -0
- package/lib/typescript/src/components/common/ModalWrapper.d.ts +11 -0
- package/lib/typescript/src/components/common/ModalWrapper.d.ts.map +1 -0
- package/lib/typescript/src/components/common/PickerHeader.d.ts +13 -0
- package/lib/typescript/src/components/common/PickerHeader.d.ts.map +1 -0
- package/lib/typescript/src/components/common/PresetsBar.d.ts +12 -0
- package/lib/typescript/src/components/common/PresetsBar.d.ts.map +1 -0
- package/lib/typescript/src/errors/DateTimePickerError.d.ts +7 -0
- package/lib/typescript/src/errors/DateTimePickerError.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useDateTimePicker.d.ts +17 -0
- package/lib/typescript/src/hooks/useDateTimePicker.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +23 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/localization/index.d.ts +4 -0
- package/lib/typescript/src/localization/index.d.ts.map +1 -0
- package/lib/typescript/src/localization/locales.d.ts +12 -0
- package/lib/typescript/src/localization/locales.d.ts.map +1 -0
- package/lib/typescript/src/localization/types.d.ts +23 -0
- package/lib/typescript/src/localization/types.d.ts.map +1 -0
- package/lib/typescript/src/localization/useLocale.d.ts +31 -0
- package/lib/typescript/src/localization/useLocale.d.ts.map +1 -0
- package/lib/typescript/src/native/NativeDateTimePicker.d.ts +14 -0
- package/lib/typescript/src/native/NativeDateTimePicker.d.ts.map +1 -0
- package/lib/typescript/src/presets/datePresets.d.ts +8 -0
- package/lib/typescript/src/presets/datePresets.d.ts.map +1 -0
- package/lib/typescript/src/presets/index.d.ts +2 -0
- package/lib/typescript/src/presets/index.d.ts.map +1 -0
- package/lib/typescript/src/slots/index.d.ts +2 -0
- package/lib/typescript/src/slots/index.d.ts.map +1 -0
- package/lib/typescript/src/slots/timeSlots.d.ts +18 -0
- package/lib/typescript/src/slots/timeSlots.d.ts.map +1 -0
- package/lib/typescript/src/theme/defaultTheme.d.ts +9 -0
- package/lib/typescript/src/theme/defaultTheme.d.ts.map +1 -0
- package/lib/typescript/src/theme/index.d.ts +4 -0
- package/lib/typescript/src/theme/index.d.ts.map +1 -0
- package/lib/typescript/src/theme/types.d.ts +76 -0
- package/lib/typescript/src/theme/types.d.ts.map +1 -0
- package/lib/typescript/src/theme/useTheme.d.ts +4 -0
- package/lib/typescript/src/theme/useTheme.d.ts.map +1 -0
- package/lib/typescript/src/types/events.d.ts +8 -0
- package/lib/typescript/src/types/events.d.ts.map +1 -0
- package/lib/typescript/src/types/index.d.ts +5 -0
- package/lib/typescript/src/types/index.d.ts.map +1 -0
- package/lib/typescript/src/types/native.d.ts +26 -0
- package/lib/typescript/src/types/native.d.ts.map +1 -0
- package/lib/typescript/src/types/presentation.d.ts +26 -0
- package/lib/typescript/src/types/presentation.d.ts.map +1 -0
- package/lib/typescript/src/types/props.d.ts +221 -0
- package/lib/typescript/src/types/props.d.ts.map +1 -0
- package/lib/typescript/src/utils/constants.d.ts +12 -0
- package/lib/typescript/src/utils/constants.d.ts.map +1 -0
- package/lib/typescript/src/utils/dateConversion.d.ts +46 -0
- package/lib/typescript/src/utils/dateConversion.d.ts.map +1 -0
- package/lib/typescript/src/utils/dateFormatting.d.ts +8 -0
- package/lib/typescript/src/utils/dateFormatting.d.ts.map +1 -0
- package/lib/typescript/src/utils/dateValidation.d.ts +46 -0
- package/lib/typescript/src/utils/dateValidation.d.ts.map +1 -0
- package/lib/typescript/src/utils/index.d.ts +5 -0
- package/lib/typescript/src/utils/index.d.ts.map +1 -0
- package/package.json +191 -4
- package/src/NativeDateTimePickerKit.ts +19 -0
- package/src/components/CalendarPicker.tsx +623 -0
- package/src/components/ClockPicker.tsx +403 -0
- package/src/components/DatePicker.tsx +276 -0
- package/src/components/DateRangePicker.tsx +251 -0
- package/src/components/DateTimePicker.tsx +1028 -0
- package/src/components/MonthPicker.tsx +216 -0
- package/src/components/TimePicker.tsx +203 -0
- package/src/components/TimeSlotPicker.tsx +163 -0
- package/src/components/WheelPicker.tsx +229 -0
- package/src/components/YearPicker.tsx +228 -0
- package/src/components/common/ModalWrapper.tsx +192 -0
- package/src/components/common/PickerHeader.tsx +121 -0
- package/src/components/common/PresetsBar.tsx +104 -0
- package/src/errors/DateTimePickerError.ts +29 -0
- package/src/hooks/useDateTimePicker.ts +100 -0
- package/src/index.tsx +92 -0
- package/src/localization/index.ts +3 -0
- package/src/localization/locales.ts +499 -0
- package/src/localization/types.ts +21 -0
- package/src/localization/useLocale.ts +197 -0
- package/src/native/NativeDateTimePicker.ts +154 -0
- package/src/presets/datePresets.ts +97 -0
- package/src/presets/index.ts +1 -0
- package/src/slots/index.ts +1 -0
- package/src/slots/timeSlots.ts +89 -0
- package/src/theme/defaultTheme.ts +102 -0
- package/src/theme/index.ts +3 -0
- package/src/theme/types.ts +82 -0
- package/src/theme/useTheme.ts +98 -0
- package/src/types/events.ts +8 -0
- package/src/types/index.ts +4 -0
- package/src/types/native.ts +28 -0
- package/src/types/presentation.ts +28 -0
- package/src/types/props.ts +291 -0
- package/src/utils/constants.ts +15 -0
- package/src/utils/dateConversion.ts +273 -0
- package/src/utils/dateFormatting.ts +166 -0
- package/src/utils/dateValidation.ts +178 -0
- package/src/utils/index.ts +4 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import React, { useCallback, useMemo, useState } from 'react';
|
|
2
|
+
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
|
|
3
|
+
import { useLocale } from '../localization';
|
|
4
|
+
import type { PartialLocaleData } from '../localization/types';
|
|
5
|
+
import { useTheme } from '../theme';
|
|
6
|
+
import type { ThemeConfig, ThemeMode } from '../theme/types';
|
|
7
|
+
import type { MinuteInterval } from '../types';
|
|
8
|
+
|
|
9
|
+
export interface ClockPickerProps {
|
|
10
|
+
value?: Date | { hour: number; minute: number };
|
|
11
|
+
onChange?: (result: { hour: number; minute: number; date: Date }) => void;
|
|
12
|
+
is24Hour?: boolean;
|
|
13
|
+
minuteInterval?: MinuteInterval;
|
|
14
|
+
locale?: string | PartialLocaleData;
|
|
15
|
+
theme?: ThemeMode | ThemeConfig;
|
|
16
|
+
testID?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const ClockPicker: React.FC<ClockPickerProps> = ({
|
|
20
|
+
value,
|
|
21
|
+
onChange,
|
|
22
|
+
is24Hour = false,
|
|
23
|
+
minuteInterval = 1,
|
|
24
|
+
locale: propLocale,
|
|
25
|
+
theme: propTheme,
|
|
26
|
+
testID,
|
|
27
|
+
}) => {
|
|
28
|
+
const theme = useTheme(propTheme);
|
|
29
|
+
const locale = useLocale(propLocale);
|
|
30
|
+
|
|
31
|
+
const initialHour =
|
|
32
|
+
value instanceof Date
|
|
33
|
+
? value.getHours()
|
|
34
|
+
: value?.hour !== undefined
|
|
35
|
+
? value.hour
|
|
36
|
+
: new Date().getHours();
|
|
37
|
+
const initialMinute =
|
|
38
|
+
value instanceof Date
|
|
39
|
+
? value.getMinutes()
|
|
40
|
+
: value?.minute !== undefined
|
|
41
|
+
? value.minute
|
|
42
|
+
: new Date().getMinutes();
|
|
43
|
+
|
|
44
|
+
const [hour, setHour] = useState<number>(initialHour);
|
|
45
|
+
const [minute, setMinute] = useState<number>(initialMinute);
|
|
46
|
+
const [activeSegment, setActiveSegment] = useState<'hour' | 'minute'>('hour');
|
|
47
|
+
|
|
48
|
+
const isPM = hour >= 12;
|
|
49
|
+
const display12Hour = hour % 12 === 0 ? 12 : hour % 12;
|
|
50
|
+
|
|
51
|
+
const emitChange = useCallback(
|
|
52
|
+
(newHour: number, newMinute: number) => {
|
|
53
|
+
const now = new Date();
|
|
54
|
+
const d = new Date(
|
|
55
|
+
now.getFullYear(),
|
|
56
|
+
now.getMonth(),
|
|
57
|
+
now.getDate(),
|
|
58
|
+
newHour,
|
|
59
|
+
newMinute,
|
|
60
|
+
0,
|
|
61
|
+
0
|
|
62
|
+
);
|
|
63
|
+
onChange?.({ hour: newHour, minute: newMinute, date: d });
|
|
64
|
+
},
|
|
65
|
+
[onChange]
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const toggleAmPm = useCallback(
|
|
69
|
+
(pm: boolean) => {
|
|
70
|
+
let nextHour = hour;
|
|
71
|
+
if (pm && hour < 12) {
|
|
72
|
+
nextHour = hour + 12;
|
|
73
|
+
} else if (!pm && hour >= 12) {
|
|
74
|
+
nextHour = hour - 12;
|
|
75
|
+
}
|
|
76
|
+
setHour(nextHour);
|
|
77
|
+
emitChange(nextHour, minute);
|
|
78
|
+
},
|
|
79
|
+
[hour, minute, emitChange]
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const handleSelectHour = useCallback(
|
|
83
|
+
(selectedH: number) => {
|
|
84
|
+
let finalH = selectedH;
|
|
85
|
+
if (!is24Hour) {
|
|
86
|
+
if (isPM && selectedH < 12) {
|
|
87
|
+
finalH = selectedH + 12;
|
|
88
|
+
} else if (!isPM && selectedH === 12) {
|
|
89
|
+
finalH = 0;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
setHour(finalH);
|
|
93
|
+
emitChange(finalH, minute);
|
|
94
|
+
setActiveSegment('minute');
|
|
95
|
+
},
|
|
96
|
+
[is24Hour, isPM, minute, emitChange]
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const handleSelectMinute = useCallback(
|
|
100
|
+
(selectedM: number) => {
|
|
101
|
+
setMinute(selectedM);
|
|
102
|
+
emitChange(hour, selectedM);
|
|
103
|
+
},
|
|
104
|
+
[hour, emitChange]
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// Hours array to display in selection grid
|
|
108
|
+
const hoursList = useMemo(() => {
|
|
109
|
+
if (is24Hour) {
|
|
110
|
+
return Array.from({ length: 24 }, (_, i) => i);
|
|
111
|
+
}
|
|
112
|
+
return [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
|
113
|
+
}, [is24Hour]);
|
|
114
|
+
|
|
115
|
+
// Minutes array to display in selection grid
|
|
116
|
+
const minutesList = useMemo(() => {
|
|
117
|
+
const list: number[] = [];
|
|
118
|
+
for (let m = 0; m < 60; m += minuteInterval) {
|
|
119
|
+
list.push(m);
|
|
120
|
+
}
|
|
121
|
+
return list;
|
|
122
|
+
}, [minuteInterval]);
|
|
123
|
+
|
|
124
|
+
const pad = (n: number) => (n < 10 ? `0${n}` : `${n}`);
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<View
|
|
128
|
+
testID={testID}
|
|
129
|
+
style={[styles.container, { backgroundColor: theme.colors.background }]}
|
|
130
|
+
>
|
|
131
|
+
{/* Time Display Header */}
|
|
132
|
+
<View style={styles.displayRow}>
|
|
133
|
+
<View style={styles.timeBox}>
|
|
134
|
+
{/* Hour Segment */}
|
|
135
|
+
<TouchableOpacity
|
|
136
|
+
onPress={() => setActiveSegment('hour')}
|
|
137
|
+
style={[
|
|
138
|
+
styles.segmentBtn,
|
|
139
|
+
activeSegment === 'hour' && {
|
|
140
|
+
backgroundColor: theme.colors.card,
|
|
141
|
+
borderColor: theme.colors.primary,
|
|
142
|
+
},
|
|
143
|
+
]}
|
|
144
|
+
>
|
|
145
|
+
<Text
|
|
146
|
+
style={[
|
|
147
|
+
styles.segmentText,
|
|
148
|
+
{
|
|
149
|
+
color:
|
|
150
|
+
activeSegment === 'hour'
|
|
151
|
+
? theme.colors.primary
|
|
152
|
+
: theme.colors.text,
|
|
153
|
+
fontSize: 32,
|
|
154
|
+
fontWeight: theme.typography.fontWeightBold,
|
|
155
|
+
},
|
|
156
|
+
]}
|
|
157
|
+
>
|
|
158
|
+
{is24Hour ? pad(hour) : pad(display12Hour)}
|
|
159
|
+
</Text>
|
|
160
|
+
</TouchableOpacity>
|
|
161
|
+
|
|
162
|
+
<Text
|
|
163
|
+
style={[
|
|
164
|
+
styles.colonText,
|
|
165
|
+
{ color: theme.colors.textMuted, fontSize: 32 },
|
|
166
|
+
]}
|
|
167
|
+
>
|
|
168
|
+
:
|
|
169
|
+
</Text>
|
|
170
|
+
|
|
171
|
+
{/* Minute Segment */}
|
|
172
|
+
<TouchableOpacity
|
|
173
|
+
onPress={() => setActiveSegment('minute')}
|
|
174
|
+
style={[
|
|
175
|
+
styles.segmentBtn,
|
|
176
|
+
activeSegment === 'minute' && {
|
|
177
|
+
backgroundColor: theme.colors.card,
|
|
178
|
+
borderColor: theme.colors.primary,
|
|
179
|
+
},
|
|
180
|
+
]}
|
|
181
|
+
>
|
|
182
|
+
<Text
|
|
183
|
+
style={[
|
|
184
|
+
styles.segmentText,
|
|
185
|
+
{
|
|
186
|
+
color:
|
|
187
|
+
activeSegment === 'minute'
|
|
188
|
+
? theme.colors.primary
|
|
189
|
+
: theme.colors.text,
|
|
190
|
+
fontSize: 32,
|
|
191
|
+
fontWeight: theme.typography.fontWeightBold,
|
|
192
|
+
},
|
|
193
|
+
]}
|
|
194
|
+
>
|
|
195
|
+
{pad(minute)}
|
|
196
|
+
</Text>
|
|
197
|
+
</TouchableOpacity>
|
|
198
|
+
</View>
|
|
199
|
+
|
|
200
|
+
{/* AM / PM Toggle (for 12-hour mode) */}
|
|
201
|
+
{!is24Hour && (
|
|
202
|
+
<View
|
|
203
|
+
style={[
|
|
204
|
+
styles.ampmContainer,
|
|
205
|
+
{
|
|
206
|
+
borderColor: theme.colors.border,
|
|
207
|
+
backgroundColor: theme.colors.card,
|
|
208
|
+
},
|
|
209
|
+
]}
|
|
210
|
+
>
|
|
211
|
+
<TouchableOpacity
|
|
212
|
+
onPress={() => toggleAmPm(false)}
|
|
213
|
+
style={[
|
|
214
|
+
styles.ampmBtn,
|
|
215
|
+
!isPM && {
|
|
216
|
+
backgroundColor: theme.colors.primary,
|
|
217
|
+
},
|
|
218
|
+
]}
|
|
219
|
+
>
|
|
220
|
+
<Text
|
|
221
|
+
style={[
|
|
222
|
+
styles.ampmText,
|
|
223
|
+
{
|
|
224
|
+
color: !isPM
|
|
225
|
+
? theme.colors.primaryText
|
|
226
|
+
: theme.colors.textSecondary,
|
|
227
|
+
fontWeight: !isPM ? 'bold' : 'normal',
|
|
228
|
+
},
|
|
229
|
+
]}
|
|
230
|
+
>
|
|
231
|
+
{locale.am}
|
|
232
|
+
</Text>
|
|
233
|
+
</TouchableOpacity>
|
|
234
|
+
|
|
235
|
+
<TouchableOpacity
|
|
236
|
+
onPress={() => toggleAmPm(true)}
|
|
237
|
+
style={[
|
|
238
|
+
styles.ampmBtn,
|
|
239
|
+
isPM && {
|
|
240
|
+
backgroundColor: theme.colors.primary,
|
|
241
|
+
},
|
|
242
|
+
]}
|
|
243
|
+
>
|
|
244
|
+
<Text
|
|
245
|
+
style={[
|
|
246
|
+
styles.ampmText,
|
|
247
|
+
{
|
|
248
|
+
color: isPM
|
|
249
|
+
? theme.colors.primaryText
|
|
250
|
+
: theme.colors.textSecondary,
|
|
251
|
+
fontWeight: isPM ? 'bold' : 'normal',
|
|
252
|
+
},
|
|
253
|
+
]}
|
|
254
|
+
>
|
|
255
|
+
{locale.pm}
|
|
256
|
+
</Text>
|
|
257
|
+
</TouchableOpacity>
|
|
258
|
+
</View>
|
|
259
|
+
)}
|
|
260
|
+
</View>
|
|
261
|
+
|
|
262
|
+
{/* Grid of Choices */}
|
|
263
|
+
<View style={styles.gridContainer}>
|
|
264
|
+
{activeSegment === 'hour' ? (
|
|
265
|
+
<View style={styles.choicesGrid}>
|
|
266
|
+
{hoursList.map((h) => {
|
|
267
|
+
const isSelected = is24Hour ? hour === h : display12Hour === h;
|
|
268
|
+
return (
|
|
269
|
+
<TouchableOpacity
|
|
270
|
+
key={`h-${h}`}
|
|
271
|
+
accessibilityRole="button"
|
|
272
|
+
accessibilityLabel={`${pad(h)} hours`}
|
|
273
|
+
accessibilityState={{ selected: isSelected }}
|
|
274
|
+
onPress={() => handleSelectHour(h)}
|
|
275
|
+
style={[
|
|
276
|
+
styles.choiceCell,
|
|
277
|
+
isSelected && {
|
|
278
|
+
backgroundColor: theme.colors.selected,
|
|
279
|
+
borderRadius: theme.borderRadius.full,
|
|
280
|
+
},
|
|
281
|
+
]}
|
|
282
|
+
>
|
|
283
|
+
<Text
|
|
284
|
+
style={[
|
|
285
|
+
styles.choiceText,
|
|
286
|
+
{
|
|
287
|
+
color: isSelected
|
|
288
|
+
? theme.colors.selectedText
|
|
289
|
+
: theme.colors.text,
|
|
290
|
+
fontWeight: isSelected ? 'bold' : 'normal',
|
|
291
|
+
},
|
|
292
|
+
]}
|
|
293
|
+
>
|
|
294
|
+
{pad(h)}
|
|
295
|
+
</Text>
|
|
296
|
+
</TouchableOpacity>
|
|
297
|
+
);
|
|
298
|
+
})}
|
|
299
|
+
</View>
|
|
300
|
+
) : (
|
|
301
|
+
<View style={styles.choicesGrid}>
|
|
302
|
+
{minutesList.map((m) => {
|
|
303
|
+
const isSelected = minute === m;
|
|
304
|
+
return (
|
|
305
|
+
<TouchableOpacity
|
|
306
|
+
key={`m-${m}`}
|
|
307
|
+
accessibilityRole="button"
|
|
308
|
+
accessibilityLabel={`${pad(m)} minutes`}
|
|
309
|
+
accessibilityState={{ selected: isSelected }}
|
|
310
|
+
onPress={() => handleSelectMinute(m)}
|
|
311
|
+
style={[
|
|
312
|
+
styles.choiceCell,
|
|
313
|
+
isSelected && {
|
|
314
|
+
backgroundColor: theme.colors.selected,
|
|
315
|
+
borderRadius: theme.borderRadius.full,
|
|
316
|
+
},
|
|
317
|
+
]}
|
|
318
|
+
>
|
|
319
|
+
<Text
|
|
320
|
+
style={[
|
|
321
|
+
styles.choiceText,
|
|
322
|
+
{
|
|
323
|
+
color: isSelected
|
|
324
|
+
? theme.colors.selectedText
|
|
325
|
+
: theme.colors.text,
|
|
326
|
+
fontWeight: isSelected ? 'bold' : 'normal',
|
|
327
|
+
},
|
|
328
|
+
]}
|
|
329
|
+
>
|
|
330
|
+
{pad(m)}
|
|
331
|
+
</Text>
|
|
332
|
+
</TouchableOpacity>
|
|
333
|
+
);
|
|
334
|
+
})}
|
|
335
|
+
</View>
|
|
336
|
+
)}
|
|
337
|
+
</View>
|
|
338
|
+
</View>
|
|
339
|
+
);
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
const styles = StyleSheet.create({
|
|
343
|
+
container: {
|
|
344
|
+
padding: 16,
|
|
345
|
+
alignItems: 'center',
|
|
346
|
+
},
|
|
347
|
+
displayRow: {
|
|
348
|
+
flexDirection: 'row',
|
|
349
|
+
alignItems: 'center',
|
|
350
|
+
justifyContent: 'center',
|
|
351
|
+
marginBottom: 20,
|
|
352
|
+
gap: 16,
|
|
353
|
+
},
|
|
354
|
+
timeBox: {
|
|
355
|
+
flexDirection: 'row',
|
|
356
|
+
alignItems: 'center',
|
|
357
|
+
},
|
|
358
|
+
segmentBtn: {
|
|
359
|
+
paddingHorizontal: 12,
|
|
360
|
+
paddingVertical: 4,
|
|
361
|
+
borderRadius: 8,
|
|
362
|
+
borderWidth: 1,
|
|
363
|
+
borderColor: 'transparent',
|
|
364
|
+
},
|
|
365
|
+
segmentText: {},
|
|
366
|
+
colonText: {
|
|
367
|
+
marginHorizontal: 4,
|
|
368
|
+
},
|
|
369
|
+
ampmContainer: {
|
|
370
|
+
flexDirection: 'column',
|
|
371
|
+
borderRadius: 8,
|
|
372
|
+
borderWidth: 1,
|
|
373
|
+
overflow: 'hidden',
|
|
374
|
+
},
|
|
375
|
+
ampmBtn: {
|
|
376
|
+
paddingHorizontal: 10,
|
|
377
|
+
paddingVertical: 6,
|
|
378
|
+
alignItems: 'center',
|
|
379
|
+
},
|
|
380
|
+
ampmText: {
|
|
381
|
+
fontSize: 12,
|
|
382
|
+
},
|
|
383
|
+
gridContainer: {
|
|
384
|
+
width: '100%',
|
|
385
|
+
minHeight: 180,
|
|
386
|
+
justifyContent: 'center',
|
|
387
|
+
},
|
|
388
|
+
choicesGrid: {
|
|
389
|
+
flexDirection: 'row',
|
|
390
|
+
flexWrap: 'wrap',
|
|
391
|
+
justifyContent: 'center',
|
|
392
|
+
gap: 8,
|
|
393
|
+
},
|
|
394
|
+
choiceCell: {
|
|
395
|
+
width: 44,
|
|
396
|
+
height: 44,
|
|
397
|
+
justifyContent: 'center',
|
|
398
|
+
alignItems: 'center',
|
|
399
|
+
},
|
|
400
|
+
choiceText: {
|
|
401
|
+
fontSize: 16,
|
|
402
|
+
},
|
|
403
|
+
});
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { forwardRef, useCallback, useImperativeHandle, useState } from 'react';
|
|
2
|
+
import { StyleSheet, View } from 'react-native';
|
|
3
|
+
import { useLocale } from '../localization';
|
|
4
|
+
import type { PartialLocaleData } from '../localization/types';
|
|
5
|
+
import { NativeDateTimePicker } from '../native/NativeDateTimePicker';
|
|
6
|
+
import { type DatePreset, getStandardSinglePresets } from '../presets';
|
|
7
|
+
import { useTheme } from '../theme';
|
|
8
|
+
import type { ThemeConfig, ThemeMode } from '../theme/types';
|
|
9
|
+
import type { BaseModalProps, PresentationMode } from '../types/presentation';
|
|
10
|
+
import {
|
|
11
|
+
clampDate,
|
|
12
|
+
dateToTimestamp,
|
|
13
|
+
parseDate,
|
|
14
|
+
timestampToDate,
|
|
15
|
+
} from '../utils/dateConversion';
|
|
16
|
+
import { CalendarPicker, type MarkedDatesMap } from './CalendarPicker';
|
|
17
|
+
import { ModalWrapper } from './common/ModalWrapper';
|
|
18
|
+
import { PickerHeader } from './common/PickerHeader';
|
|
19
|
+
import { PresetsBar } from './common/PresetsBar';
|
|
20
|
+
|
|
21
|
+
export interface DatePickerRef {
|
|
22
|
+
open: () => Promise<Date | null> | void;
|
|
23
|
+
close: () => Promise<boolean> | void;
|
|
24
|
+
getValue: () => Date | null;
|
|
25
|
+
setValue: (date: Date | string | number) => void;
|
|
26
|
+
reset: () => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface DatePickerProps extends Omit<BaseModalProps, 'onConfirm'> {
|
|
30
|
+
presentation?: PresentationMode;
|
|
31
|
+
value?: Date | string | number | null;
|
|
32
|
+
defaultValue?: Date | string | number | null;
|
|
33
|
+
onChange?: (date: Date) => void;
|
|
34
|
+
onConfirmDate?: (date: Date) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Alias for `onConfirmDate`.
|
|
37
|
+
*/
|
|
38
|
+
onConfirm?: (date: Date) => void;
|
|
39
|
+
minimumDate?: Date | string | number;
|
|
40
|
+
/**
|
|
41
|
+
* Alias for `minimumDate`.
|
|
42
|
+
*/
|
|
43
|
+
minDate?: Date | string | number;
|
|
44
|
+
maximumDate?: Date | string | number;
|
|
45
|
+
/**
|
|
46
|
+
* Alias for `maximumDate`.
|
|
47
|
+
*/
|
|
48
|
+
maxDate?: Date | string | number;
|
|
49
|
+
disabledDates?: (Date | string)[];
|
|
50
|
+
disabledWeekdays?: number[];
|
|
51
|
+
markedDates?: MarkedDatesMap;
|
|
52
|
+
firstDayOfWeek?: number;
|
|
53
|
+
showPresets?: boolean;
|
|
54
|
+
customPresets?: DatePreset[];
|
|
55
|
+
locale?: string | PartialLocaleData;
|
|
56
|
+
theme?: ThemeMode | ThemeConfig;
|
|
57
|
+
testID?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const DatePicker = forwardRef<DatePickerRef, DatePickerProps>(
|
|
61
|
+
(
|
|
62
|
+
{
|
|
63
|
+
presentation = 'bottom-sheet',
|
|
64
|
+
visible: propVisible,
|
|
65
|
+
onClose,
|
|
66
|
+
title,
|
|
67
|
+
confirmText,
|
|
68
|
+
cancelText,
|
|
69
|
+
onConfirmDate,
|
|
70
|
+
onCancel,
|
|
71
|
+
value: propValue,
|
|
72
|
+
defaultValue,
|
|
73
|
+
onChange,
|
|
74
|
+
minimumDate,
|
|
75
|
+
maximumDate,
|
|
76
|
+
disabledDates,
|
|
77
|
+
disabledWeekdays,
|
|
78
|
+
markedDates,
|
|
79
|
+
firstDayOfWeek,
|
|
80
|
+
showPresets = false,
|
|
81
|
+
customPresets,
|
|
82
|
+
locale: propLocale,
|
|
83
|
+
theme: propTheme,
|
|
84
|
+
backdropOpacity,
|
|
85
|
+
animationType,
|
|
86
|
+
showHandle,
|
|
87
|
+
closeOnBackdropPress,
|
|
88
|
+
testID,
|
|
89
|
+
...restProps
|
|
90
|
+
},
|
|
91
|
+
ref
|
|
92
|
+
) => {
|
|
93
|
+
const effectiveMin = minimumDate ?? restProps.minDate;
|
|
94
|
+
const effectiveMax = maximumDate ?? restProps.maxDate;
|
|
95
|
+
const effectiveOnConfirm = onConfirmDate ?? restProps.onConfirm;
|
|
96
|
+
|
|
97
|
+
const theme = useTheme(propTheme);
|
|
98
|
+
const locale = useLocale(propLocale);
|
|
99
|
+
|
|
100
|
+
const initialDate =
|
|
101
|
+
parseDate(propValue) || parseDate(defaultValue) || new Date();
|
|
102
|
+
const clampedInitial =
|
|
103
|
+
clampDate(initialDate, effectiveMin, effectiveMax) ?? initialDate;
|
|
104
|
+
const [selectedDate, setSelectedDate] = useState<Date>(clampedInitial);
|
|
105
|
+
const [isVisible, setIsVisible] = useState(false);
|
|
106
|
+
const [activePresetId, setActivePresetId] = useState<string | undefined>();
|
|
107
|
+
|
|
108
|
+
const modalVisible =
|
|
109
|
+
restProps.isVisible !== undefined
|
|
110
|
+
? restProps.isVisible
|
|
111
|
+
: propVisible !== undefined
|
|
112
|
+
? propVisible
|
|
113
|
+
: isVisible;
|
|
114
|
+
|
|
115
|
+
// Imperative ref implementation
|
|
116
|
+
useImperativeHandle(ref, () => ({
|
|
117
|
+
open: async () => {
|
|
118
|
+
if (presentation === 'native') {
|
|
119
|
+
const res = await NativeDateTimePicker.openDatePicker({
|
|
120
|
+
value: selectedDate.getTime(),
|
|
121
|
+
minimumDate: effectiveMin
|
|
122
|
+
? (dateToTimestamp(effectiveMin) ?? undefined)
|
|
123
|
+
: undefined,
|
|
124
|
+
maximumDate: effectiveMax
|
|
125
|
+
? (dateToTimestamp(effectiveMax) ?? undefined)
|
|
126
|
+
: undefined,
|
|
127
|
+
title: title || locale.selectDateTitle,
|
|
128
|
+
confirmText: confirmText || locale.confirmText,
|
|
129
|
+
cancelText: cancelText || locale.cancelText,
|
|
130
|
+
});
|
|
131
|
+
if (res.action === 'confirmed') {
|
|
132
|
+
const confirmedDate = timestampToDate(res.timestamp);
|
|
133
|
+
const clamped =
|
|
134
|
+
clampDate(confirmedDate, effectiveMin, effectiveMax) ??
|
|
135
|
+
confirmedDate;
|
|
136
|
+
setSelectedDate(clamped);
|
|
137
|
+
onChange?.(clamped);
|
|
138
|
+
effectiveOnConfirm?.(clamped);
|
|
139
|
+
return clamped;
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
} else {
|
|
143
|
+
setIsVisible(true);
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
close: async () => {
|
|
148
|
+
if (presentation === 'native') {
|
|
149
|
+
return await NativeDateTimePicker.closePicker();
|
|
150
|
+
} else {
|
|
151
|
+
setIsVisible(false);
|
|
152
|
+
onClose?.();
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
getValue: () => selectedDate,
|
|
157
|
+
setValue: (newDate: Date | string | number) => {
|
|
158
|
+
const parsed = parseDate(newDate);
|
|
159
|
+
if (parsed) {
|
|
160
|
+
setSelectedDate(parsed);
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
reset: () => {
|
|
164
|
+
setSelectedDate(initialDate);
|
|
165
|
+
setActivePresetId(undefined);
|
|
166
|
+
},
|
|
167
|
+
}));
|
|
168
|
+
|
|
169
|
+
const presets = showPresets
|
|
170
|
+
? customPresets || getStandardSinglePresets()
|
|
171
|
+
: [];
|
|
172
|
+
|
|
173
|
+
const handleSelectPreset = useCallback(
|
|
174
|
+
(preset: DatePreset) => {
|
|
175
|
+
setActivePresetId(preset.id);
|
|
176
|
+
const val = preset.getValue();
|
|
177
|
+
if (val instanceof Date) {
|
|
178
|
+
setSelectedDate(val);
|
|
179
|
+
onChange?.(val);
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
[onChange]
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const handleDayChange = useCallback(
|
|
186
|
+
(date: Date) => {
|
|
187
|
+
setActivePresetId(undefined);
|
|
188
|
+
setSelectedDate(date);
|
|
189
|
+
onChange?.(date);
|
|
190
|
+
},
|
|
191
|
+
[onChange]
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
const handleConfirm = useCallback(() => {
|
|
195
|
+
const clamped =
|
|
196
|
+
clampDate(selectedDate, effectiveMin, effectiveMax) ?? selectedDate;
|
|
197
|
+
effectiveOnConfirm?.(clamped);
|
|
198
|
+
setIsVisible(false);
|
|
199
|
+
onClose?.();
|
|
200
|
+
}, [selectedDate, effectiveMin, effectiveMax, effectiveOnConfirm, onClose]);
|
|
201
|
+
|
|
202
|
+
const handleCancel = useCallback(() => {
|
|
203
|
+
onCancel?.();
|
|
204
|
+
setIsVisible(false);
|
|
205
|
+
onClose?.();
|
|
206
|
+
}, [onCancel, onClose]);
|
|
207
|
+
|
|
208
|
+
// If native presentation, render nothing in React DOM (Native dialog handles UI)
|
|
209
|
+
if (presentation === 'native') {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const content = (
|
|
214
|
+
<View style={styles.innerContent}>
|
|
215
|
+
{presentation !== 'inline' && (
|
|
216
|
+
<PickerHeader
|
|
217
|
+
title={title || locale.selectDateTitle}
|
|
218
|
+
cancelText={cancelText || locale.cancelText}
|
|
219
|
+
confirmText={confirmText || locale.confirmText}
|
|
220
|
+
onCancel={handleCancel}
|
|
221
|
+
onConfirm={handleConfirm}
|
|
222
|
+
theme={theme}
|
|
223
|
+
isRTL={locale.isRTL}
|
|
224
|
+
/>
|
|
225
|
+
)}
|
|
226
|
+
|
|
227
|
+
{showPresets && presets.length > 0 && (
|
|
228
|
+
<PresetsBar
|
|
229
|
+
presets={presets}
|
|
230
|
+
activePresetId={activePresetId}
|
|
231
|
+
onSelectPreset={handleSelectPreset}
|
|
232
|
+
theme={theme}
|
|
233
|
+
isRTL={locale.isRTL}
|
|
234
|
+
/>
|
|
235
|
+
)}
|
|
236
|
+
|
|
237
|
+
<CalendarPicker
|
|
238
|
+
value={selectedDate}
|
|
239
|
+
onChange={handleDayChange}
|
|
240
|
+
minimumDate={effectiveMin}
|
|
241
|
+
maximumDate={effectiveMax}
|
|
242
|
+
disabledDates={disabledDates}
|
|
243
|
+
disabledWeekdays={disabledWeekdays}
|
|
244
|
+
markedDates={markedDates}
|
|
245
|
+
firstDayOfWeek={firstDayOfWeek}
|
|
246
|
+
locale={locale}
|
|
247
|
+
theme={theme}
|
|
248
|
+
/>
|
|
249
|
+
</View>
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
<ModalWrapper
|
|
254
|
+
testID={testID}
|
|
255
|
+
presentation={presentation}
|
|
256
|
+
visible={modalVisible}
|
|
257
|
+
onClose={handleCancel}
|
|
258
|
+
theme={theme}
|
|
259
|
+
backdropOpacity={backdropOpacity}
|
|
260
|
+
animationType={animationType}
|
|
261
|
+
showHandle={showHandle}
|
|
262
|
+
closeOnBackdropPress={closeOnBackdropPress}
|
|
263
|
+
>
|
|
264
|
+
{content}
|
|
265
|
+
</ModalWrapper>
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
DatePicker.displayName = 'DatePicker';
|
|
271
|
+
|
|
272
|
+
const styles = StyleSheet.create({
|
|
273
|
+
innerContent: {
|
|
274
|
+
width: '100%',
|
|
275
|
+
},
|
|
276
|
+
});
|