@izonnet-web-solution/react-native-date-time-picker-kit 0.0.0-stage → 1.0.0
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 +532 -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 +414 -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 +174 -0
- package/lib/module/components/DatePicker.js.map +1 -0
- package/lib/module/components/DateRangePicker.js +154 -0
- package/lib/module/components/DateRangePicker.js.map +1 -0
- package/lib/module/components/DateTimePicker.js +598 -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 +140 -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 +138 -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 +130 -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 +126 -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 +59 -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 +115 -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 +35 -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 +32 -0
- package/lib/typescript/src/components/DatePicker.d.ts.map +1 -0
- package/lib/typescript/src/components/DateRangePicker.d.ts +41 -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 +25 -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 +10 -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 +65 -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 +19 -0
- package/lib/typescript/src/types/presentation.d.ts.map +1 -0
- package/lib/typescript/src/types/props.d.ts +68 -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 +27 -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 +183 -4
- package/src/NativeDateTimePickerKit.ts +19 -0
- package/src/components/CalendarPicker.tsx +593 -0
- package/src/components/ClockPicker.tsx +403 -0
- package/src/components/DatePicker.tsx +246 -0
- package/src/components/DateRangePicker.tsx +226 -0
- package/src/components/DateTimePicker.tsx +862 -0
- package/src/components/MonthPicker.tsx +216 -0
- package/src/components/TimePicker.tsx +190 -0
- package/src/components/TimeSlotPicker.tsx +163 -0
- package/src/components/WheelPicker.tsx +199 -0
- package/src/components/YearPicker.tsx +228 -0
- package/src/components/common/ModalWrapper.tsx +184 -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 +137 -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 +70 -0
- package/src/theme/useTheme.ts +75 -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 +21 -0
- package/src/types/props.ts +104 -0
- package/src/utils/constants.ts +15 -0
- package/src/utils/dateConversion.ts +127 -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,862 @@
|
|
|
1
|
+
import {
|
|
2
|
+
forwardRef,
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useImperativeHandle,
|
|
6
|
+
useMemo,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from 'react';
|
|
10
|
+
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
|
|
11
|
+
import { DateTimePickerError } from '../errors/DateTimePickerError';
|
|
12
|
+
import { useLocale } from '../localization';
|
|
13
|
+
import { NativeDateTimePicker } from '../native/NativeDateTimePicker';
|
|
14
|
+
import { type DatePreset, getStandardSinglePresets } from '../presets';
|
|
15
|
+
import { useTheme } from '../theme';
|
|
16
|
+
import type { ResolvedTheme, ThemeConfig } from '../theme/types';
|
|
17
|
+
import type {
|
|
18
|
+
DateTimePickerProps,
|
|
19
|
+
DateTimePickerRef,
|
|
20
|
+
NativePickerAction,
|
|
21
|
+
NativePickerOptions,
|
|
22
|
+
NativePickerTheme,
|
|
23
|
+
OpenPickerOptions,
|
|
24
|
+
} from '../types';
|
|
25
|
+
import {
|
|
26
|
+
dateToTimestamp,
|
|
27
|
+
parseDate,
|
|
28
|
+
timestampToDate,
|
|
29
|
+
} from '../utils/dateConversion';
|
|
30
|
+
import {
|
|
31
|
+
isDateDisabled,
|
|
32
|
+
validateDateRange,
|
|
33
|
+
validateMinuteInterval,
|
|
34
|
+
} from '../utils/dateValidation';
|
|
35
|
+
import { CalendarPicker } from './CalendarPicker';
|
|
36
|
+
import { ClockPicker } from './ClockPicker';
|
|
37
|
+
import { ModalWrapper } from './common/ModalWrapper';
|
|
38
|
+
import { PickerHeader } from './common/PickerHeader';
|
|
39
|
+
import { PresetsBar } from './common/PresetsBar';
|
|
40
|
+
import { WheelPicker } from './WheelPicker';
|
|
41
|
+
|
|
42
|
+
export const DateTimePicker = forwardRef<
|
|
43
|
+
DateTimePickerRef,
|
|
44
|
+
DateTimePickerProps
|
|
45
|
+
>((props, ref) => {
|
|
46
|
+
const {
|
|
47
|
+
mode = 'date',
|
|
48
|
+
value: propValue,
|
|
49
|
+
defaultValue,
|
|
50
|
+
display = 'default',
|
|
51
|
+
disabled = false,
|
|
52
|
+
presentation = 'native',
|
|
53
|
+
minimumDate,
|
|
54
|
+
maximumDate,
|
|
55
|
+
minuteInterval,
|
|
56
|
+
disabledDates,
|
|
57
|
+
disabledWeekdays,
|
|
58
|
+
markedDates,
|
|
59
|
+
firstDayOfWeek,
|
|
60
|
+
locale: propLocale,
|
|
61
|
+
timeZoneOffsetInMinutes,
|
|
62
|
+
is24Hour,
|
|
63
|
+
theme: propTheme = 'auto',
|
|
64
|
+
title,
|
|
65
|
+
confirmText,
|
|
66
|
+
cancelText,
|
|
67
|
+
accentColor,
|
|
68
|
+
textColor,
|
|
69
|
+
backgroundColor,
|
|
70
|
+
showPresets = false,
|
|
71
|
+
customPresets,
|
|
72
|
+
onChange,
|
|
73
|
+
onConfirm,
|
|
74
|
+
onCancel,
|
|
75
|
+
onDismiss,
|
|
76
|
+
onOpen,
|
|
77
|
+
onError,
|
|
78
|
+
open: propOpen,
|
|
79
|
+
backdropOpacity,
|
|
80
|
+
animationType,
|
|
81
|
+
showHandle,
|
|
82
|
+
closeOnBackdropPress,
|
|
83
|
+
testID,
|
|
84
|
+
} = props;
|
|
85
|
+
|
|
86
|
+
// Track internal date state
|
|
87
|
+
const initialDate = useMemo(
|
|
88
|
+
() => parseDate(propValue) || parseDate(defaultValue) || new Date(),
|
|
89
|
+
[propValue, defaultValue]
|
|
90
|
+
);
|
|
91
|
+
const [currentDate, setCurrentDate] = useState<Date>(initialDate);
|
|
92
|
+
const isOpenRef = useRef<boolean>(false);
|
|
93
|
+
const dateRef = useRef<Date>(currentDate);
|
|
94
|
+
|
|
95
|
+
// JS modal presentation state
|
|
96
|
+
const [isJsModalVisible, setIsJsModalVisible] = useState(false);
|
|
97
|
+
const [dateTimeTab, setDateTimeTab] = useState<'date' | 'time'>('date');
|
|
98
|
+
const [activePresetId, setActivePresetId] = useState<string | undefined>();
|
|
99
|
+
|
|
100
|
+
// Theme & Locale
|
|
101
|
+
const resolvedTheme: ResolvedTheme = useTheme(
|
|
102
|
+
typeof propTheme === 'object'
|
|
103
|
+
? propTheme
|
|
104
|
+
: ({ mode: propTheme } as ThemeConfig)
|
|
105
|
+
);
|
|
106
|
+
const localeData = useLocale(propLocale);
|
|
107
|
+
|
|
108
|
+
// Keep dateRef synced
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
dateRef.current = currentDate;
|
|
111
|
+
}, [currentDate]);
|
|
112
|
+
|
|
113
|
+
// Update if propValue changes
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
if (propValue !== undefined && propValue !== null) {
|
|
116
|
+
const parsed = parseDate(propValue);
|
|
117
|
+
if (parsed) {
|
|
118
|
+
setCurrentDate(parsed);
|
|
119
|
+
dateRef.current = parsed;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}, [propValue]);
|
|
123
|
+
|
|
124
|
+
const buildNativeOptions = useCallback(
|
|
125
|
+
(targetDate: Date): NativePickerOptions => {
|
|
126
|
+
const minTimestamp = minimumDate
|
|
127
|
+
? dateToTimestamp(minimumDate)
|
|
128
|
+
: undefined;
|
|
129
|
+
const maxTimestamp = maximumDate
|
|
130
|
+
? dateToTimestamp(maximumDate)
|
|
131
|
+
: undefined;
|
|
132
|
+
|
|
133
|
+
const nativeTheme: NativePickerTheme =
|
|
134
|
+
typeof propTheme === 'string' &&
|
|
135
|
+
(propTheme === 'light' || propTheme === 'dark' || propTheme === 'auto')
|
|
136
|
+
? propTheme
|
|
137
|
+
: 'auto';
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
value: targetDate.getTime(),
|
|
141
|
+
minimumDate: minTimestamp ?? undefined,
|
|
142
|
+
maximumDate: maxTimestamp ?? undefined,
|
|
143
|
+
display,
|
|
144
|
+
locale: typeof propLocale === 'string' ? propLocale : propLocale?.code,
|
|
145
|
+
timeZoneOffsetInMinutes,
|
|
146
|
+
is24Hour,
|
|
147
|
+
minuteInterval,
|
|
148
|
+
title,
|
|
149
|
+
confirmText,
|
|
150
|
+
cancelText,
|
|
151
|
+
theme: nativeTheme,
|
|
152
|
+
accentColor,
|
|
153
|
+
textColor,
|
|
154
|
+
backgroundColor,
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
[
|
|
158
|
+
minimumDate,
|
|
159
|
+
maximumDate,
|
|
160
|
+
display,
|
|
161
|
+
propLocale,
|
|
162
|
+
timeZoneOffsetInMinutes,
|
|
163
|
+
is24Hour,
|
|
164
|
+
minuteInterval,
|
|
165
|
+
title,
|
|
166
|
+
confirmText,
|
|
167
|
+
cancelText,
|
|
168
|
+
propTheme,
|
|
169
|
+
accentColor,
|
|
170
|
+
textColor,
|
|
171
|
+
backgroundColor,
|
|
172
|
+
]
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
const openPicker = useCallback(async (): Promise<Date | null> => {
|
|
176
|
+
if (disabled) {
|
|
177
|
+
const err = new DateTimePickerError(
|
|
178
|
+
'UNKNOWN_ERROR',
|
|
179
|
+
'Cannot open picker: component is disabled.'
|
|
180
|
+
);
|
|
181
|
+
onError?.(err);
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (minuteInterval !== undefined) {
|
|
186
|
+
if (!validateMinuteInterval(minuteInterval)) {
|
|
187
|
+
const err = new DateTimePickerError(
|
|
188
|
+
'INVALID_MINUTE_INTERVAL',
|
|
189
|
+
`minuteInterval must be a positive integer that evenly divides 60. Received: ${minuteInterval}`
|
|
190
|
+
);
|
|
191
|
+
onError?.(err);
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (minimumDate && maximumDate) {
|
|
197
|
+
if (!validateDateRange(minimumDate, maximumDate)) {
|
|
198
|
+
const err = new DateTimePickerError(
|
|
199
|
+
'DATE_OUT_OF_RANGE',
|
|
200
|
+
'minimumDate cannot be after maximumDate.'
|
|
201
|
+
);
|
|
202
|
+
onError?.(err);
|
|
203
|
+
throw err;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Presentation: JS Modal / Bottom Sheet / Fullscreen
|
|
208
|
+
if (presentation !== 'native') {
|
|
209
|
+
setIsJsModalVisible(true);
|
|
210
|
+
setDateTimeTab('date');
|
|
211
|
+
isOpenRef.current = true;
|
|
212
|
+
onOpen?.();
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Presentation: Native (TurboModule)
|
|
217
|
+
if (isOpenRef.current) {
|
|
218
|
+
const err = new DateTimePickerError(
|
|
219
|
+
'PICKER_ALREADY_OPEN',
|
|
220
|
+
'A native picker dialog is already active.'
|
|
221
|
+
);
|
|
222
|
+
onError?.(err);
|
|
223
|
+
throw err;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
isOpenRef.current = true;
|
|
227
|
+
onOpen?.();
|
|
228
|
+
|
|
229
|
+
const targetDate = dateRef.current;
|
|
230
|
+
const nativeOptions = buildNativeOptions(targetDate);
|
|
231
|
+
|
|
232
|
+
try {
|
|
233
|
+
let result: { action: NativePickerAction; timestamp: number };
|
|
234
|
+
|
|
235
|
+
switch (mode) {
|
|
236
|
+
case 'time':
|
|
237
|
+
result = await NativeDateTimePicker.openTimePicker(nativeOptions);
|
|
238
|
+
break;
|
|
239
|
+
case 'datetime':
|
|
240
|
+
result = await NativeDateTimePicker.openDateTimePicker(nativeOptions);
|
|
241
|
+
break;
|
|
242
|
+
case 'countdown':
|
|
243
|
+
result =
|
|
244
|
+
await NativeDateTimePicker.openCountdownPicker(nativeOptions);
|
|
245
|
+
break;
|
|
246
|
+
case 'date':
|
|
247
|
+
default:
|
|
248
|
+
result = await NativeDateTimePicker.openDatePicker(nativeOptions);
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const action = result.action;
|
|
253
|
+
if (action === 'confirmed') {
|
|
254
|
+
const selectedDate = timestampToDate(result.timestamp);
|
|
255
|
+
|
|
256
|
+
if (isDateDisabled(selectedDate, disabledDates, disabledWeekdays)) {
|
|
257
|
+
const err = new DateTimePickerError(
|
|
258
|
+
'DATE_OUT_OF_RANGE',
|
|
259
|
+
'Selected date is in disabledDates or disabledWeekdays.'
|
|
260
|
+
);
|
|
261
|
+
onError?.(err);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
setCurrentDate(selectedDate);
|
|
265
|
+
dateRef.current = selectedDate;
|
|
266
|
+
onChange?.(selectedDate);
|
|
267
|
+
onConfirm?.(selectedDate);
|
|
268
|
+
return selectedDate;
|
|
269
|
+
} else if (action === 'cancelled') {
|
|
270
|
+
onCancel?.();
|
|
271
|
+
return null;
|
|
272
|
+
} else {
|
|
273
|
+
onDismiss?.();
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
} catch (error) {
|
|
277
|
+
const normalized =
|
|
278
|
+
error instanceof DateTimePickerError
|
|
279
|
+
? error
|
|
280
|
+
: new DateTimePickerError(
|
|
281
|
+
'UNKNOWN_ERROR',
|
|
282
|
+
error instanceof Error ? error.message : 'Unknown error',
|
|
283
|
+
error
|
|
284
|
+
);
|
|
285
|
+
onError?.(normalized);
|
|
286
|
+
throw normalized;
|
|
287
|
+
} finally {
|
|
288
|
+
isOpenRef.current = false;
|
|
289
|
+
}
|
|
290
|
+
}, [
|
|
291
|
+
disabled,
|
|
292
|
+
minuteInterval,
|
|
293
|
+
minimumDate,
|
|
294
|
+
maximumDate,
|
|
295
|
+
presentation,
|
|
296
|
+
onOpen,
|
|
297
|
+
buildNativeOptions,
|
|
298
|
+
mode,
|
|
299
|
+
disabledDates,
|
|
300
|
+
disabledWeekdays,
|
|
301
|
+
onChange,
|
|
302
|
+
onConfirm,
|
|
303
|
+
onCancel,
|
|
304
|
+
onDismiss,
|
|
305
|
+
onError,
|
|
306
|
+
]);
|
|
307
|
+
|
|
308
|
+
const closePicker = useCallback(async (): Promise<boolean> => {
|
|
309
|
+
if (presentation !== 'native') {
|
|
310
|
+
setIsJsModalVisible(false);
|
|
311
|
+
isOpenRef.current = false;
|
|
312
|
+
onDismiss?.();
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (!isOpenRef.current) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
const closed = await NativeDateTimePicker.closePicker();
|
|
321
|
+
isOpenRef.current = false;
|
|
322
|
+
onDismiss?.();
|
|
323
|
+
return closed;
|
|
324
|
+
} catch (error) {
|
|
325
|
+
const normalized =
|
|
326
|
+
error instanceof DateTimePickerError
|
|
327
|
+
? error
|
|
328
|
+
: new DateTimePickerError(
|
|
329
|
+
'UNKNOWN_ERROR',
|
|
330
|
+
error instanceof Error ? error.message : 'Failed to close picker',
|
|
331
|
+
error
|
|
332
|
+
);
|
|
333
|
+
onError?.(normalized);
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
}, [presentation, onDismiss, onError]);
|
|
337
|
+
|
|
338
|
+
const confirmProgrammatic = useCallback(async (): Promise<Date | null> => {
|
|
339
|
+
if (presentation !== 'native') {
|
|
340
|
+
const selectedDate = dateRef.current;
|
|
341
|
+
setIsJsModalVisible(false);
|
|
342
|
+
isOpenRef.current = false;
|
|
343
|
+
onChange?.(selectedDate);
|
|
344
|
+
onConfirm?.(selectedDate);
|
|
345
|
+
return selectedDate;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
try {
|
|
349
|
+
const result = await NativeDateTimePicker.confirmPicker();
|
|
350
|
+
const selectedDate = timestampToDate(result.timestamp);
|
|
351
|
+
setCurrentDate(selectedDate);
|
|
352
|
+
dateRef.current = selectedDate;
|
|
353
|
+
isOpenRef.current = false;
|
|
354
|
+
onChange?.(selectedDate);
|
|
355
|
+
onConfirm?.(selectedDate);
|
|
356
|
+
return selectedDate;
|
|
357
|
+
} catch (error) {
|
|
358
|
+
const normalized =
|
|
359
|
+
error instanceof DateTimePickerError
|
|
360
|
+
? error
|
|
361
|
+
: new DateTimePickerError(
|
|
362
|
+
'PICKER_NOT_OPEN',
|
|
363
|
+
error instanceof Error
|
|
364
|
+
? error.message
|
|
365
|
+
: 'Picker is not currently open to confirm',
|
|
366
|
+
error
|
|
367
|
+
);
|
|
368
|
+
onError?.(normalized);
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
}, [presentation, onChange, onConfirm, onError]);
|
|
372
|
+
|
|
373
|
+
const cancelProgrammatic = useCallback(async (): Promise<boolean> => {
|
|
374
|
+
if (presentation !== 'native') {
|
|
375
|
+
setIsJsModalVisible(false);
|
|
376
|
+
isOpenRef.current = false;
|
|
377
|
+
onCancel?.();
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
try {
|
|
382
|
+
const cancelled = await NativeDateTimePicker.cancelPicker();
|
|
383
|
+
isOpenRef.current = false;
|
|
384
|
+
onCancel?.();
|
|
385
|
+
return cancelled;
|
|
386
|
+
} catch (error) {
|
|
387
|
+
const normalized =
|
|
388
|
+
error instanceof DateTimePickerError
|
|
389
|
+
? error
|
|
390
|
+
: new DateTimePickerError(
|
|
391
|
+
'PICKER_NOT_OPEN',
|
|
392
|
+
error instanceof Error
|
|
393
|
+
? error.message
|
|
394
|
+
: 'Picker is not currently open to cancel',
|
|
395
|
+
error
|
|
396
|
+
);
|
|
397
|
+
onError?.(normalized);
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
}, [presentation, onCancel, onError]);
|
|
401
|
+
|
|
402
|
+
const reset = useCallback(() => {
|
|
403
|
+
setCurrentDate(initialDate);
|
|
404
|
+
dateRef.current = initialDate;
|
|
405
|
+
setActivePresetId(undefined);
|
|
406
|
+
}, [initialDate]);
|
|
407
|
+
|
|
408
|
+
const getValue = useCallback((): Date | null => {
|
|
409
|
+
return dateRef.current;
|
|
410
|
+
}, []);
|
|
411
|
+
|
|
412
|
+
const setValue = useCallback((date: Date | number | string) => {
|
|
413
|
+
const parsed = parseDate(date);
|
|
414
|
+
if (parsed) {
|
|
415
|
+
setCurrentDate(parsed);
|
|
416
|
+
dateRef.current = parsed;
|
|
417
|
+
}
|
|
418
|
+
}, []);
|
|
419
|
+
|
|
420
|
+
// Imperative handle
|
|
421
|
+
useImperativeHandle(
|
|
422
|
+
ref,
|
|
423
|
+
() => ({
|
|
424
|
+
open: openPicker,
|
|
425
|
+
close: closePicker,
|
|
426
|
+
confirm: confirmProgrammatic,
|
|
427
|
+
cancel: cancelProgrammatic,
|
|
428
|
+
reset,
|
|
429
|
+
getValue,
|
|
430
|
+
setValue,
|
|
431
|
+
}),
|
|
432
|
+
[
|
|
433
|
+
openPicker,
|
|
434
|
+
closePicker,
|
|
435
|
+
confirmProgrammatic,
|
|
436
|
+
cancelProgrammatic,
|
|
437
|
+
reset,
|
|
438
|
+
getValue,
|
|
439
|
+
setValue,
|
|
440
|
+
]
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
// Declarative open prop support
|
|
444
|
+
useEffect(() => {
|
|
445
|
+
if (propOpen === true && !isOpenRef.current) {
|
|
446
|
+
openPicker().catch(() => {});
|
|
447
|
+
} else if (propOpen === false && isOpenRef.current) {
|
|
448
|
+
closePicker().catch(() => {});
|
|
449
|
+
}
|
|
450
|
+
}, [propOpen, openPicker, closePicker]);
|
|
451
|
+
|
|
452
|
+
// Presets handling
|
|
453
|
+
const presets = useMemo(() => {
|
|
454
|
+
if (!showPresets) return [];
|
|
455
|
+
return customPresets || getStandardSinglePresets();
|
|
456
|
+
}, [showPresets, customPresets]);
|
|
457
|
+
|
|
458
|
+
const handleSelectPreset = useCallback(
|
|
459
|
+
(preset: DatePreset) => {
|
|
460
|
+
setActivePresetId(preset.id);
|
|
461
|
+
const val = preset.getValue();
|
|
462
|
+
if (val instanceof Date) {
|
|
463
|
+
setCurrentDate(val);
|
|
464
|
+
dateRef.current = val;
|
|
465
|
+
onChange?.(val);
|
|
466
|
+
}
|
|
467
|
+
},
|
|
468
|
+
[onChange]
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
const handleJsDateChange = useCallback(
|
|
472
|
+
(newDate: Date) => {
|
|
473
|
+
setActivePresetId(undefined);
|
|
474
|
+
setCurrentDate(newDate);
|
|
475
|
+
dateRef.current = newDate;
|
|
476
|
+
onChange?.(newDate);
|
|
477
|
+
},
|
|
478
|
+
[onChange]
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
const handleJsConfirm = useCallback(() => {
|
|
482
|
+
setIsJsModalVisible(false);
|
|
483
|
+
isOpenRef.current = false;
|
|
484
|
+
onConfirm?.(currentDate);
|
|
485
|
+
}, [currentDate, onConfirm]);
|
|
486
|
+
|
|
487
|
+
const handleJsCancel = useCallback(() => {
|
|
488
|
+
setIsJsModalVisible(false);
|
|
489
|
+
isOpenRef.current = false;
|
|
490
|
+
onCancel?.();
|
|
491
|
+
}, [onCancel]);
|
|
492
|
+
|
|
493
|
+
// If native, don't render JS UI (TurboModule handles presentation)
|
|
494
|
+
if (presentation === 'native') {
|
|
495
|
+
return null;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const defaultHeaderTitle =
|
|
499
|
+
title ||
|
|
500
|
+
(mode === 'time'
|
|
501
|
+
? localeData.selectTimeTitle
|
|
502
|
+
: mode === 'datetime'
|
|
503
|
+
? localeData.selectDateTimeTitle
|
|
504
|
+
: localeData.selectDateTitle);
|
|
505
|
+
|
|
506
|
+
const isModalVisible = isJsModalVisible;
|
|
507
|
+
|
|
508
|
+
// Render JS UI based on mode
|
|
509
|
+
const renderPickerBody = () => {
|
|
510
|
+
if (mode === 'time') {
|
|
511
|
+
return (
|
|
512
|
+
<ClockPicker
|
|
513
|
+
value={currentDate}
|
|
514
|
+
is24Hour={is24Hour}
|
|
515
|
+
minuteInterval={minuteInterval}
|
|
516
|
+
locale={localeData}
|
|
517
|
+
theme={resolvedTheme}
|
|
518
|
+
onChange={(res) => handleJsDateChange(res.date)}
|
|
519
|
+
/>
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (mode === 'countdown') {
|
|
524
|
+
const minutesItems = Array.from({ length: 60 }, (_, i) => ({
|
|
525
|
+
label: `${i} min`,
|
|
526
|
+
value: i,
|
|
527
|
+
}));
|
|
528
|
+
return (
|
|
529
|
+
<View style={styles.countdownContainer}>
|
|
530
|
+
<WheelPicker
|
|
531
|
+
items={minutesItems}
|
|
532
|
+
selectedValue={currentDate.getMinutes()}
|
|
533
|
+
theme={resolvedTheme}
|
|
534
|
+
onChange={(val) => {
|
|
535
|
+
const d = new Date(currentDate);
|
|
536
|
+
d.setMinutes(val);
|
|
537
|
+
handleJsDateChange(d);
|
|
538
|
+
}}
|
|
539
|
+
/>
|
|
540
|
+
</View>
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (mode === 'datetime') {
|
|
545
|
+
return (
|
|
546
|
+
<View>
|
|
547
|
+
{/* Tab selector between Date and Time */}
|
|
548
|
+
<View
|
|
549
|
+
style={[
|
|
550
|
+
styles.tabsRow,
|
|
551
|
+
{
|
|
552
|
+
borderColor: resolvedTheme.colors.border,
|
|
553
|
+
backgroundColor: resolvedTheme.colors.card,
|
|
554
|
+
},
|
|
555
|
+
]}
|
|
556
|
+
>
|
|
557
|
+
<TouchableOpacity
|
|
558
|
+
onPress={() => setDateTimeTab('date')}
|
|
559
|
+
style={[
|
|
560
|
+
styles.tabBtn,
|
|
561
|
+
dateTimeTab === 'date' && {
|
|
562
|
+
backgroundColor: resolvedTheme.colors.primary,
|
|
563
|
+
},
|
|
564
|
+
]}
|
|
565
|
+
>
|
|
566
|
+
<Text
|
|
567
|
+
style={[
|
|
568
|
+
styles.tabText,
|
|
569
|
+
{
|
|
570
|
+
color:
|
|
571
|
+
dateTimeTab === 'date'
|
|
572
|
+
? resolvedTheme.colors.primaryText
|
|
573
|
+
: resolvedTheme.colors.textSecondary,
|
|
574
|
+
fontWeight: dateTimeTab === 'date' ? 'bold' : 'normal',
|
|
575
|
+
},
|
|
576
|
+
]}
|
|
577
|
+
>
|
|
578
|
+
Date
|
|
579
|
+
</Text>
|
|
580
|
+
</TouchableOpacity>
|
|
581
|
+
|
|
582
|
+
<TouchableOpacity
|
|
583
|
+
onPress={() => setDateTimeTab('time')}
|
|
584
|
+
style={[
|
|
585
|
+
styles.tabBtn,
|
|
586
|
+
dateTimeTab === 'time' && {
|
|
587
|
+
backgroundColor: resolvedTheme.colors.primary,
|
|
588
|
+
},
|
|
589
|
+
]}
|
|
590
|
+
>
|
|
591
|
+
<Text
|
|
592
|
+
style={[
|
|
593
|
+
styles.tabText,
|
|
594
|
+
{
|
|
595
|
+
color:
|
|
596
|
+
dateTimeTab === 'time'
|
|
597
|
+
? resolvedTheme.colors.primaryText
|
|
598
|
+
: resolvedTheme.colors.textSecondary,
|
|
599
|
+
fontWeight: dateTimeTab === 'time' ? 'bold' : 'normal',
|
|
600
|
+
},
|
|
601
|
+
]}
|
|
602
|
+
>
|
|
603
|
+
Time
|
|
604
|
+
</Text>
|
|
605
|
+
</TouchableOpacity>
|
|
606
|
+
</View>
|
|
607
|
+
|
|
608
|
+
{dateTimeTab === 'date' ? (
|
|
609
|
+
<CalendarPicker
|
|
610
|
+
value={currentDate}
|
|
611
|
+
onChange={handleJsDateChange}
|
|
612
|
+
minimumDate={minimumDate}
|
|
613
|
+
maximumDate={maximumDate}
|
|
614
|
+
disabledDates={disabledDates}
|
|
615
|
+
disabledWeekdays={disabledWeekdays}
|
|
616
|
+
markedDates={markedDates}
|
|
617
|
+
firstDayOfWeek={firstDayOfWeek}
|
|
618
|
+
locale={localeData}
|
|
619
|
+
theme={resolvedTheme}
|
|
620
|
+
/>
|
|
621
|
+
) : (
|
|
622
|
+
<ClockPicker
|
|
623
|
+
value={currentDate}
|
|
624
|
+
is24Hour={is24Hour}
|
|
625
|
+
minuteInterval={minuteInterval}
|
|
626
|
+
locale={localeData}
|
|
627
|
+
theme={resolvedTheme}
|
|
628
|
+
onChange={(res) => {
|
|
629
|
+
const combined = new Date(currentDate);
|
|
630
|
+
combined.setHours(res.hour, res.minute);
|
|
631
|
+
handleJsDateChange(combined);
|
|
632
|
+
}}
|
|
633
|
+
/>
|
|
634
|
+
)}
|
|
635
|
+
</View>
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// Default mode === 'date'
|
|
640
|
+
return (
|
|
641
|
+
<CalendarPicker
|
|
642
|
+
value={currentDate}
|
|
643
|
+
onChange={handleJsDateChange}
|
|
644
|
+
minimumDate={minimumDate}
|
|
645
|
+
maximumDate={maximumDate}
|
|
646
|
+
disabledDates={disabledDates}
|
|
647
|
+
disabledWeekdays={disabledWeekdays}
|
|
648
|
+
markedDates={markedDates}
|
|
649
|
+
firstDayOfWeek={firstDayOfWeek}
|
|
650
|
+
locale={localeData}
|
|
651
|
+
theme={resolvedTheme}
|
|
652
|
+
/>
|
|
653
|
+
);
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
const content = (
|
|
657
|
+
<View style={styles.innerContent}>
|
|
658
|
+
{presentation !== 'inline' && (
|
|
659
|
+
<PickerHeader
|
|
660
|
+
title={defaultHeaderTitle}
|
|
661
|
+
cancelText={cancelText || localeData.cancelText}
|
|
662
|
+
confirmText={confirmText || localeData.confirmText}
|
|
663
|
+
onCancel={handleJsCancel}
|
|
664
|
+
onConfirm={handleJsConfirm}
|
|
665
|
+
theme={resolvedTheme}
|
|
666
|
+
isRTL={localeData.isRTL}
|
|
667
|
+
/>
|
|
668
|
+
)}
|
|
669
|
+
|
|
670
|
+
{showPresets && presets.length > 0 && (
|
|
671
|
+
<PresetsBar
|
|
672
|
+
presets={presets}
|
|
673
|
+
activePresetId={activePresetId}
|
|
674
|
+
onSelectPreset={handleSelectPreset}
|
|
675
|
+
theme={resolvedTheme}
|
|
676
|
+
isRTL={localeData.isRTL}
|
|
677
|
+
/>
|
|
678
|
+
)}
|
|
679
|
+
|
|
680
|
+
{renderPickerBody()}
|
|
681
|
+
</View>
|
|
682
|
+
);
|
|
683
|
+
|
|
684
|
+
return (
|
|
685
|
+
<ModalWrapper
|
|
686
|
+
testID={testID}
|
|
687
|
+
presentation={presentation}
|
|
688
|
+
visible={isModalVisible}
|
|
689
|
+
onClose={handleJsCancel}
|
|
690
|
+
theme={resolvedTheme}
|
|
691
|
+
backdropOpacity={backdropOpacity}
|
|
692
|
+
animationType={animationType}
|
|
693
|
+
showHandle={showHandle}
|
|
694
|
+
closeOnBackdropPress={closeOnBackdropPress}
|
|
695
|
+
>
|
|
696
|
+
{content}
|
|
697
|
+
</ModalWrapper>
|
|
698
|
+
);
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
DateTimePicker.displayName = 'DateTimePicker';
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Imperative helper function to open the native Date Time Picker modal.
|
|
705
|
+
*/
|
|
706
|
+
export async function openDateTimePickerModal(
|
|
707
|
+
options: OpenPickerOptions
|
|
708
|
+
): Promise<Date | null> {
|
|
709
|
+
const {
|
|
710
|
+
mode = 'date',
|
|
711
|
+
value = new Date(),
|
|
712
|
+
minimumDate,
|
|
713
|
+
maximumDate,
|
|
714
|
+
display = 'default',
|
|
715
|
+
locale,
|
|
716
|
+
timeZoneOffsetInMinutes,
|
|
717
|
+
is24Hour,
|
|
718
|
+
minuteInterval,
|
|
719
|
+
title,
|
|
720
|
+
confirmText,
|
|
721
|
+
cancelText,
|
|
722
|
+
theme = 'auto',
|
|
723
|
+
accentColor,
|
|
724
|
+
textColor,
|
|
725
|
+
backgroundColor,
|
|
726
|
+
disabledDates,
|
|
727
|
+
disabledWeekdays,
|
|
728
|
+
onChange,
|
|
729
|
+
onConfirm,
|
|
730
|
+
onCancel,
|
|
731
|
+
onDismiss,
|
|
732
|
+
onError,
|
|
733
|
+
} = options;
|
|
734
|
+
|
|
735
|
+
if (minuteInterval !== undefined && !validateMinuteInterval(minuteInterval)) {
|
|
736
|
+
const err = new DateTimePickerError(
|
|
737
|
+
'INVALID_MINUTE_INTERVAL',
|
|
738
|
+
`minuteInterval must be a positive integer dividing 60. Received: ${minuteInterval}`
|
|
739
|
+
);
|
|
740
|
+
onError?.(err);
|
|
741
|
+
throw err;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
if (
|
|
745
|
+
minimumDate &&
|
|
746
|
+
maximumDate &&
|
|
747
|
+
!validateDateRange(minimumDate, maximumDate)
|
|
748
|
+
) {
|
|
749
|
+
const err = new DateTimePickerError(
|
|
750
|
+
'DATE_OUT_OF_RANGE',
|
|
751
|
+
'minimumDate cannot be after maximumDate.'
|
|
752
|
+
);
|
|
753
|
+
onError?.(err);
|
|
754
|
+
throw err;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
const parsedValue = parseDate(value) || new Date();
|
|
758
|
+
const minTimestamp = minimumDate ? dateToTimestamp(minimumDate) : undefined;
|
|
759
|
+
const maxTimestamp = maximumDate ? dateToTimestamp(maximumDate) : undefined;
|
|
760
|
+
|
|
761
|
+
const nativeTheme: NativePickerTheme =
|
|
762
|
+
typeof theme === 'string' &&
|
|
763
|
+
(theme === 'light' || theme === 'dark' || theme === 'auto')
|
|
764
|
+
? theme
|
|
765
|
+
: 'auto';
|
|
766
|
+
|
|
767
|
+
const nativeOptions: NativePickerOptions = {
|
|
768
|
+
value: parsedValue.getTime(),
|
|
769
|
+
minimumDate: minTimestamp ?? undefined,
|
|
770
|
+
maximumDate: maxTimestamp ?? undefined,
|
|
771
|
+
display,
|
|
772
|
+
locale: typeof locale === 'string' ? locale : locale?.code,
|
|
773
|
+
timeZoneOffsetInMinutes,
|
|
774
|
+
is24Hour,
|
|
775
|
+
minuteInterval,
|
|
776
|
+
title,
|
|
777
|
+
confirmText,
|
|
778
|
+
cancelText,
|
|
779
|
+
theme: nativeTheme,
|
|
780
|
+
accentColor,
|
|
781
|
+
textColor,
|
|
782
|
+
backgroundColor,
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
try {
|
|
786
|
+
let result: { action: NativePickerAction; timestamp: number };
|
|
787
|
+
|
|
788
|
+
switch (mode) {
|
|
789
|
+
case 'time':
|
|
790
|
+
result = await NativeDateTimePicker.openTimePicker(nativeOptions);
|
|
791
|
+
break;
|
|
792
|
+
case 'datetime':
|
|
793
|
+
result = await NativeDateTimePicker.openDateTimePicker(nativeOptions);
|
|
794
|
+
break;
|
|
795
|
+
case 'countdown':
|
|
796
|
+
result = await NativeDateTimePicker.openCountdownPicker(nativeOptions);
|
|
797
|
+
break;
|
|
798
|
+
case 'date':
|
|
799
|
+
default:
|
|
800
|
+
result = await NativeDateTimePicker.openDatePicker(nativeOptions);
|
|
801
|
+
break;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
const selectedDate = timestampToDate(result.timestamp);
|
|
805
|
+
if (result.action === 'confirmed') {
|
|
806
|
+
if (isDateDisabled(selectedDate, disabledDates, disabledWeekdays)) {
|
|
807
|
+
const err = new DateTimePickerError(
|
|
808
|
+
'DATE_OUT_OF_RANGE',
|
|
809
|
+
'Selected date is disabled.'
|
|
810
|
+
);
|
|
811
|
+
onError?.(err);
|
|
812
|
+
}
|
|
813
|
+
onChange?.(selectedDate);
|
|
814
|
+
onConfirm?.(selectedDate);
|
|
815
|
+
return selectedDate;
|
|
816
|
+
} else if (result.action === 'cancelled') {
|
|
817
|
+
onCancel?.();
|
|
818
|
+
return null;
|
|
819
|
+
} else {
|
|
820
|
+
onDismiss?.();
|
|
821
|
+
return null;
|
|
822
|
+
}
|
|
823
|
+
} catch (error) {
|
|
824
|
+
const normalized =
|
|
825
|
+
error instanceof DateTimePickerError
|
|
826
|
+
? error
|
|
827
|
+
: new DateTimePickerError(
|
|
828
|
+
'UNKNOWN_ERROR',
|
|
829
|
+
error instanceof Error ? error.message : 'Unknown error',
|
|
830
|
+
error
|
|
831
|
+
);
|
|
832
|
+
onError?.(normalized);
|
|
833
|
+
throw normalized;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
const styles = StyleSheet.create({
|
|
838
|
+
innerContent: {
|
|
839
|
+
width: '100%',
|
|
840
|
+
},
|
|
841
|
+
countdownContainer: {
|
|
842
|
+
padding: 16,
|
|
843
|
+
},
|
|
844
|
+
tabsRow: {
|
|
845
|
+
flexDirection: 'row',
|
|
846
|
+
marginHorizontal: 16,
|
|
847
|
+
marginTop: 12,
|
|
848
|
+
marginBottom: 8,
|
|
849
|
+
borderRadius: 8,
|
|
850
|
+
borderWidth: 1,
|
|
851
|
+
overflow: 'hidden',
|
|
852
|
+
},
|
|
853
|
+
tabBtn: {
|
|
854
|
+
flex: 1,
|
|
855
|
+
paddingVertical: 8,
|
|
856
|
+
alignItems: 'center',
|
|
857
|
+
justifyContent: 'center',
|
|
858
|
+
},
|
|
859
|
+
tabText: {
|
|
860
|
+
fontSize: 14,
|
|
861
|
+
},
|
|
862
|
+
});
|