@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,216 @@
|
|
|
1
|
+
import React, { useCallback, 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
|
+
|
|
8
|
+
export interface MonthPickerProps {
|
|
9
|
+
value?: Date | { year: number; month: number };
|
|
10
|
+
onChange?: (result: { year: number; month: number; date: Date }) => void;
|
|
11
|
+
minYear?: number;
|
|
12
|
+
maxYear?: number;
|
|
13
|
+
locale?: string | PartialLocaleData;
|
|
14
|
+
theme?: ThemeMode | ThemeConfig;
|
|
15
|
+
testID?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const MonthPicker: React.FC<MonthPickerProps> = ({
|
|
19
|
+
value,
|
|
20
|
+
onChange,
|
|
21
|
+
minYear = 1900,
|
|
22
|
+
maxYear = 2100,
|
|
23
|
+
locale: propLocale,
|
|
24
|
+
theme: propTheme,
|
|
25
|
+
testID,
|
|
26
|
+
}) => {
|
|
27
|
+
const theme = useTheme(propTheme);
|
|
28
|
+
const locale = useLocale(propLocale);
|
|
29
|
+
|
|
30
|
+
const initialYear =
|
|
31
|
+
value instanceof Date
|
|
32
|
+
? value.getFullYear()
|
|
33
|
+
: value?.year || new Date().getFullYear();
|
|
34
|
+
const initialMonth =
|
|
35
|
+
value instanceof Date
|
|
36
|
+
? value.getMonth()
|
|
37
|
+
: value?.month !== undefined
|
|
38
|
+
? value.month
|
|
39
|
+
: new Date().getMonth();
|
|
40
|
+
|
|
41
|
+
const [currentYear, setCurrentYear] = useState<number>(initialYear);
|
|
42
|
+
const [selectedMonth, setSelectedMonth] = useState<number>(initialMonth);
|
|
43
|
+
|
|
44
|
+
const prevYear = useCallback(() => {
|
|
45
|
+
if (currentYear > minYear) {
|
|
46
|
+
setCurrentYear((y) => y - 1);
|
|
47
|
+
}
|
|
48
|
+
}, [currentYear, minYear]);
|
|
49
|
+
|
|
50
|
+
const nextYear = useCallback(() => {
|
|
51
|
+
if (currentYear < maxYear) {
|
|
52
|
+
setCurrentYear((y) => y + 1);
|
|
53
|
+
}
|
|
54
|
+
}, [currentYear, maxYear]);
|
|
55
|
+
|
|
56
|
+
const selectMonth = useCallback(
|
|
57
|
+
(monthIndex: number) => {
|
|
58
|
+
setSelectedMonth(monthIndex);
|
|
59
|
+
const date = new Date(currentYear, monthIndex, 1);
|
|
60
|
+
onChange?.({ year: currentYear, month: monthIndex, date });
|
|
61
|
+
},
|
|
62
|
+
[currentYear, onChange]
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<View
|
|
67
|
+
testID={testID}
|
|
68
|
+
style={[styles.container, { backgroundColor: theme.colors.background }]}
|
|
69
|
+
>
|
|
70
|
+
{/* Year Selector Header */}
|
|
71
|
+
<View
|
|
72
|
+
style={[
|
|
73
|
+
styles.headerRow,
|
|
74
|
+
{ flexDirection: locale.isRTL ? 'row-reverse' : 'row' },
|
|
75
|
+
]}
|
|
76
|
+
>
|
|
77
|
+
<TouchableOpacity
|
|
78
|
+
accessibilityRole="button"
|
|
79
|
+
accessibilityLabel="Previous Year"
|
|
80
|
+
onPress={prevYear}
|
|
81
|
+
disabled={currentYear <= minYear}
|
|
82
|
+
style={styles.navButton}
|
|
83
|
+
>
|
|
84
|
+
<Text
|
|
85
|
+
style={[
|
|
86
|
+
styles.navArrow,
|
|
87
|
+
{
|
|
88
|
+
color:
|
|
89
|
+
currentYear <= minYear
|
|
90
|
+
? theme.colors.disabledText
|
|
91
|
+
: theme.colors.primary,
|
|
92
|
+
},
|
|
93
|
+
]}
|
|
94
|
+
>
|
|
95
|
+
{locale.isRTL ? '›' : '‹'}
|
|
96
|
+
</Text>
|
|
97
|
+
</TouchableOpacity>
|
|
98
|
+
|
|
99
|
+
<Text
|
|
100
|
+
style={[
|
|
101
|
+
styles.yearTitle,
|
|
102
|
+
{
|
|
103
|
+
color: theme.colors.text,
|
|
104
|
+
fontSize: theme.typography.fontSizeLarge,
|
|
105
|
+
fontWeight: theme.typography.fontWeightBold,
|
|
106
|
+
},
|
|
107
|
+
]}
|
|
108
|
+
>
|
|
109
|
+
{currentYear}
|
|
110
|
+
</Text>
|
|
111
|
+
|
|
112
|
+
<TouchableOpacity
|
|
113
|
+
accessibilityRole="button"
|
|
114
|
+
accessibilityLabel="Next Year"
|
|
115
|
+
onPress={nextYear}
|
|
116
|
+
disabled={currentYear >= maxYear}
|
|
117
|
+
style={styles.navButton}
|
|
118
|
+
>
|
|
119
|
+
<Text
|
|
120
|
+
style={[
|
|
121
|
+
styles.navArrow,
|
|
122
|
+
{
|
|
123
|
+
color:
|
|
124
|
+
currentYear >= maxYear
|
|
125
|
+
? theme.colors.disabledText
|
|
126
|
+
: theme.colors.primary,
|
|
127
|
+
},
|
|
128
|
+
]}
|
|
129
|
+
>
|
|
130
|
+
{locale.isRTL ? '‹' : '›'}
|
|
131
|
+
</Text>
|
|
132
|
+
</TouchableOpacity>
|
|
133
|
+
</View>
|
|
134
|
+
|
|
135
|
+
{/* 3x4 Month Grid */}
|
|
136
|
+
<View
|
|
137
|
+
style={[
|
|
138
|
+
styles.monthsGrid,
|
|
139
|
+
{ flexDirection: locale.isRTL ? 'row-reverse' : 'row' },
|
|
140
|
+
]}
|
|
141
|
+
>
|
|
142
|
+
{locale.monthsShort.map((monthName, idx) => {
|
|
143
|
+
const isSelected = selectedMonth === idx;
|
|
144
|
+
return (
|
|
145
|
+
<TouchableOpacity
|
|
146
|
+
key={`month-${idx}`}
|
|
147
|
+
onPress={() => selectMonth(idx)}
|
|
148
|
+
style={[
|
|
149
|
+
styles.monthCell,
|
|
150
|
+
isSelected && {
|
|
151
|
+
backgroundColor: theme.colors.selected,
|
|
152
|
+
borderRadius: theme.borderRadius.md,
|
|
153
|
+
},
|
|
154
|
+
]}
|
|
155
|
+
>
|
|
156
|
+
<Text
|
|
157
|
+
style={[
|
|
158
|
+
styles.monthText,
|
|
159
|
+
{
|
|
160
|
+
color: isSelected
|
|
161
|
+
? theme.colors.selectedText
|
|
162
|
+
: theme.colors.text,
|
|
163
|
+
fontSize: theme.typography.fontSizeMedium,
|
|
164
|
+
fontWeight: isSelected
|
|
165
|
+
? theme.typography.fontWeightBold
|
|
166
|
+
: theme.typography.fontWeightRegular,
|
|
167
|
+
},
|
|
168
|
+
]}
|
|
169
|
+
>
|
|
170
|
+
{monthName}
|
|
171
|
+
</Text>
|
|
172
|
+
</TouchableOpacity>
|
|
173
|
+
);
|
|
174
|
+
})}
|
|
175
|
+
</View>
|
|
176
|
+
</View>
|
|
177
|
+
);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const styles = StyleSheet.create({
|
|
181
|
+
container: {
|
|
182
|
+
padding: 16,
|
|
183
|
+
},
|
|
184
|
+
headerRow: {
|
|
185
|
+
alignItems: 'center',
|
|
186
|
+
justifyContent: 'space-between',
|
|
187
|
+
marginBottom: 16,
|
|
188
|
+
},
|
|
189
|
+
yearTitle: {
|
|
190
|
+
textAlign: 'center',
|
|
191
|
+
},
|
|
192
|
+
navButton: {
|
|
193
|
+
width: 36,
|
|
194
|
+
height: 36,
|
|
195
|
+
justifyContent: 'center',
|
|
196
|
+
alignItems: 'center',
|
|
197
|
+
},
|
|
198
|
+
navArrow: {
|
|
199
|
+
fontSize: 24,
|
|
200
|
+
lineHeight: 26,
|
|
201
|
+
},
|
|
202
|
+
monthsGrid: {
|
|
203
|
+
flexWrap: 'wrap',
|
|
204
|
+
justifyContent: 'space-between',
|
|
205
|
+
rowGap: 12,
|
|
206
|
+
},
|
|
207
|
+
monthCell: {
|
|
208
|
+
width: '31%',
|
|
209
|
+
height: 48,
|
|
210
|
+
justifyContent: 'center',
|
|
211
|
+
alignItems: 'center',
|
|
212
|
+
},
|
|
213
|
+
monthText: {
|
|
214
|
+
textAlign: 'center',
|
|
215
|
+
},
|
|
216
|
+
});
|
|
@@ -0,0 +1,190 @@
|
|
|
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 { useTheme } from '../theme';
|
|
7
|
+
import type { ThemeConfig, ThemeMode } from '../theme/types';
|
|
8
|
+
import type { MinuteInterval } from '../types';
|
|
9
|
+
import type { BaseModalProps, PresentationMode } from '../types/presentation';
|
|
10
|
+
import { parseDate, timestampToDate } from '../utils/dateConversion';
|
|
11
|
+
import { ClockPicker } from './ClockPicker';
|
|
12
|
+
import { ModalWrapper } from './common/ModalWrapper';
|
|
13
|
+
import { PickerHeader } from './common/PickerHeader';
|
|
14
|
+
|
|
15
|
+
export interface TimePickerRef {
|
|
16
|
+
open: () => Promise<Date | null> | void;
|
|
17
|
+
close: () => Promise<boolean> | void;
|
|
18
|
+
getValue: () => Date | null;
|
|
19
|
+
setValue: (time: Date | string | number) => void;
|
|
20
|
+
reset: () => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface TimePickerProps extends BaseModalProps {
|
|
24
|
+
presentation?: PresentationMode;
|
|
25
|
+
value?: Date | string | number | null;
|
|
26
|
+
defaultValue?: Date | string | number | null;
|
|
27
|
+
onChange?: (date: Date) => void;
|
|
28
|
+
onConfirmTime?: (date: Date) => void;
|
|
29
|
+
is24Hour?: boolean;
|
|
30
|
+
minuteInterval?: MinuteInterval;
|
|
31
|
+
locale?: string | PartialLocaleData;
|
|
32
|
+
theme?: ThemeMode | ThemeConfig;
|
|
33
|
+
testID?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const TimePicker = forwardRef<TimePickerRef, TimePickerProps>(
|
|
37
|
+
(
|
|
38
|
+
{
|
|
39
|
+
presentation = 'bottom-sheet',
|
|
40
|
+
visible: propVisible,
|
|
41
|
+
onClose,
|
|
42
|
+
title,
|
|
43
|
+
confirmText,
|
|
44
|
+
cancelText,
|
|
45
|
+
onConfirmTime,
|
|
46
|
+
onCancel,
|
|
47
|
+
value: propValue,
|
|
48
|
+
defaultValue,
|
|
49
|
+
onChange,
|
|
50
|
+
is24Hour = false,
|
|
51
|
+
minuteInterval = 1,
|
|
52
|
+
locale: propLocale,
|
|
53
|
+
theme: propTheme,
|
|
54
|
+
backdropOpacity,
|
|
55
|
+
animationType,
|
|
56
|
+
showHandle,
|
|
57
|
+
closeOnBackdropPress,
|
|
58
|
+
testID,
|
|
59
|
+
},
|
|
60
|
+
ref
|
|
61
|
+
) => {
|
|
62
|
+
const theme = useTheme(propTheme);
|
|
63
|
+
const locale = useLocale(propLocale);
|
|
64
|
+
|
|
65
|
+
const initialDate =
|
|
66
|
+
parseDate(propValue) || parseDate(defaultValue) || new Date();
|
|
67
|
+
const [selectedTime, setSelectedTime] = useState<Date>(initialDate);
|
|
68
|
+
const [isVisible, setIsVisible] = useState(false);
|
|
69
|
+
|
|
70
|
+
const modalVisible = propVisible !== undefined ? propVisible : isVisible;
|
|
71
|
+
|
|
72
|
+
useImperativeHandle(ref, () => ({
|
|
73
|
+
open: async () => {
|
|
74
|
+
if (presentation === 'native') {
|
|
75
|
+
const res = await NativeDateTimePicker.openTimePicker({
|
|
76
|
+
value: selectedTime.getTime(),
|
|
77
|
+
is24Hour,
|
|
78
|
+
minuteInterval,
|
|
79
|
+
title: title || locale.selectTimeTitle,
|
|
80
|
+
confirmText: confirmText || locale.confirmText,
|
|
81
|
+
cancelText: cancelText || locale.cancelText,
|
|
82
|
+
});
|
|
83
|
+
if (res.action === 'confirmed') {
|
|
84
|
+
const confirmedDate = timestampToDate(res.timestamp);
|
|
85
|
+
setSelectedTime(confirmedDate);
|
|
86
|
+
onChange?.(confirmedDate);
|
|
87
|
+
onConfirmTime?.(confirmedDate);
|
|
88
|
+
return confirmedDate;
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
} else {
|
|
92
|
+
setIsVisible(true);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
close: async () => {
|
|
97
|
+
if (presentation === 'native') {
|
|
98
|
+
return await NativeDateTimePicker.closePicker();
|
|
99
|
+
} else {
|
|
100
|
+
setIsVisible(false);
|
|
101
|
+
onClose?.();
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
getValue: () => selectedTime,
|
|
106
|
+
setValue: (newTime: Date | string | number) => {
|
|
107
|
+
const parsed = parseDate(newTime);
|
|
108
|
+
if (parsed) {
|
|
109
|
+
setSelectedTime(parsed);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
reset: () => {
|
|
113
|
+
setSelectedTime(initialDate);
|
|
114
|
+
},
|
|
115
|
+
}));
|
|
116
|
+
|
|
117
|
+
const handleClockChange = useCallback(
|
|
118
|
+
(res: { date: Date }) => {
|
|
119
|
+
setSelectedTime(res.date);
|
|
120
|
+
onChange?.(res.date);
|
|
121
|
+
},
|
|
122
|
+
[onChange]
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const handleConfirm = useCallback(() => {
|
|
126
|
+
onConfirmTime?.(selectedTime);
|
|
127
|
+
setIsVisible(false);
|
|
128
|
+
onClose?.();
|
|
129
|
+
}, [selectedTime, onConfirmTime, onClose]);
|
|
130
|
+
|
|
131
|
+
const handleCancel = useCallback(() => {
|
|
132
|
+
onCancel?.();
|
|
133
|
+
setIsVisible(false);
|
|
134
|
+
onClose?.();
|
|
135
|
+
}, [onCancel, onClose]);
|
|
136
|
+
|
|
137
|
+
if (presentation === 'native') {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const content = (
|
|
142
|
+
<View style={styles.innerContent}>
|
|
143
|
+
{presentation !== 'inline' && (
|
|
144
|
+
<PickerHeader
|
|
145
|
+
title={title || locale.selectTimeTitle}
|
|
146
|
+
cancelText={cancelText || locale.cancelText}
|
|
147
|
+
confirmText={confirmText || locale.confirmText}
|
|
148
|
+
onCancel={handleCancel}
|
|
149
|
+
onConfirm={handleConfirm}
|
|
150
|
+
theme={theme}
|
|
151
|
+
isRTL={locale.isRTL}
|
|
152
|
+
/>
|
|
153
|
+
)}
|
|
154
|
+
|
|
155
|
+
<ClockPicker
|
|
156
|
+
value={selectedTime}
|
|
157
|
+
onChange={handleClockChange}
|
|
158
|
+
is24Hour={is24Hour}
|
|
159
|
+
minuteInterval={minuteInterval}
|
|
160
|
+
locale={locale}
|
|
161
|
+
theme={theme}
|
|
162
|
+
/>
|
|
163
|
+
</View>
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<ModalWrapper
|
|
168
|
+
testID={testID}
|
|
169
|
+
presentation={presentation}
|
|
170
|
+
visible={modalVisible}
|
|
171
|
+
onClose={handleCancel}
|
|
172
|
+
theme={theme}
|
|
173
|
+
backdropOpacity={backdropOpacity}
|
|
174
|
+
animationType={animationType}
|
|
175
|
+
showHandle={showHandle}
|
|
176
|
+
closeOnBackdropPress={closeOnBackdropPress}
|
|
177
|
+
>
|
|
178
|
+
{content}
|
|
179
|
+
</ModalWrapper>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
TimePicker.displayName = 'TimePicker';
|
|
185
|
+
|
|
186
|
+
const styles = StyleSheet.create({
|
|
187
|
+
innerContent: {
|
|
188
|
+
width: '100%',
|
|
189
|
+
},
|
|
190
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import React, { useCallback, useMemo, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
ScrollView,
|
|
4
|
+
StyleSheet,
|
|
5
|
+
Text,
|
|
6
|
+
TouchableOpacity,
|
|
7
|
+
View,
|
|
8
|
+
} from 'react-native';
|
|
9
|
+
import { generateTimeSlots, type TimeSlot } from '../slots';
|
|
10
|
+
import { useTheme } from '../theme';
|
|
11
|
+
import type { ThemeConfig, ThemeMode } from '../theme/types';
|
|
12
|
+
|
|
13
|
+
export interface TimeSlotPickerProps {
|
|
14
|
+
slots?: TimeSlot[];
|
|
15
|
+
startTime?: string; // "09:00"
|
|
16
|
+
endTime?: string; // "18:00"
|
|
17
|
+
intervalMinutes?: number;
|
|
18
|
+
disabledSlots?: string[];
|
|
19
|
+
is24Hour?: boolean;
|
|
20
|
+
selectedSlotId?: string;
|
|
21
|
+
onSelectSlot?: (slot: TimeSlot) => void;
|
|
22
|
+
theme?: ThemeMode | ThemeConfig;
|
|
23
|
+
columns?: number;
|
|
24
|
+
testID?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const TimeSlotPicker: React.FC<TimeSlotPickerProps> = ({
|
|
28
|
+
slots: propSlots,
|
|
29
|
+
startTime,
|
|
30
|
+
endTime,
|
|
31
|
+
intervalMinutes,
|
|
32
|
+
disabledSlots,
|
|
33
|
+
is24Hour,
|
|
34
|
+
selectedSlotId: propSelectedId,
|
|
35
|
+
onSelectSlot,
|
|
36
|
+
theme: propTheme,
|
|
37
|
+
columns = 3,
|
|
38
|
+
testID,
|
|
39
|
+
}) => {
|
|
40
|
+
const theme = useTheme(propTheme);
|
|
41
|
+
|
|
42
|
+
// If user provided slots, use them; otherwise auto-generate
|
|
43
|
+
const availableSlots = useMemo(() => {
|
|
44
|
+
if (propSlots && propSlots.length > 0) {
|
|
45
|
+
return propSlots;
|
|
46
|
+
}
|
|
47
|
+
return generateTimeSlots({
|
|
48
|
+
startTime,
|
|
49
|
+
endTime,
|
|
50
|
+
intervalMinutes,
|
|
51
|
+
disabledSlots,
|
|
52
|
+
is24Hour,
|
|
53
|
+
});
|
|
54
|
+
}, [propSlots, startTime, endTime, intervalMinutes, disabledSlots, is24Hour]);
|
|
55
|
+
|
|
56
|
+
const [selectedId, setSelectedId] = useState<string | undefined>(
|
|
57
|
+
propSelectedId || availableSlots[0]?.id
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const activeId = propSelectedId !== undefined ? propSelectedId : selectedId;
|
|
61
|
+
|
|
62
|
+
const handleSlotPress = useCallback(
|
|
63
|
+
(slot: TimeSlot) => {
|
|
64
|
+
if (!slot.available) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
setSelectedId(slot.id);
|
|
68
|
+
onSelectSlot?.(slot);
|
|
69
|
+
},
|
|
70
|
+
[onSelectSlot]
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const colWidthPercent = `${Math.floor(100 / columns) - 2}%`;
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<View
|
|
77
|
+
testID={testID}
|
|
78
|
+
style={[styles.container, { backgroundColor: theme.colors.background }]}
|
|
79
|
+
>
|
|
80
|
+
<ScrollView
|
|
81
|
+
showsVerticalScrollIndicator={false}
|
|
82
|
+
contentContainerStyle={styles.slotsGrid}
|
|
83
|
+
>
|
|
84
|
+
{availableSlots.map((slot) => {
|
|
85
|
+
const isSelected = activeId === slot.id;
|
|
86
|
+
const isDisabled = !slot.available;
|
|
87
|
+
|
|
88
|
+
let bg = theme.colors.card;
|
|
89
|
+
let border = theme.colors.border;
|
|
90
|
+
let textColor = theme.colors.text;
|
|
91
|
+
|
|
92
|
+
if (isDisabled) {
|
|
93
|
+
bg = theme.colors.disabled;
|
|
94
|
+
textColor = theme.colors.disabledText;
|
|
95
|
+
border = 'transparent';
|
|
96
|
+
} else if (isSelected) {
|
|
97
|
+
bg = theme.colors.selected;
|
|
98
|
+
textColor = theme.colors.selectedText;
|
|
99
|
+
border = theme.colors.selected;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
<TouchableOpacity
|
|
104
|
+
key={slot.id}
|
|
105
|
+
accessibilityRole="button"
|
|
106
|
+
accessibilityLabel={`${slot.formattedStart} to ${slot.formattedEnd}${isDisabled ? ', unavailable' : ''}`}
|
|
107
|
+
accessibilityState={{
|
|
108
|
+
selected: isSelected,
|
|
109
|
+
disabled: isDisabled,
|
|
110
|
+
}}
|
|
111
|
+
disabled={isDisabled}
|
|
112
|
+
onPress={() => handleSlotPress(slot)}
|
|
113
|
+
style={[
|
|
114
|
+
styles.slotButton,
|
|
115
|
+
{
|
|
116
|
+
width: colWidthPercent as any,
|
|
117
|
+
backgroundColor: bg,
|
|
118
|
+
borderColor: border,
|
|
119
|
+
borderRadius: theme.borderRadius.md,
|
|
120
|
+
},
|
|
121
|
+
]}
|
|
122
|
+
>
|
|
123
|
+
<Text
|
|
124
|
+
style={[
|
|
125
|
+
styles.slotText,
|
|
126
|
+
{
|
|
127
|
+
color: textColor,
|
|
128
|
+
fontSize: theme.typography.fontSizeSmall,
|
|
129
|
+
fontWeight: isSelected ? 'bold' : 'normal',
|
|
130
|
+
},
|
|
131
|
+
]}
|
|
132
|
+
>
|
|
133
|
+
{slot.formattedStart}
|
|
134
|
+
</Text>
|
|
135
|
+
</TouchableOpacity>
|
|
136
|
+
);
|
|
137
|
+
})}
|
|
138
|
+
</ScrollView>
|
|
139
|
+
</View>
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const styles = StyleSheet.create({
|
|
144
|
+
container: {
|
|
145
|
+
padding: 12,
|
|
146
|
+
maxHeight: 280,
|
|
147
|
+
},
|
|
148
|
+
slotsGrid: {
|
|
149
|
+
flexDirection: 'row',
|
|
150
|
+
flexWrap: 'wrap',
|
|
151
|
+
gap: 8,
|
|
152
|
+
justifyContent: 'flex-start',
|
|
153
|
+
},
|
|
154
|
+
slotButton: {
|
|
155
|
+
height: 42,
|
|
156
|
+
justifyContent: 'center',
|
|
157
|
+
alignItems: 'center',
|
|
158
|
+
borderWidth: 1,
|
|
159
|
+
},
|
|
160
|
+
slotText: {
|
|
161
|
+
textAlign: 'center',
|
|
162
|
+
},
|
|
163
|
+
});
|