@expressive-ui/native 0.2.1 → 0.4.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/README.md +11 -1
- package/dist/Feedback.d.ts +3 -3
- package/dist/Feedback.d.ts.map +1 -1
- package/dist/Feedback.js +73 -14
- package/dist/Feedback.js.map +1 -1
- package/dist/Fields.d.ts +10 -6
- package/dist/Fields.d.ts.map +1 -1
- package/dist/Fields.js +125 -29
- package/dist/Fields.js.map +1 -1
- package/dist/FileUpload.d.ts.map +1 -1
- package/dist/FileUpload.js +61 -19
- package/dist/FileUpload.js.map +1 -1
- package/dist/NumberField.d.ts +6 -2
- package/dist/NumberField.d.ts.map +1 -1
- package/dist/NumberField.js +71 -11
- package/dist/NumberField.js.map +1 -1
- package/dist/OtpField.d.ts +34 -0
- package/dist/OtpField.d.ts.map +1 -0
- package/dist/OtpField.js +156 -0
- package/dist/OtpField.js.map +1 -0
- package/dist/Pickers.d.ts.map +1 -1
- package/dist/Pickers.js +227 -36
- package/dist/Pickers.js.map +1 -1
- package/dist/RenderUI.d.ts +3 -0
- package/dist/RenderUI.d.ts.map +1 -0
- package/dist/RenderUI.js +45 -0
- package/dist/RenderUI.js.map +1 -0
- package/dist/Search.js +1 -1
- package/dist/Search.js.map +1 -1
- package/dist/Select.d.ts +5 -1
- package/dist/Select.d.ts.map +1 -1
- package/dist/Select.js +52 -10
- package/dist/Select.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/theme.d.ts +3 -1
- package/dist/theme.d.ts.map +1 -1
- package/dist/theme.js +36 -11
- package/dist/theme.js.map +1 -1
- package/package.json +4 -4
- package/src/Feedback.tsx +211 -16
- package/src/Fields.tsx +341 -58
- package/src/FileUpload.tsx +169 -7
- package/src/NumberField.tsx +187 -9
- package/src/OtpField.tsx +353 -0
- package/src/Pickers.tsx +783 -86
- package/src/RenderUI.tsx +47 -0
- package/src/Search.tsx +1 -1
- package/src/Select.tsx +209 -27
- package/src/index.ts +3 -0
- package/src/theme.tsx +77 -14
package/src/Pickers.tsx
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
1
|
import { Fragment, useEffect, useRef, type ReactNode } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Keyboard,
|
|
4
|
+
Pressable,
|
|
5
|
+
ScrollView,
|
|
6
|
+
StyleSheet,
|
|
7
|
+
TextInput,
|
|
8
|
+
useWindowDimensions,
|
|
9
|
+
View,
|
|
10
|
+
type StyleProp,
|
|
11
|
+
type ViewStyle,
|
|
12
|
+
} from 'react-native';
|
|
3
13
|
import Svg, { Circle, Line, Path, Rect } from 'react-native-svg';
|
|
4
14
|
import {
|
|
5
|
-
resolveTimePickerLayout,
|
|
6
|
-
|
|
15
|
+
resolveTimePickerLayout,
|
|
16
|
+
calendarDays,
|
|
17
|
+
dateInputPattern,
|
|
18
|
+
dateKey,
|
|
19
|
+
dialValueAt,
|
|
20
|
+
padTime,
|
|
21
|
+
parseDate,
|
|
22
|
+
pickerLabels,
|
|
23
|
+
pickerYears,
|
|
24
|
+
useDatePickerState,
|
|
25
|
+
useDateRangePickerState,
|
|
26
|
+
useTimePickerState,
|
|
27
|
+
type DatePickerOptions,
|
|
28
|
+
type DateRangePickerOptions,
|
|
29
|
+
type TimePickerOptions,
|
|
7
30
|
} from '@expressive-ui/core';
|
|
8
31
|
import { Dialog } from './Dialogs.js';
|
|
9
32
|
import { Button } from './Button.js';
|
|
@@ -11,25 +34,130 @@ import { TextField } from './Fields.js';
|
|
|
11
34
|
import { Icon, T } from './primitives.js';
|
|
12
35
|
import { useExpressiveTheme } from './theme.js';
|
|
13
36
|
|
|
14
|
-
function PickerIcon({
|
|
37
|
+
function PickerIcon({
|
|
38
|
+
name,
|
|
39
|
+
label,
|
|
40
|
+
onPress,
|
|
41
|
+
disabled,
|
|
42
|
+
}: {
|
|
43
|
+
name: string;
|
|
44
|
+
label: string;
|
|
45
|
+
onPress: () => void;
|
|
46
|
+
disabled?: boolean;
|
|
47
|
+
}) {
|
|
15
48
|
const { colors } = useExpressiveTheme();
|
|
16
|
-
return
|
|
49
|
+
return (
|
|
50
|
+
<Pressable
|
|
51
|
+
accessibilityRole="button"
|
|
52
|
+
accessibilityLabel={label}
|
|
53
|
+
accessibilityState={{ disabled }}
|
|
54
|
+
disabled={disabled}
|
|
55
|
+
onPress={onPress}
|
|
56
|
+
style={({ pressed }) => [
|
|
57
|
+
styles.icon,
|
|
58
|
+
{
|
|
59
|
+
opacity: disabled ? 0.38 : 1,
|
|
60
|
+
backgroundColor: pressed ? colors.surfaceContainerHighest : undefined,
|
|
61
|
+
},
|
|
62
|
+
]}
|
|
63
|
+
>
|
|
64
|
+
<Icon name={name} color="onSurfaceVariant" />
|
|
65
|
+
</Pressable>
|
|
66
|
+
);
|
|
17
67
|
}
|
|
18
|
-
function PickerActions({
|
|
19
|
-
|
|
68
|
+
function PickerActions({
|
|
69
|
+
cancel,
|
|
70
|
+
confirm,
|
|
71
|
+
onClose,
|
|
72
|
+
onConfirm,
|
|
73
|
+
valid,
|
|
74
|
+
toggle,
|
|
75
|
+
}: {
|
|
76
|
+
cancel: string;
|
|
77
|
+
confirm: string;
|
|
78
|
+
onClose: () => void;
|
|
79
|
+
onConfirm: () => void;
|
|
80
|
+
valid: boolean;
|
|
81
|
+
toggle?: ReactNode;
|
|
82
|
+
}) {
|
|
83
|
+
return (
|
|
84
|
+
<View style={styles.actions}>
|
|
85
|
+
{toggle}
|
|
86
|
+
<View style={{ flex: 1 }} />
|
|
87
|
+
<Button variant="text" action={{ id: 'cancel', label: cancel }} onPress={onClose} />
|
|
88
|
+
<Button
|
|
89
|
+
variant="text"
|
|
90
|
+
action={{ id: 'confirm', label: confirm }}
|
|
91
|
+
disabled={!valid}
|
|
92
|
+
onPress={onConfirm}
|
|
93
|
+
/>
|
|
94
|
+
</View>
|
|
95
|
+
);
|
|
20
96
|
}
|
|
21
97
|
|
|
22
|
-
function TimeModeToggle({
|
|
98
|
+
function TimeModeToggle({
|
|
99
|
+
input,
|
|
100
|
+
label,
|
|
101
|
+
onPress,
|
|
102
|
+
}: {
|
|
103
|
+
input: boolean;
|
|
104
|
+
label: string;
|
|
105
|
+
onPress: () => void;
|
|
106
|
+
}) {
|
|
23
107
|
const { colors } = useExpressiveTheme();
|
|
24
|
-
return
|
|
25
|
-
<
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
108
|
+
return (
|
|
109
|
+
<Pressable
|
|
110
|
+
accessibilityRole="button"
|
|
111
|
+
accessibilityLabel={label}
|
|
112
|
+
accessibilityState={{ selected: input }}
|
|
113
|
+
onPress={() => {
|
|
114
|
+
if (input) Keyboard.dismiss();
|
|
115
|
+
onPress();
|
|
116
|
+
}}
|
|
117
|
+
style={({ pressed }) => ({
|
|
118
|
+
width: 48,
|
|
119
|
+
height: 48,
|
|
120
|
+
borderRadius: 24,
|
|
121
|
+
alignItems: 'center',
|
|
122
|
+
justifyContent: 'center',
|
|
123
|
+
backgroundColor: pressed ? colors.surfaceContainerHighest : undefined,
|
|
124
|
+
})}
|
|
125
|
+
>
|
|
126
|
+
<Svg
|
|
127
|
+
width={24}
|
|
128
|
+
height={24}
|
|
129
|
+
viewBox="0 0 24 24"
|
|
130
|
+
fill="none"
|
|
131
|
+
stroke={colors.onSurfaceVariant}
|
|
132
|
+
strokeWidth={1.8}
|
|
133
|
+
strokeLinecap="round"
|
|
134
|
+
strokeLinejoin="round"
|
|
135
|
+
accessible={false}
|
|
136
|
+
>
|
|
137
|
+
{input ? (
|
|
138
|
+
<>
|
|
139
|
+
<Circle cx={12} cy={12} r={9} />
|
|
140
|
+
<Path d="M12 7v5l3 2" />
|
|
141
|
+
</>
|
|
142
|
+
) : (
|
|
143
|
+
<>
|
|
144
|
+
<Rect x={2} y={5} width={20} height={14} rx={2} />
|
|
145
|
+
<Path d="M5 9h1m3 0h1m3 0h1m3 0h1M5 12h1m3 0h1m3 0h1m3 0h1M7 15h10" />
|
|
146
|
+
</>
|
|
147
|
+
)}
|
|
148
|
+
</Svg>
|
|
149
|
+
</Pressable>
|
|
150
|
+
);
|
|
29
151
|
}
|
|
30
152
|
|
|
31
|
-
export type DatePickerProps = DatePickerOptions & {
|
|
32
|
-
|
|
153
|
+
export type DatePickerProps = DatePickerOptions & {
|
|
154
|
+
variant?: 'modal' | 'docked';
|
|
155
|
+
style?: StyleProp<ViewStyle>;
|
|
156
|
+
};
|
|
157
|
+
export type DateRangePickerProps = DateRangePickerOptions & {
|
|
158
|
+
variant?: 'modal' | 'docked';
|
|
159
|
+
style?: StyleProp<ViewStyle>;
|
|
160
|
+
};
|
|
33
161
|
export type { DateRange } from '@expressive-ui/core';
|
|
34
162
|
export function DatePicker(props: DatePickerProps) {
|
|
35
163
|
const { locale: themeLocale } = useExpressiveTheme();
|
|
@@ -52,98 +180,667 @@ type DatePickerSurfaceProps = Omit<DatePickerProps, 'value' | 'onConfirm'> & {
|
|
|
52
180
|
};
|
|
53
181
|
function DatePickerSurface(props: DatePickerSurfaceProps) {
|
|
54
182
|
const { colors } = useExpressiveTheme();
|
|
55
|
-
const {
|
|
56
|
-
|
|
183
|
+
const {
|
|
184
|
+
open,
|
|
185
|
+
onClose,
|
|
186
|
+
title,
|
|
187
|
+
fieldLabel,
|
|
188
|
+
cancelLabel,
|
|
189
|
+
confirmLabel,
|
|
190
|
+
min,
|
|
191
|
+
max,
|
|
192
|
+
style,
|
|
193
|
+
locale,
|
|
194
|
+
state,
|
|
195
|
+
range,
|
|
196
|
+
startLabel,
|
|
197
|
+
endLabel,
|
|
198
|
+
variant = 'modal',
|
|
199
|
+
} = props;
|
|
200
|
+
const labels = pickerLabels(locale, useExpressiveTheme().labels?.pickerLabels);
|
|
57
201
|
const rangeEnd = range?.range.end ?? '';
|
|
58
|
-
const shortDate = (date: string) =>
|
|
202
|
+
const shortDate = (date: string) =>
|
|
203
|
+
parseDate(date)?.toLocaleDateString(locale, { month: 'short', day: 'numeric' });
|
|
59
204
|
const list = useRef<ScrollView>(null);
|
|
60
205
|
const years = pickerYears(state.month, min, max);
|
|
61
206
|
useEffect(() => {
|
|
62
|
-
const index =
|
|
63
|
-
|
|
207
|
+
const index =
|
|
208
|
+
state.view === 'months' ? state.month.getMonth() : years.indexOf(state.month.getFullYear());
|
|
209
|
+
if (state.view !== 'days')
|
|
210
|
+
list.current?.scrollTo({ y: Math.max(0, index * 48 - 96), animated: false });
|
|
64
211
|
}, [state.view]);
|
|
65
|
-
const monthName = (m: number, long = false) =>
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
212
|
+
const monthName = (m: number, long = false) =>
|
|
213
|
+
new Date(2024, m, 1).toLocaleDateString(locale, { month: long ? 'long' : 'short' });
|
|
214
|
+
const menu = (view: 'months' | 'years', label: string, text: string) => (
|
|
215
|
+
<Pressable
|
|
216
|
+
accessibilityRole="button"
|
|
217
|
+
accessibilityLabel={label}
|
|
218
|
+
accessibilityState={{
|
|
219
|
+
expanded: state.view === view,
|
|
220
|
+
disabled: state.view !== 'days' && state.view !== view,
|
|
221
|
+
}}
|
|
222
|
+
disabled={state.view !== 'days' && state.view !== view}
|
|
223
|
+
onPress={() => state.setView(state.view === view ? 'days' : view)}
|
|
224
|
+
style={[
|
|
225
|
+
styles.menuTrigger,
|
|
226
|
+
{ opacity: state.view !== 'days' && state.view !== view ? 0.38 : 1 },
|
|
227
|
+
]}
|
|
228
|
+
>
|
|
229
|
+
<T variant="labelLarge" color={state.view === view ? 'primary' : 'onSurfaceVariant'}>
|
|
230
|
+
{text}
|
|
231
|
+
</T>
|
|
232
|
+
<Icon name="arrow_drop_down" size={18} />
|
|
233
|
+
</Pressable>
|
|
234
|
+
);
|
|
235
|
+
const content = (
|
|
236
|
+
<View style={{ gap: 16 }}>
|
|
237
|
+
<T variant="labelLarge" color="onSurfaceVariant">
|
|
238
|
+
{title ?? (range ? labels.range : labels.date)}
|
|
239
|
+
</T>
|
|
240
|
+
<View
|
|
241
|
+
style={[
|
|
242
|
+
styles.headline,
|
|
243
|
+
{ borderBottomColor: colors.outlineVariant, borderBottomWidth: 1, paddingBottom: 12 },
|
|
244
|
+
]}
|
|
245
|
+
>
|
|
246
|
+
<T variant={range ? 'headlineSmall' : 'headlineLarge'} style={{ flex: 1 }}>
|
|
247
|
+
{range
|
|
248
|
+
? `${shortDate(range.range.start) ?? labels.start} – ${
|
|
249
|
+
shortDate(range.range.end) ?? labels.end
|
|
250
|
+
}`
|
|
251
|
+
: parseDate(state.draft)?.toLocaleDateString(locale, {
|
|
252
|
+
weekday: 'short',
|
|
253
|
+
month: 'short',
|
|
254
|
+
day: 'numeric',
|
|
255
|
+
}) ?? '—'}
|
|
256
|
+
</T>
|
|
257
|
+
<PickerIcon
|
|
258
|
+
name={state.mode === 'calendar' ? 'edit' : 'calendar_today'}
|
|
259
|
+
label={state.mode === 'calendar' ? labels.input : labels.calendar}
|
|
260
|
+
onPress={() => {
|
|
261
|
+
state.setMode(state.mode === 'calendar' ? 'input' : 'calendar');
|
|
262
|
+
state.setView('days');
|
|
263
|
+
}}
|
|
264
|
+
/>
|
|
265
|
+
</View>
|
|
266
|
+
{state.mode === 'input' || variant === 'docked' ? (
|
|
267
|
+
range ? (
|
|
268
|
+
<View style={{ gap: 16 }}>
|
|
269
|
+
{(['start', 'end'] as const).map((part) => (
|
|
270
|
+
<TextField
|
|
271
|
+
key={part}
|
|
272
|
+
variant="outlined"
|
|
273
|
+
label={part === 'start' ? startLabel ?? labels.start : endLabel ?? labels.end}
|
|
274
|
+
value={part === 'start' ? range.startText : range.endText}
|
|
275
|
+
onChange={(text) => range.editPart(text, part)}
|
|
276
|
+
keyboardType="numbers-and-punctuation"
|
|
277
|
+
placeholder={dateInputPattern(locale)}
|
|
278
|
+
supportingText={dateInputPattern(locale)}
|
|
279
|
+
error={
|
|
280
|
+
(part === 'start' ? range.startText : range.endText) &&
|
|
281
|
+
(!range.available(range.range[part]) ||
|
|
282
|
+
!!(range.range.start && range.range.end && !range.valid))
|
|
283
|
+
? labels.rangeInvalid
|
|
284
|
+
: undefined
|
|
285
|
+
}
|
|
286
|
+
/>
|
|
287
|
+
))}
|
|
288
|
+
</View>
|
|
289
|
+
) : (
|
|
290
|
+
<TextField
|
|
291
|
+
variant="outlined"
|
|
292
|
+
label={fieldLabel ?? labels.dateField}
|
|
293
|
+
value={state.text}
|
|
294
|
+
onChange={state.edit}
|
|
295
|
+
keyboardType="numbers-and-punctuation"
|
|
296
|
+
placeholder={dateInputPattern(locale)}
|
|
297
|
+
supportingText={dateInputPattern(locale)}
|
|
298
|
+
error={state.text && !state.valid ? labels.invalid : undefined}
|
|
299
|
+
/>
|
|
300
|
+
)
|
|
301
|
+
) : null}
|
|
302
|
+
{state.mode === 'calendar' || variant === 'docked' ? (
|
|
303
|
+
<View>
|
|
304
|
+
<View style={styles.navigation}>
|
|
305
|
+
<View style={styles.navGroup}>
|
|
306
|
+
<PickerIcon
|
|
307
|
+
name="chevron_left"
|
|
308
|
+
label={labels.previous}
|
|
309
|
+
disabled={state.view === 'years' || !state.canNavigate(-1)}
|
|
310
|
+
onPress={() => state.navigate(-1)}
|
|
311
|
+
/>
|
|
312
|
+
{menu('months', labels.month, monthName(state.month.getMonth()))}
|
|
313
|
+
<PickerIcon
|
|
314
|
+
name="chevron_right"
|
|
315
|
+
label={labels.next}
|
|
316
|
+
disabled={state.view === 'years' || !state.canNavigate(1)}
|
|
317
|
+
onPress={() => state.navigate(1)}
|
|
318
|
+
/>
|
|
319
|
+
</View>
|
|
320
|
+
<View style={styles.navGroup}>
|
|
321
|
+
<PickerIcon
|
|
322
|
+
name="chevron_left"
|
|
323
|
+
label={labels.previousYear}
|
|
324
|
+
disabled={state.view === 'months' || !state.canNavigate(-12)}
|
|
325
|
+
onPress={() => state.navigate(-12)}
|
|
326
|
+
/>
|
|
327
|
+
{menu('years', labels.year, String(state.month.getFullYear()))}
|
|
328
|
+
<PickerIcon
|
|
329
|
+
name="chevron_right"
|
|
330
|
+
label={labels.nextYear}
|
|
331
|
+
disabled={state.view === 'months' || !state.canNavigate(12)}
|
|
332
|
+
onPress={() => state.navigate(12)}
|
|
333
|
+
/>
|
|
334
|
+
</View>
|
|
335
|
+
</View>
|
|
336
|
+
{state.view === 'days' ? (
|
|
337
|
+
<View style={styles.calendar}>
|
|
338
|
+
{Array.from({ length: 7 }, (_, i) => (
|
|
339
|
+
<View key={i} style={styles.day}>
|
|
340
|
+
<T variant="labelSmall" color="onSurfaceVariant">
|
|
341
|
+
{new Date(2024, 0, 7 + i).toLocaleDateString(locale, { weekday: 'narrow' })}
|
|
342
|
+
</T>
|
|
343
|
+
</View>
|
|
344
|
+
))}
|
|
345
|
+
{calendarDays(state.month).map((date) => {
|
|
346
|
+
const start = !!range && date === range.range.start,
|
|
347
|
+
end = !!range && date === range.range.end;
|
|
348
|
+
const selected = range ? start || end : state.draft === date,
|
|
349
|
+
today = dateKey(new Date()) === date,
|
|
350
|
+
enabled = state.available(date);
|
|
351
|
+
const inRange = !!(
|
|
352
|
+
range?.range.start &&
|
|
353
|
+
rangeEnd &&
|
|
354
|
+
date >= range.range.start &&
|
|
355
|
+
date <= rangeEnd
|
|
356
|
+
);
|
|
357
|
+
return (
|
|
358
|
+
<Pressable
|
|
359
|
+
key={date}
|
|
360
|
+
accessibilityRole="button"
|
|
361
|
+
accessibilityLabel={`${parseDate(date)!.toLocaleDateString(locale, {
|
|
362
|
+
dateStyle: 'full',
|
|
363
|
+
})}${start ? `, ${startLabel ?? labels.start}` : ''}${
|
|
364
|
+
end ? `, ${endLabel ?? labels.end}` : ''
|
|
365
|
+
}`}
|
|
366
|
+
accessibilityHint={range ? labels.rangeHelp : undefined}
|
|
367
|
+
accessibilityState={{ selected, disabled: !enabled }}
|
|
368
|
+
disabled={!enabled}
|
|
369
|
+
onPress={() => state.select(date)}
|
|
370
|
+
style={[
|
|
371
|
+
styles.day,
|
|
372
|
+
{
|
|
373
|
+
opacity: !enabled
|
|
374
|
+
? 0.38
|
|
375
|
+
: selected || inRange
|
|
376
|
+
? 1
|
|
377
|
+
: parseDate(date)!.getMonth() !== state.month.getMonth()
|
|
378
|
+
? 0.38
|
|
379
|
+
: 1,
|
|
380
|
+
},
|
|
381
|
+
]}
|
|
382
|
+
>
|
|
383
|
+
{inRange && !(start && end) ? (
|
|
384
|
+
<View
|
|
385
|
+
pointerEvents="none"
|
|
386
|
+
style={{
|
|
387
|
+
position: 'absolute',
|
|
388
|
+
top: 5,
|
|
389
|
+
bottom: 5,
|
|
390
|
+
left: start ? '50%' : 0,
|
|
391
|
+
right: end ? '50%' : 0,
|
|
392
|
+
backgroundColor: colors.secondaryContainer,
|
|
393
|
+
}}
|
|
394
|
+
/>
|
|
395
|
+
) : null}
|
|
396
|
+
<View
|
|
397
|
+
style={{
|
|
398
|
+
width: 34,
|
|
399
|
+
height: 34,
|
|
400
|
+
borderRadius: 17,
|
|
401
|
+
alignItems: 'center',
|
|
402
|
+
justifyContent: 'center',
|
|
403
|
+
backgroundColor: selected ? colors.primary : undefined,
|
|
404
|
+
borderWidth: today ? 1 : 0,
|
|
405
|
+
borderColor: colors.primary,
|
|
406
|
+
}}
|
|
407
|
+
>
|
|
408
|
+
<T
|
|
409
|
+
color={
|
|
410
|
+
selected
|
|
411
|
+
? 'onPrimary'
|
|
412
|
+
: today
|
|
413
|
+
? 'primary'
|
|
414
|
+
: inRange
|
|
415
|
+
? 'onSecondaryContainer'
|
|
416
|
+
: 'onSurface'
|
|
417
|
+
}
|
|
418
|
+
>
|
|
419
|
+
{parseDate(date)!.getDate()}
|
|
420
|
+
</T>
|
|
421
|
+
</View>
|
|
422
|
+
</Pressable>
|
|
423
|
+
);
|
|
424
|
+
})}
|
|
425
|
+
</View>
|
|
426
|
+
) : (
|
|
427
|
+
<ScrollView
|
|
428
|
+
ref={list}
|
|
429
|
+
style={{ height: 308, borderTopWidth: 1, borderColor: colors.outlineVariant }}
|
|
430
|
+
nestedScrollEnabled
|
|
431
|
+
>
|
|
432
|
+
{(state.view === 'months' ? Array.from({ length: 12 }, (_, i) => i) : years).map(
|
|
433
|
+
(n) => {
|
|
434
|
+
const selected =
|
|
435
|
+
n ===
|
|
436
|
+
(state.view === 'months' ? state.month.getMonth() : state.month.getFullYear());
|
|
437
|
+
const start = new Date(state.month);
|
|
438
|
+
start.setDate(1);
|
|
439
|
+
state.view === 'months' ? start.setMonth(n) : start.setFullYear(n, 0, 1);
|
|
440
|
+
const end = new Date(start);
|
|
441
|
+
state.view === 'months'
|
|
442
|
+
? end.setMonth(end.getMonth() + 1, 0)
|
|
443
|
+
: end.setFullYear(n + 1, 0, 0);
|
|
444
|
+
const disabled = !!((min && dateKey(end) < min) || (max && dateKey(start) > max));
|
|
445
|
+
return (
|
|
446
|
+
<Pressable
|
|
447
|
+
key={n}
|
|
448
|
+
accessibilityRole="button"
|
|
449
|
+
accessibilityState={{ selected, disabled }}
|
|
450
|
+
disabled={disabled}
|
|
451
|
+
onPress={() =>
|
|
452
|
+
state.view === 'months' ? state.chooseMonth(n) : state.chooseYear(n)
|
|
453
|
+
}
|
|
454
|
+
style={{
|
|
455
|
+
minHeight: 48,
|
|
456
|
+
paddingHorizontal: 16,
|
|
457
|
+
flexDirection: 'row',
|
|
458
|
+
alignItems: 'center',
|
|
459
|
+
gap: 16,
|
|
460
|
+
opacity: disabled ? 0.38 : 1,
|
|
461
|
+
backgroundColor: selected ? colors.secondaryContainer : undefined,
|
|
462
|
+
}}
|
|
463
|
+
>
|
|
464
|
+
<View style={{ width: 20 }}>
|
|
465
|
+
{selected ? (
|
|
466
|
+
<Icon name="check" size={20} color="onSecondaryContainer" />
|
|
467
|
+
) : null}
|
|
468
|
+
</View>
|
|
469
|
+
<T color={selected ? 'onSecondaryContainer' : 'onSurface'}>
|
|
470
|
+
{state.view === 'months' ? monthName(n, true) : n}
|
|
471
|
+
</T>
|
|
472
|
+
</Pressable>
|
|
473
|
+
);
|
|
474
|
+
},
|
|
475
|
+
)}
|
|
476
|
+
</ScrollView>
|
|
477
|
+
)}
|
|
75
478
|
</View>
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
479
|
+
) : null}
|
|
480
|
+
{range ? (
|
|
481
|
+
<T variant="bodySmall" color="onSurfaceVariant" accessibilityLiveRegion="polite">
|
|
482
|
+
{range.range.start && !range.range.end
|
|
483
|
+
? labels.end
|
|
484
|
+
: range.range.end && !range.valid
|
|
485
|
+
? labels.rangeInvalid
|
|
486
|
+
: labels.rangeHelp}
|
|
487
|
+
</T>
|
|
488
|
+
) : null}
|
|
489
|
+
<PickerActions
|
|
490
|
+
cancel={cancelLabel ?? labels.cancel}
|
|
491
|
+
confirm={confirmLabel ?? labels.confirm}
|
|
492
|
+
onClose={onClose}
|
|
493
|
+
onConfirm={state.confirm}
|
|
494
|
+
valid={state.valid}
|
|
495
|
+
/>
|
|
496
|
+
</View>
|
|
497
|
+
);
|
|
498
|
+
if (variant === 'docked')
|
|
499
|
+
return open ? (
|
|
500
|
+
<View
|
|
501
|
+
style={[
|
|
502
|
+
{
|
|
503
|
+
maxWidth: 360,
|
|
504
|
+
alignSelf: 'stretch',
|
|
505
|
+
borderRadius: 16,
|
|
506
|
+
padding: 16,
|
|
507
|
+
backgroundColor: colors.surfaceContainerHigh,
|
|
508
|
+
},
|
|
509
|
+
style,
|
|
510
|
+
]}
|
|
511
|
+
>
|
|
512
|
+
{content}
|
|
513
|
+
</View>
|
|
514
|
+
) : null;
|
|
515
|
+
return (
|
|
516
|
+
<Dialog open={open} onClose={onClose} style={[{ maxWidth: 360, borderRadius: 28 }, style]}>
|
|
517
|
+
{content}
|
|
518
|
+
</Dialog>
|
|
519
|
+
);
|
|
100
520
|
}
|
|
101
521
|
|
|
102
522
|
export type TimePickerProps = TimePickerOptions & { style?: StyleProp<ViewStyle> };
|
|
103
523
|
export function TimePicker(props: TimePickerProps) {
|
|
104
524
|
const { locale, colors } = useExpressiveTheme();
|
|
105
|
-
const {
|
|
106
|
-
|
|
107
|
-
|
|
525
|
+
const {
|
|
526
|
+
open,
|
|
527
|
+
onClose,
|
|
528
|
+
title,
|
|
529
|
+
cancelLabel,
|
|
530
|
+
confirmLabel,
|
|
531
|
+
format = '24h',
|
|
532
|
+
layout = 'auto',
|
|
533
|
+
style,
|
|
534
|
+
} = props;
|
|
535
|
+
const labels = pickerLabels(props.locale ?? locale, useExpressiveTheme().labels?.pickerLabels),
|
|
536
|
+
state = useTimePickerState(props);
|
|
537
|
+
const hourInput = useRef<TextInput>(null),
|
|
538
|
+
minuteInput = useRef<TextInput>(null);
|
|
108
539
|
const { width, height } = useWindowDimensions();
|
|
109
540
|
const inputMode = state.mode === 'input';
|
|
110
541
|
const horizontal = resolveTimePickerLayout(layout, width, height, state.mode) === 'horizontal';
|
|
111
|
-
const size = horizontal
|
|
112
|
-
|
|
113
|
-
|
|
542
|
+
const size = horizontal
|
|
543
|
+
? Math.min(256, ((Math.min(560, width - 48) - 72) * 1.1) / 2.1)
|
|
544
|
+
: Math.min(256, width - 96),
|
|
545
|
+
scale = size / 256,
|
|
546
|
+
center = size / 2;
|
|
547
|
+
useEffect(() => {
|
|
548
|
+
if (open && inputMode) (state.part === 'hour' ? hourInput : minuteInput).current?.focus();
|
|
549
|
+
}, [inputMode, open]);
|
|
550
|
+
const angle = (state.part === 'minute' ? state.minute / 60 : (state.h24 % 12) / 12) * Math.PI * 2;
|
|
114
551
|
const radius = (state.h24 >= 12 && format === '24h' && state.part === 'hour' ? 65 : 100) * scale;
|
|
115
552
|
const selected = state.part === 'hour' ? state.hour : state.minute;
|
|
116
|
-
const periodSelector =
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
553
|
+
const periodSelector =
|
|
554
|
+
format === '12h' ? (
|
|
555
|
+
<View
|
|
556
|
+
style={{
|
|
557
|
+
borderWidth: 1,
|
|
558
|
+
borderColor: colors.outline,
|
|
559
|
+
borderRadius: 8,
|
|
560
|
+
overflow: 'hidden',
|
|
561
|
+
flexDirection: horizontal ? 'row' : 'column',
|
|
562
|
+
width: horizontal ? '100%' : 52,
|
|
563
|
+
}}
|
|
564
|
+
>
|
|
565
|
+
{['AM', 'PM'].map((period, i) => (
|
|
566
|
+
<Pressable
|
|
567
|
+
key={period}
|
|
568
|
+
accessibilityRole="button"
|
|
569
|
+
accessibilityState={{ selected: state.period === i }}
|
|
570
|
+
onPress={() => state.setPeriod(i)}
|
|
571
|
+
style={{
|
|
572
|
+
minWidth: 46,
|
|
573
|
+
flex: horizontal ? 1 : undefined,
|
|
574
|
+
height: 39,
|
|
575
|
+
alignItems: 'center',
|
|
576
|
+
justifyContent: 'center',
|
|
577
|
+
borderTopWidth: horizontal ? 0 : i,
|
|
578
|
+
borderLeftWidth: horizontal ? i : 0,
|
|
579
|
+
borderColor: colors.outline,
|
|
580
|
+
backgroundColor: state.period === i ? colors.tertiaryContainer : undefined,
|
|
581
|
+
}}
|
|
582
|
+
>
|
|
583
|
+
<T color={state.period === i ? 'onTertiaryContainer' : 'onSurfaceVariant'}>{period}</T>
|
|
584
|
+
</Pressable>
|
|
585
|
+
))}
|
|
137
586
|
</View>
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
587
|
+
) : null;
|
|
588
|
+
return (
|
|
589
|
+
<Dialog
|
|
590
|
+
open={open}
|
|
591
|
+
onClose={onClose}
|
|
592
|
+
style={[{ maxWidth: horizontal ? 560 : 360, borderRadius: 28 }, style]}
|
|
593
|
+
>
|
|
594
|
+
<View style={{ gap: 24 }}>
|
|
595
|
+
<T variant="labelLarge" color="onSurfaceVariant">
|
|
596
|
+
{title ?? (inputMode ? labels.enterTime : labels.time)}
|
|
597
|
+
</T>
|
|
598
|
+
<View
|
|
599
|
+
style={{ flexDirection: horizontal ? 'row' : 'column', gap: 24, alignItems: 'center' }}
|
|
600
|
+
>
|
|
601
|
+
<View
|
|
602
|
+
style={{ flex: horizontal ? 1 : undefined, width: horizontal ? undefined : '100%' }}
|
|
603
|
+
>
|
|
604
|
+
<View
|
|
605
|
+
style={{
|
|
606
|
+
gap: 16,
|
|
607
|
+
width: '100%',
|
|
608
|
+
maxWidth: format === '12h' && !horizontal ? 284 : 256,
|
|
609
|
+
alignSelf: 'center',
|
|
610
|
+
}}
|
|
611
|
+
>
|
|
612
|
+
<View style={[styles.time, { gap: format === '12h' ? 8 : 4 }]}>
|
|
613
|
+
{(['hour', 'minute'] as const).map((part, i) => (
|
|
614
|
+
<Fragment key={part}>
|
|
615
|
+
{i ? (
|
|
616
|
+
<T
|
|
617
|
+
accessible={false}
|
|
618
|
+
style={{
|
|
619
|
+
width: 16,
|
|
620
|
+
height: 80,
|
|
621
|
+
fontSize: 56,
|
|
622
|
+
lineHeight: 80,
|
|
623
|
+
textAlign: 'center',
|
|
624
|
+
}}
|
|
625
|
+
>
|
|
626
|
+
:
|
|
627
|
+
</T>
|
|
628
|
+
) : null}
|
|
629
|
+
<View style={{ gap: 7, flex: 1, minWidth: 0 }}>
|
|
630
|
+
{inputMode ? (
|
|
631
|
+
<TextInput
|
|
632
|
+
ref={part === 'hour' ? hourInput : minuteInput}
|
|
633
|
+
accessibilityLabel={labels[part]}
|
|
634
|
+
keyboardType="number-pad"
|
|
635
|
+
selectTextOnFocus
|
|
636
|
+
maxLength={2}
|
|
637
|
+
value={part === 'hour' ? state.hourText : state.minuteText}
|
|
638
|
+
onFocus={() => state.setPart(part)}
|
|
639
|
+
onBlur={() => state.normalize(part)}
|
|
640
|
+
onChangeText={(text) => state.edit(text, part)}
|
|
641
|
+
onSubmitEditing={() =>
|
|
642
|
+
part === 'hour' ? minuteInput.current?.focus() : state.confirm()
|
|
643
|
+
}
|
|
644
|
+
style={{
|
|
645
|
+
width: '100%',
|
|
646
|
+
height: 80,
|
|
647
|
+
fontSize: 56,
|
|
648
|
+
textAlign: 'center',
|
|
649
|
+
borderRadius: 8,
|
|
650
|
+
borderWidth: 2,
|
|
651
|
+
borderColor: !(part === 'hour' ? state.hourValid : state.minuteValid)
|
|
652
|
+
? colors.error
|
|
653
|
+
: state.part === part
|
|
654
|
+
? colors.primary
|
|
655
|
+
: 'transparent',
|
|
656
|
+
color:
|
|
657
|
+
state.part === part ? colors.onPrimaryContainer : colors.onSurface,
|
|
658
|
+
backgroundColor:
|
|
659
|
+
state.part === part
|
|
660
|
+
? colors.primaryContainer
|
|
661
|
+
: colors.surfaceContainerHighest,
|
|
662
|
+
}}
|
|
663
|
+
/>
|
|
664
|
+
) : (
|
|
665
|
+
<Pressable
|
|
666
|
+
accessibilityRole="button"
|
|
667
|
+
accessibilityLabel={labels[part]}
|
|
668
|
+
accessibilityState={{ selected: state.part === part }}
|
|
669
|
+
onPress={() => state.setPart(part)}
|
|
670
|
+
onLongPress={() => {
|
|
671
|
+
state.setPart(part);
|
|
672
|
+
state.setMode('input');
|
|
673
|
+
}}
|
|
674
|
+
style={{
|
|
675
|
+
width: '100%',
|
|
676
|
+
height: 80,
|
|
677
|
+
justifyContent: 'center',
|
|
678
|
+
alignItems: 'center',
|
|
679
|
+
borderRadius: 8,
|
|
680
|
+
backgroundColor:
|
|
681
|
+
state.part === part
|
|
682
|
+
? colors.primaryContainer
|
|
683
|
+
: colors.surfaceContainerHighest,
|
|
684
|
+
}}
|
|
685
|
+
>
|
|
686
|
+
<T
|
|
687
|
+
style={{ fontSize: 56, lineHeight: 68 }}
|
|
688
|
+
color={state.part === part ? 'onPrimaryContainer' : 'onSurface'}
|
|
689
|
+
>
|
|
690
|
+
{padTime(part === 'hour' ? state.hour : state.minute)}
|
|
691
|
+
</T>
|
|
692
|
+
</Pressable>
|
|
693
|
+
)}
|
|
694
|
+
{inputMode ? (
|
|
695
|
+
<T variant="labelSmall" color="onSurfaceVariant">
|
|
696
|
+
{labels[part]}
|
|
697
|
+
</T>
|
|
698
|
+
) : null}
|
|
699
|
+
</View>
|
|
700
|
+
</Fragment>
|
|
701
|
+
))}
|
|
702
|
+
{!horizontal ? periodSelector : null}
|
|
703
|
+
</View>
|
|
704
|
+
{horizontal ? periodSelector : null}
|
|
705
|
+
</View>
|
|
706
|
+
{inputMode && !state.valid ? (
|
|
707
|
+
<T color="error" accessibilityRole="alert">
|
|
708
|
+
{labels.invalid}
|
|
709
|
+
</T>
|
|
710
|
+
) : null}
|
|
711
|
+
</View>
|
|
712
|
+
{!inputMode ? (
|
|
713
|
+
<View
|
|
714
|
+
accessibilityLabel={labels[state.part]}
|
|
715
|
+
style={{
|
|
716
|
+
width: size,
|
|
717
|
+
height: size,
|
|
718
|
+
alignSelf: 'center',
|
|
719
|
+
borderRadius: center,
|
|
720
|
+
backgroundColor: colors.surfaceContainerHighest,
|
|
721
|
+
}}
|
|
722
|
+
>
|
|
723
|
+
<View
|
|
724
|
+
style={StyleSheet.absoluteFill}
|
|
725
|
+
onStartShouldSetResponder={() => true}
|
|
726
|
+
onMoveShouldSetResponder={() => true}
|
|
727
|
+
onResponderGrant={(event) =>
|
|
728
|
+
state.select(
|
|
729
|
+
dialValueAt(
|
|
730
|
+
event.nativeEvent.locationX,
|
|
731
|
+
event.nativeEvent.locationY,
|
|
732
|
+
size,
|
|
733
|
+
state.part,
|
|
734
|
+
format,
|
|
735
|
+
),
|
|
736
|
+
)
|
|
737
|
+
}
|
|
738
|
+
onResponderMove={(event) =>
|
|
739
|
+
state.select(
|
|
740
|
+
dialValueAt(
|
|
741
|
+
event.nativeEvent.locationX,
|
|
742
|
+
event.nativeEvent.locationY,
|
|
743
|
+
size,
|
|
744
|
+
state.part,
|
|
745
|
+
format,
|
|
746
|
+
),
|
|
747
|
+
)
|
|
748
|
+
}
|
|
749
|
+
onResponderRelease={() => {
|
|
750
|
+
if (state.part === 'hour') state.setPart('minute');
|
|
751
|
+
}}
|
|
752
|
+
/>
|
|
753
|
+
<Svg width={size} height={size} style={StyleSheet.absoluteFill} pointerEvents="none">
|
|
754
|
+
<Line
|
|
755
|
+
x1={center}
|
|
756
|
+
y1={center}
|
|
757
|
+
x2={center + Math.sin(angle) * radius}
|
|
758
|
+
y2={center - Math.cos(angle) * radius}
|
|
759
|
+
stroke={colors.primary}
|
|
760
|
+
strokeWidth={2}
|
|
761
|
+
/>
|
|
762
|
+
<Circle cx={center} cy={center} r={4} fill={colors.primary} />
|
|
763
|
+
<Circle
|
|
764
|
+
cx={center + Math.sin(angle) * radius}
|
|
765
|
+
cy={center - Math.cos(angle) * radius}
|
|
766
|
+
r={20 * scale}
|
|
767
|
+
fill={colors.primary}
|
|
768
|
+
/>
|
|
769
|
+
</Svg>
|
|
770
|
+
{Array.from(
|
|
771
|
+
{ length: state.part === 'hour' && format === '24h' ? 24 : 12 },
|
|
772
|
+
(_, i) => {
|
|
773
|
+
const n = state.part === 'minute' ? i * 5 : format === '12h' ? i || 12 : i;
|
|
774
|
+
const a = ((i % 12) / 12) * Math.PI * 2,
|
|
775
|
+
r = (i >= 12 ? 65 : 100) * scale;
|
|
776
|
+
return (
|
|
777
|
+
<Pressable
|
|
778
|
+
key={n}
|
|
779
|
+
accessibilityRole="button"
|
|
780
|
+
accessibilityLabel={`${labels[state.part]} ${n}`}
|
|
781
|
+
accessibilityState={{ selected: selected === n }}
|
|
782
|
+
onPress={() => {
|
|
783
|
+
state.select(n);
|
|
784
|
+
if (state.part === 'hour') state.setPart('minute');
|
|
785
|
+
}}
|
|
786
|
+
style={{
|
|
787
|
+
position: 'absolute',
|
|
788
|
+
left: center - 20 * scale + Math.sin(a) * r,
|
|
789
|
+
top: center - 20 * scale - Math.cos(a) * r,
|
|
790
|
+
width: 40 * scale,
|
|
791
|
+
height: 40 * scale,
|
|
792
|
+
borderRadius: 20,
|
|
793
|
+
alignItems: 'center',
|
|
794
|
+
justifyContent: 'center',
|
|
795
|
+
}}
|
|
796
|
+
>
|
|
797
|
+
<T
|
|
798
|
+
variant={i >= 12 ? 'labelSmall' : 'bodyLarge'}
|
|
799
|
+
color={selected === n ? 'onPrimary' : 'onSurface'}
|
|
800
|
+
>
|
|
801
|
+
{state.part === 'minute' || (format === '24h' && n === 0) ? padTime(n) : n}
|
|
802
|
+
</T>
|
|
803
|
+
</Pressable>
|
|
804
|
+
);
|
|
805
|
+
},
|
|
806
|
+
)}
|
|
807
|
+
</View>
|
|
808
|
+
) : null}
|
|
809
|
+
</View>
|
|
810
|
+
<PickerActions
|
|
811
|
+
cancel={cancelLabel ?? labels.cancel}
|
|
812
|
+
confirm={confirmLabel ?? labels.confirm}
|
|
813
|
+
onClose={onClose}
|
|
814
|
+
onConfirm={state.confirm}
|
|
815
|
+
valid={state.valid}
|
|
816
|
+
toggle={
|
|
817
|
+
<TimeModeToggle
|
|
818
|
+
input={inputMode}
|
|
819
|
+
label={inputMode ? labels.dial : labels.input}
|
|
820
|
+
onPress={state.toggle}
|
|
821
|
+
/>
|
|
822
|
+
}
|
|
823
|
+
/>
|
|
824
|
+
</View>
|
|
825
|
+
</Dialog>
|
|
826
|
+
);
|
|
141
827
|
}
|
|
142
828
|
const styles = StyleSheet.create({
|
|
143
|
-
icon: {
|
|
829
|
+
icon: {
|
|
830
|
+
width: 28,
|
|
831
|
+
minHeight: 48,
|
|
832
|
+
borderRadius: 24,
|
|
833
|
+
alignItems: 'center',
|
|
834
|
+
justifyContent: 'center',
|
|
835
|
+
},
|
|
144
836
|
actions: { flexDirection: 'row', alignItems: 'center', gap: 8 },
|
|
145
837
|
headline: { flexDirection: 'row', alignItems: 'center', gap: 8 },
|
|
146
|
-
navigation: {
|
|
838
|
+
navigation: {
|
|
839
|
+
flexDirection: 'row',
|
|
840
|
+
alignItems: 'center',
|
|
841
|
+
justifyContent: 'space-between',
|
|
842
|
+
minHeight: 48,
|
|
843
|
+
},
|
|
147
844
|
navGroup: { flexDirection: 'row', alignItems: 'center', flexShrink: 1 },
|
|
148
845
|
menuTrigger: { flexDirection: 'row', alignItems: 'center', gap: 2, minHeight: 48 },
|
|
149
846
|
calendar: { flexDirection: 'row', flexWrap: 'wrap' },
|