@plexui/react-native 0.2.4 → 0.2.5
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/dist/es/components/DatePicker/Calendar.js +112 -0
- package/dist/es/components/DatePicker/DatePicker.js +335 -0
- package/dist/es/components/DatePicker/TimeDial.js +123 -0
- package/dist/es/components/DatePicker/Wheel.js +164 -0
- package/dist/es/components/DatePicker/dates.js +147 -0
- package/dist/es/components/Dialog/Dialog.js +21 -0
- package/dist/es/components/Icon/material.js +16 -0
- package/dist/es/index.js +1 -0
- package/dist/types/components/DatePicker/Calendar.d.ts +29 -0
- package/dist/types/components/DatePicker/DatePicker.d.ts +57 -0
- package/dist/types/components/DatePicker/TimeDial.d.ts +20 -0
- package/dist/types/components/DatePicker/Wheel.d.ts +40 -0
- package/dist/types/components/DatePicker/dates.d.ts +45 -0
- package/dist/types/components/Dialog/Dialog.d.ts +18 -0
- package/dist/types/components/Icon/material.d.ts +6 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/DatePicker/Calendar.tsx +227 -0
- package/src/components/DatePicker/DatePicker.tsx +608 -0
- package/src/components/DatePicker/TimeDial.tsx +201 -0
- package/src/components/DatePicker/Wheel.tsx +247 -0
- package/src/components/DatePicker/dates.ts +161 -0
- package/src/components/Dialog/Dialog.tsx +46 -0
- package/src/components/Icon/material.ts +18 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import { useContext, useEffect, useRef, useState, type ReactNode } from "react"
|
|
2
|
+
import { Pressable, StyleSheet, View, useWindowDimensions, type StyleProp, type ViewStyle } from "react-native"
|
|
3
|
+
import { RowEndContext, RowHoldContext, rowHold } from "../../internal/row"
|
|
4
|
+
import { withAlpha } from "../../theme/material"
|
|
5
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
6
|
+
import { PickerDialog } from "../Dialog/Dialog"
|
|
7
|
+
import { MaterialCalendarToday, MaterialSchedule } from "../Icon/material"
|
|
8
|
+
import { FieldOutline, fieldLook } from "../Input/Input"
|
|
9
|
+
import { Popover } from "../Popover/Popover"
|
|
10
|
+
import { Text } from "../Text/Text"
|
|
11
|
+
import { Calendar, IOS_CALENDAR_INSET } from "./Calendar"
|
|
12
|
+
import {
|
|
13
|
+
clampDate,
|
|
14
|
+
dateOrder,
|
|
15
|
+
formatDate,
|
|
16
|
+
formatHeadline,
|
|
17
|
+
formatTime,
|
|
18
|
+
formatWheelDay,
|
|
19
|
+
localeHour12,
|
|
20
|
+
localeWeekStart,
|
|
21
|
+
monthNames,
|
|
22
|
+
pad2,
|
|
23
|
+
sameDay,
|
|
24
|
+
periodLabels,
|
|
25
|
+
startOfDay,
|
|
26
|
+
timeOutside,
|
|
27
|
+
withDay,
|
|
28
|
+
withTime,
|
|
29
|
+
} from "./dates"
|
|
30
|
+
import { TimeDial } from "./TimeDial"
|
|
31
|
+
import { Wheels, type WheelColumn } from "./Wheel"
|
|
32
|
+
|
|
33
|
+
export type DatePickerMode = "date" | "time" | "datetime"
|
|
34
|
+
export type DatePickerDisplay = "compact" | "inline" | "wheels"
|
|
35
|
+
|
|
36
|
+
export type DatePickerProps = {
|
|
37
|
+
/** The date and time chosen, `null` for none yet. */
|
|
38
|
+
value: Date | null
|
|
39
|
+
/** Called with the new date (its time kept when only the day changes, its day when only the time does). */
|
|
40
|
+
onChange: (value: Date) => void
|
|
41
|
+
/** What is chosen: a day, a time, or both. @default "date" */
|
|
42
|
+
mode?: DatePickerMode
|
|
43
|
+
/**
|
|
44
|
+
* How it is drawn. `compact`: the value in a capsule on iOS that opens the calendar (or the time's wheels) in a
|
|
45
|
+
* popover, Android's field or row value that opens Material's date (or time) dialog. `inline`: the month's calendar
|
|
46
|
+
* in place. `wheels`: the wheels of iOS, Android's spinners. @default "compact"
|
|
47
|
+
*/
|
|
48
|
+
display?: DatePickerDisplay
|
|
49
|
+
/** The earliest date that can be chosen. */
|
|
50
|
+
min?: Date
|
|
51
|
+
/** The latest date that can be chosen. */
|
|
52
|
+
max?: Date
|
|
53
|
+
/** The locale of the names and the formats. @default the device's */
|
|
54
|
+
locale?: string
|
|
55
|
+
/** The first day of the week, 0 Sunday to 6 Saturday. @default the locale's */
|
|
56
|
+
weekStartsOn?: number
|
|
57
|
+
/** A 12-hour clock with AM and PM. @default the locale's */
|
|
58
|
+
hour12?: boolean
|
|
59
|
+
/** Shown while there is no value. @default "Select date" / "Select time" */
|
|
60
|
+
placeholder?: string
|
|
61
|
+
disabled?: boolean
|
|
62
|
+
/** The picker's name for a screen reader (a row's label). */
|
|
63
|
+
"aria-label"?: string
|
|
64
|
+
/** Android: the header of the date dialog. @default "Select date" */
|
|
65
|
+
dateTitle?: string
|
|
66
|
+
/** Android: the header of the time dialog. @default "Select time" */
|
|
67
|
+
timeTitle?: string
|
|
68
|
+
/** @default "Cancel" */
|
|
69
|
+
cancelLabel?: string
|
|
70
|
+
/** @default "OK" */
|
|
71
|
+
okLabel?: string
|
|
72
|
+
/** The label of the time row under an inline calendar (`mode="datetime"`). @default "Time" */
|
|
73
|
+
timeLabel?: string
|
|
74
|
+
testID?: string
|
|
75
|
+
style?: StyleProp<ViewStyle>
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type Shared = {
|
|
79
|
+
value: Date | null
|
|
80
|
+
set: (next: Date) => void
|
|
81
|
+
min?: Date
|
|
82
|
+
max?: Date
|
|
83
|
+
locale?: string
|
|
84
|
+
weekStartsOn: number
|
|
85
|
+
hour12: boolean
|
|
86
|
+
disabled: boolean
|
|
87
|
+
ariaLabel?: string
|
|
88
|
+
placeholder: { date: string; time: string }
|
|
89
|
+
dateTitle: string
|
|
90
|
+
timeTitle: string
|
|
91
|
+
cancelLabel: string
|
|
92
|
+
okLabel: string
|
|
93
|
+
testID?: string
|
|
94
|
+
/** One of the parts' pickers is open (or just closed): a press that reaches the row from inside it opens no other. */
|
|
95
|
+
busy: { current: boolean }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A date and time picker. On iOS UIDatePicker of iOS 27.2: `compact`, the date and the time in two capsules 8 apart
|
|
100
|
+
* (34 tall, 12 in, the secondary soft fill, the text in the accent while open) that open the month's calendar and the
|
|
101
|
+
* time's wheels in the glass popover, at most 32 from the screen's edge; `inline`, the calendar in place (a Time row
|
|
102
|
+
* under it for both); `wheels`, the wheels. On Android Material 3's pickers: the value as a row's value in the accent
|
|
103
|
+
* (or an outlined field with its icon) that opens the date picker dialog (the headline date over the month) or the
|
|
104
|
+
* time picker dial, both applied with OK; `inline` the calendar in place, `wheels` Android's spinners. The value is a
|
|
105
|
+
* plain `Date`; `min` and `max` bound it.
|
|
106
|
+
*/
|
|
107
|
+
export function DatePicker({
|
|
108
|
+
value,
|
|
109
|
+
onChange,
|
|
110
|
+
mode = "date",
|
|
111
|
+
display = "compact",
|
|
112
|
+
min,
|
|
113
|
+
max,
|
|
114
|
+
locale,
|
|
115
|
+
weekStartsOn,
|
|
116
|
+
hour12,
|
|
117
|
+
placeholder,
|
|
118
|
+
disabled = false,
|
|
119
|
+
"aria-label": ariaLabel,
|
|
120
|
+
dateTitle = "Select date",
|
|
121
|
+
timeTitle = "Select time",
|
|
122
|
+
cancelLabel = "Cancel",
|
|
123
|
+
okLabel = "OK",
|
|
124
|
+
timeLabel = "Time",
|
|
125
|
+
testID,
|
|
126
|
+
style,
|
|
127
|
+
}: DatePickerProps) {
|
|
128
|
+
const theme = useTheme()
|
|
129
|
+
const busy = useRef(false)
|
|
130
|
+
const shared: Shared = {
|
|
131
|
+
busy,
|
|
132
|
+
value,
|
|
133
|
+
set: (next) => onChange(clampDate(next, min, max)),
|
|
134
|
+
min,
|
|
135
|
+
max,
|
|
136
|
+
locale,
|
|
137
|
+
weekStartsOn: weekStartsOn ?? localeWeekStart(locale),
|
|
138
|
+
hour12: hour12 ?? localeHour12(locale),
|
|
139
|
+
disabled,
|
|
140
|
+
ariaLabel,
|
|
141
|
+
placeholder: { date: placeholder ?? "Select date", time: placeholder ?? "Select time" },
|
|
142
|
+
dateTitle,
|
|
143
|
+
timeTitle,
|
|
144
|
+
cancelLabel,
|
|
145
|
+
okLabel,
|
|
146
|
+
testID,
|
|
147
|
+
}
|
|
148
|
+
if (display === "wheels" || (display === "inline" && mode === "time")) {
|
|
149
|
+
return (
|
|
150
|
+
<View style={style} pointerEvents={disabled ? "none" : "auto"}>
|
|
151
|
+
<View style={{ opacity: disabled ? 0.4 : 1 }}>
|
|
152
|
+
<Wheels columns={wheelColumns(shared, mode)} height={theme.isAndroid ? undefined : 216} bandInset={8} disabled={disabled} testID={testID} />
|
|
153
|
+
</View>
|
|
154
|
+
</View>
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
if (display === "inline") return <InlinePicker shared={shared} mode={mode} timeLabel={timeLabel} style={style} />
|
|
158
|
+
return <Compact shared={shared} mode={mode} style={style} />
|
|
159
|
+
}
|
|
160
|
+
DatePicker.displayName = "DatePicker"
|
|
161
|
+
|
|
162
|
+
/** The compact parts, at a row's end: they take the room after the label (two capsules pass its 60%), set at its end. */
|
|
163
|
+
function Compact({ shared, mode, style }: { shared: Shared; mode: DatePickerMode; style?: StyleProp<ViewStyle> }) {
|
|
164
|
+
const row = useContext(RowEndContext)
|
|
165
|
+
const fill = row?.fill
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
if (!fill) return
|
|
168
|
+
fill(true)
|
|
169
|
+
return () => fill(false)
|
|
170
|
+
}, [fill])
|
|
171
|
+
return (
|
|
172
|
+
<View style={[styles.parts, row ? styles.atEnd : null, style]} testID={shared.testID}>
|
|
173
|
+
{mode !== "time" ? <Part shared={shared} kind="date" first /> : null}
|
|
174
|
+
{mode !== "date" ? <Part shared={shared} kind="time" first={mode === "time"} /> : null}
|
|
175
|
+
</View>
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The wheels, as UIDatePicker's `.wheels` (iOS 27.2, en_US, 402pt): a date's month, day and year in the locale's order,
|
|
181
|
+
* their centres 104 and 77.5 apart (the month 157 wide, the day 50, the year 105); a time's hour and minute 63.5 apart,
|
|
182
|
+
* AM / PM from 49 after the minute's centre; with both, one column of days ("Wed Nov 25", "Today") before the time.
|
|
183
|
+
*/
|
|
184
|
+
function wheelColumns(shared: Shared, mode: DatePickerMode): WheelColumn[] {
|
|
185
|
+
const { value, set, min, max, locale } = shared
|
|
186
|
+
const base = value ?? clampDate(new Date(), min, max)
|
|
187
|
+
const columns: WheelColumn[] = []
|
|
188
|
+
if (mode === "datetime") columns.push(dayColumn(shared, base))
|
|
189
|
+
else if (mode === "date") {
|
|
190
|
+
const names = monthNames(locale)
|
|
191
|
+
// The years stand still around today (not the value), so turning the wheel never moves its rows
|
|
192
|
+
const thisYear = new Date().getFullYear()
|
|
193
|
+
const yearFrom = Math.min(min?.getFullYear() ?? thisYear - 100, base.getFullYear())
|
|
194
|
+
const yearTo = Math.max(max?.getFullYear() ?? thisYear + 50, base.getFullYear())
|
|
195
|
+
const days = new Date(base.getFullYear(), base.getMonth() + 1, 0).getDate()
|
|
196
|
+
// A day that would fall out of the month (31 into February) is the month's last
|
|
197
|
+
const on = (year: number, month: number, day: number) => {
|
|
198
|
+
const last = new Date(year, month + 1, 0).getDate()
|
|
199
|
+
return new Date(year, month, Math.min(day, last), base.getHours(), base.getMinutes())
|
|
200
|
+
}
|
|
201
|
+
const dayOut = (date: Date) => (min != null && startOfDay(date) < startOfDay(min)) || (max != null && startOfDay(date) > startOfDay(max))
|
|
202
|
+
const byPart = {
|
|
203
|
+
month: {
|
|
204
|
+
key: "month",
|
|
205
|
+
label: "Month",
|
|
206
|
+
width: 157,
|
|
207
|
+
items: names.map((label, month) => ({
|
|
208
|
+
label,
|
|
209
|
+
disabled: (min != null && new Date(base.getFullYear(), month + 1, 0) < startOfDay(min)) || (max != null && new Date(base.getFullYear(), month, 1) > startOfDay(max)),
|
|
210
|
+
})),
|
|
211
|
+
index: base.getMonth(),
|
|
212
|
+
onChange: (month: number) => set(on(base.getFullYear(), month, base.getDate())),
|
|
213
|
+
},
|
|
214
|
+
day: {
|
|
215
|
+
key: "day",
|
|
216
|
+
label: "Day",
|
|
217
|
+
width: 50,
|
|
218
|
+
items: Array.from({ length: days }, (_, index) => ({ label: String(index + 1), disabled: dayOut(on(base.getFullYear(), base.getMonth(), index + 1)) })),
|
|
219
|
+
index: base.getDate() - 1,
|
|
220
|
+
onChange: (index: number) => set(on(base.getFullYear(), base.getMonth(), index + 1)),
|
|
221
|
+
},
|
|
222
|
+
year: {
|
|
223
|
+
key: "year",
|
|
224
|
+
label: "Year",
|
|
225
|
+
width: 105,
|
|
226
|
+
items: Array.from({ length: yearTo - yearFrom + 1 }, (_, index) => ({ label: String(yearFrom + index) })),
|
|
227
|
+
index: base.getFullYear() - yearFrom,
|
|
228
|
+
onChange: (index: number) => set(on(yearFrom + index, base.getMonth(), base.getDate())),
|
|
229
|
+
},
|
|
230
|
+
}
|
|
231
|
+
for (const part of dateOrder(locale)) columns.push(byPart[part])
|
|
232
|
+
}
|
|
233
|
+
if (mode !== "date") columns.push(...timeColumns(shared, base, set))
|
|
234
|
+
return columns
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** The days of a date-and-time wheel: a year each side of today (or `min` to `max`), "Today" for today. */
|
|
238
|
+
function dayColumn(shared: Shared, base: Date): WheelColumn {
|
|
239
|
+
const { min, max, locale, set } = shared
|
|
240
|
+
const today = startOfDay(new Date())
|
|
241
|
+
const from = min ? startOfDay(min) : new Date(today.getFullYear() - 1, today.getMonth(), today.getDate())
|
|
242
|
+
const to = max ? startOfDay(max) : new Date(today.getFullYear() + 1, today.getMonth(), today.getDate())
|
|
243
|
+
const first = startOfDay(base) < from ? startOfDay(base) : from
|
|
244
|
+
const last = startOfDay(base) > to ? startOfDay(base) : to
|
|
245
|
+
const days: Date[] = []
|
|
246
|
+
for (let day = first; day <= last; day = new Date(day.getFullYear(), day.getMonth(), day.getDate() + 1)) days.push(day)
|
|
247
|
+
return {
|
|
248
|
+
key: "day",
|
|
249
|
+
label: "Day",
|
|
250
|
+
width: 130,
|
|
251
|
+
align: "end",
|
|
252
|
+
items: days.map((day) => ({ label: sameDay(day, today) ? "Today" : formatWheelDay(day, locale) })),
|
|
253
|
+
index: Math.max(0, days.findIndex((day) => sameDay(day, base))),
|
|
254
|
+
onChange: (index) => set(withDay(base, days[index])),
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function timeColumns(shared: Shared, base: Date, set: (next: Date) => void): WheelColumn[] {
|
|
259
|
+
const { min, max, locale, hour12 } = shared
|
|
260
|
+
const hours = base.getHours()
|
|
261
|
+
const minutes = base.getMinutes()
|
|
262
|
+
// An hour can be chosen when any of its minutes lies in range
|
|
263
|
+
const hourOut = (hour: number) => (min != null && withTime(base, hour, 59) < min) || (max != null && withTime(base, hour, 0) > max)
|
|
264
|
+
const columns: WheelColumn[] = [
|
|
265
|
+
hour12
|
|
266
|
+
? {
|
|
267
|
+
key: "hours",
|
|
268
|
+
label: "Hours",
|
|
269
|
+
width: 64,
|
|
270
|
+
items: Array.from({ length: 12 }, (_, index) => ({ label: String(index === 0 ? 12 : index), disabled: hourOut(index + (hours >= 12 ? 12 : 0)) })),
|
|
271
|
+
index: hours % 12,
|
|
272
|
+
onChange: (index) => set(withTime(base, index + (hours >= 12 ? 12 : 0), minutes)),
|
|
273
|
+
}
|
|
274
|
+
: {
|
|
275
|
+
key: "hours",
|
|
276
|
+
label: "Hours",
|
|
277
|
+
width: 64,
|
|
278
|
+
items: Array.from({ length: 24 }, (_, hour) => ({ label: pad2(hour), disabled: hourOut(hour) })),
|
|
279
|
+
index: hours,
|
|
280
|
+
onChange: (hour) => set(withTime(base, hour, minutes)),
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
key: "minutes",
|
|
284
|
+
label: "Minutes",
|
|
285
|
+
width: 64,
|
|
286
|
+
items: Array.from({ length: 60 }, (_, minute) => ({ label: pad2(minute), disabled: timeOutside(withTime(base, hours, minute), min, max) })),
|
|
287
|
+
index: minutes,
|
|
288
|
+
onChange: (minute) => set(withTime(base, hours, minute)),
|
|
289
|
+
},
|
|
290
|
+
]
|
|
291
|
+
if (hour12) {
|
|
292
|
+
const [am, pm] = periodLabels(locale)
|
|
293
|
+
// AM / PM from 49 after the minutes' centre: 17 into its column
|
|
294
|
+
columns.push({
|
|
295
|
+
key: "period",
|
|
296
|
+
label: "AM/PM",
|
|
297
|
+
width: 64,
|
|
298
|
+
align: "start",
|
|
299
|
+
inset: 17,
|
|
300
|
+
items: [
|
|
301
|
+
{ label: am, disabled: hourOut(hours % 12) && [...Array(12).keys()].every((hour) => hourOut(hour)) },
|
|
302
|
+
{ label: pm, disabled: [...Array(12).keys()].every((hour) => hourOut(hour + 12)) },
|
|
303
|
+
],
|
|
304
|
+
index: hours >= 12 ? 1 : 0,
|
|
305
|
+
onChange: (index) => set(withTime(base, (hours % 12) + (index ? 12 : 0), minutes)),
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
return columns
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** One part of a compact picker: the date or the time. */
|
|
312
|
+
function Part({ shared, kind, first }: { shared: Shared; kind: "date" | "time"; first: boolean }) {
|
|
313
|
+
const theme = useTheme()
|
|
314
|
+
const row = useContext(RowEndContext)
|
|
315
|
+
const hold = useContext(RowHoldContext)
|
|
316
|
+
const [open, setOwnOpen] = useState(false)
|
|
317
|
+
// Presses inside a picker's dialog or popover reach the row in React's tree: while one is open, and for the press
|
|
318
|
+
// that closes it, the row opens nothing
|
|
319
|
+
const setOpen = (next: boolean) => {
|
|
320
|
+
if (next) {
|
|
321
|
+
if (shared.busy.current) return
|
|
322
|
+
shared.busy.current = true
|
|
323
|
+
} else setTimeout(() => (shared.busy.current = false), 0)
|
|
324
|
+
setOwnOpen(next)
|
|
325
|
+
}
|
|
326
|
+
const screen = useWindowDimensions().width
|
|
327
|
+
const { value, disabled, locale, hour12, ariaLabel, testID } = shared
|
|
328
|
+
if (disabled && open) {
|
|
329
|
+
shared.busy.current = false
|
|
330
|
+
setOwnOpen(false)
|
|
331
|
+
}
|
|
332
|
+
const text = value == null ? null : kind === "date" ? formatDate(value, locale) : formatTime(value, locale, hour12)
|
|
333
|
+
const shown = text ?? shared.placeholder[kind]
|
|
334
|
+
const partID = testID ? `${testID}-${kind}` : undefined
|
|
335
|
+
const a11y = {
|
|
336
|
+
accessibilityRole: "button" as const,
|
|
337
|
+
accessibilityLabel: [ariaLabel, shown].filter(Boolean).join(", "),
|
|
338
|
+
accessibilityState: { disabled, expanded: open },
|
|
339
|
+
}
|
|
340
|
+
if (theme.isAndroid) {
|
|
341
|
+
return <AndroidPart shared={shared} kind={kind} first={first} open={open} setOpen={setOpen} shown={shown} filled={text != null} a11y={a11y} partID={partID} />
|
|
342
|
+
}
|
|
343
|
+
// iOS: the capsule, and the popover it opens
|
|
344
|
+
const calendarWidth = 7 * 44 + 2 * IOS_CALENDAR_INSET
|
|
345
|
+
// At most 32 from the screen's edge, as the compact picker's popover stands (less on a narrow screen)
|
|
346
|
+
const edge = Math.max(8, Math.min(32, (screen - calendarWidth) / 2))
|
|
347
|
+
// The popover's wheels: a 24-hour time 75 apart (iOS 27.2's compact time popover), a 12-hour one as the wheels
|
|
348
|
+
const columns = kind === "time" ? timeColumns(shared, value ?? clampDate(new Date(), shared.min, shared.max), shared.set).map((column) => (hour12 ? column : { ...column, width: 75 })) : []
|
|
349
|
+
const wheelsWidth = Math.max(233, 40 + columns.reduce((sum, column) => sum + column.width, 0))
|
|
350
|
+
return (
|
|
351
|
+
<Popover open={open} onOpenChange={(next) => setOpen(next && !disabled)}>
|
|
352
|
+
<Popover.Trigger>
|
|
353
|
+
<Pressable
|
|
354
|
+
{...a11y}
|
|
355
|
+
disabled={disabled}
|
|
356
|
+
{...rowHold(hold)}
|
|
357
|
+
testID={partID}
|
|
358
|
+
style={({ pressed }) => [
|
|
359
|
+
styles.capsule,
|
|
360
|
+
disabled
|
|
361
|
+
? null
|
|
362
|
+
: { backgroundColor: theme.color("--color-background-secondary-soft-alpha", withAlpha(theme.colors.text, 0.07)), opacity: pressed ? 0.6 : 1 },
|
|
363
|
+
]}
|
|
364
|
+
>
|
|
365
|
+
<Text
|
|
366
|
+
numberOfLines={1}
|
|
367
|
+
color={disabled || text == null ? theme.colors.labelTertiary : open ? theme.colors.textAccent : theme.colors.text}
|
|
368
|
+
style={styles.capsuleText}
|
|
369
|
+
tabularNums={kind === "time"}
|
|
370
|
+
>
|
|
371
|
+
{shown}
|
|
372
|
+
</Text>
|
|
373
|
+
</Pressable>
|
|
374
|
+
</Popover.Trigger>
|
|
375
|
+
<Popover.Content
|
|
376
|
+
width={kind === "date" ? calendarWidth : wheelsWidth}
|
|
377
|
+
minWidth="auto"
|
|
378
|
+
align={row ? "end" : "center"}
|
|
379
|
+
alignOffset={0}
|
|
380
|
+
collisionPadding={{ left: edge, right: edge, top: 20, bottom: 20 }}
|
|
381
|
+
aria-label={kind === "date" ? shared.dateTitle : shared.timeTitle}
|
|
382
|
+
testID={partID ? `${partID}-popover` : undefined}
|
|
383
|
+
style={kind === "date" ? { padding: IOS_CALENDAR_INSET } : { padding: 0 }}
|
|
384
|
+
>
|
|
385
|
+
{kind === "date" ? (
|
|
386
|
+
<Calendar
|
|
387
|
+
value={value}
|
|
388
|
+
onSelect={(day) => {
|
|
389
|
+
shared.set(withDay(value, day))
|
|
390
|
+
setOpen(false)
|
|
391
|
+
}}
|
|
392
|
+
min={shared.min}
|
|
393
|
+
max={shared.max}
|
|
394
|
+
locale={locale}
|
|
395
|
+
weekStartsOn={shared.weekStartsOn}
|
|
396
|
+
width={7 * 44}
|
|
397
|
+
testID={partID ? `${partID}-calendar` : undefined}
|
|
398
|
+
/>
|
|
399
|
+
) : (
|
|
400
|
+
<Wheels columns={columns} testID={partID ? `${partID}-wheels` : undefined} />
|
|
401
|
+
)}
|
|
402
|
+
</Popover.Content>
|
|
403
|
+
</Popover>
|
|
404
|
+
)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
type AndroidPartProps = {
|
|
408
|
+
shared: Shared
|
|
409
|
+
kind: "date" | "time"
|
|
410
|
+
first: boolean
|
|
411
|
+
open: boolean
|
|
412
|
+
setOpen: (open: boolean) => void
|
|
413
|
+
shown: string
|
|
414
|
+
filled: boolean
|
|
415
|
+
a11y: Record<string, unknown>
|
|
416
|
+
partID?: string
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Android: a row's value in the accent, or an outlined field with its icon; either opens Material's dialog. */
|
|
420
|
+
function AndroidPart({ shared, kind, first, open, setOpen, shown, filled, a11y, partID }: AndroidPartProps) {
|
|
421
|
+
const theme = useTheme()
|
|
422
|
+
const m = theme.material
|
|
423
|
+
const row = useContext(RowEndContext)
|
|
424
|
+
const hold = useContext(RowHoldContext)
|
|
425
|
+
const { disabled } = shared
|
|
426
|
+
// In a row the first part takes the row's press, as an Android value row opens its picker from anywhere on it
|
|
427
|
+
const handRow = row != null && first && !disabled ? row.press : undefined
|
|
428
|
+
useEffect(() => {
|
|
429
|
+
if (!handRow) return
|
|
430
|
+
handRow(() => setOpen(true))
|
|
431
|
+
return () => handRow(null)
|
|
432
|
+
}, [handRow, setOpen])
|
|
433
|
+
const look = fieldLook(theme, { variant: "outline", size: "md", pill: false }, { focused: open, filled, disabled, invalid: false })
|
|
434
|
+
const Icon = kind === "date" ? MaterialCalendarToday : MaterialSchedule
|
|
435
|
+
return (
|
|
436
|
+
<>
|
|
437
|
+
{row != null ? (
|
|
438
|
+
<Pressable {...a11y} disabled={disabled} {...rowHold(hold)} onPress={() => setOpen(true)} hitSlop={ROW_SLOP} testID={partID}>
|
|
439
|
+
<Text numberOfLines={1} color={withAlpha(filled ? m.primary : withAlpha(m.onSurface, 0.35), disabled ? 0.5 : 1)} style={styles.androidValue} tabularNums={kind === "time"}>
|
|
440
|
+
{shown}
|
|
441
|
+
</Text>
|
|
442
|
+
</Pressable>
|
|
443
|
+
) : (
|
|
444
|
+
<Pressable {...a11y} disabled={disabled} onPress={() => setOpen(true)} testID={partID} style={[styles.field, look.box, { gap: look.gap, paddingEnd: 12 }]}>
|
|
445
|
+
<FieldOutline frame={look.frame} />
|
|
446
|
+
<Text numberOfLines={1} color={filled ? look.text.color : look.placeholder} style={[styles.grow, { fontSize: look.text.fontSize, lineHeight: look.text.lineHeight }]}>
|
|
447
|
+
{shown}
|
|
448
|
+
</Text>
|
|
449
|
+
<Icon size={24} color={look.trailingIcon} />
|
|
450
|
+
</Pressable>
|
|
451
|
+
)}
|
|
452
|
+
{kind === "date" ? <AndroidDateDialog shared={shared} open={open} onClose={() => setOpen(false)} partID={partID} /> : <AndroidTimeDialog shared={shared} open={open} onClose={() => setOpen(false)} partID={partID} />}
|
|
453
|
+
</>
|
|
454
|
+
)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** Material 3's date picker dialog: "Select date", the headline date, a line, then the month; OK applies the day. */
|
|
458
|
+
function AndroidDateDialog({ shared, open, onClose, partID }: { shared: Shared; open: boolean; onClose: () => void; partID?: string }) {
|
|
459
|
+
const theme = useTheme()
|
|
460
|
+
const m = theme.material
|
|
461
|
+
const { value, locale } = shared
|
|
462
|
+
const [draft, setDraft] = useState<Date | null>(value)
|
|
463
|
+
const [wasOpen, setWasOpen] = useState(open)
|
|
464
|
+
if (wasOpen !== open) {
|
|
465
|
+
setWasOpen(open)
|
|
466
|
+
if (open) setDraft(value)
|
|
467
|
+
}
|
|
468
|
+
const width = Math.min(useWindowDimensions().width - 48, 356)
|
|
469
|
+
return (
|
|
470
|
+
<PickerDialog
|
|
471
|
+
open={open}
|
|
472
|
+
onClose={onClose}
|
|
473
|
+
onOk={() => {
|
|
474
|
+
if (draft) shared.set(withDay(value, draft))
|
|
475
|
+
}}
|
|
476
|
+
cancelLabel={shared.cancelLabel}
|
|
477
|
+
okLabel={shared.okLabel}
|
|
478
|
+
label={shared.dateTitle}
|
|
479
|
+
testID={partID ? `${partID}-dialog` : undefined}
|
|
480
|
+
>
|
|
481
|
+
<View style={styles.androidHeader}>
|
|
482
|
+
<Text color={m.onSurfaceVariant} style={[styles.androidSupporting, theme.font("medium")]}>
|
|
483
|
+
{shared.dateTitle}
|
|
484
|
+
</Text>
|
|
485
|
+
<Text color={m.onSurfaceVariant} style={styles.androidHeadline} accessibilityLiveRegion="polite">
|
|
486
|
+
{draft ? formatHeadline(draft, locale) : shared.placeholder.date}
|
|
487
|
+
</Text>
|
|
488
|
+
</View>
|
|
489
|
+
<View style={{ height: 1, backgroundColor: m.outlineVariant }} />
|
|
490
|
+
<View style={styles.androidCalendar}>
|
|
491
|
+
<Calendar
|
|
492
|
+
value={draft}
|
|
493
|
+
onSelect={setDraft}
|
|
494
|
+
min={shared.min}
|
|
495
|
+
max={shared.max}
|
|
496
|
+
locale={locale}
|
|
497
|
+
weekStartsOn={shared.weekStartsOn}
|
|
498
|
+
width={width - 24}
|
|
499
|
+
testID={partID ? `${partID}-calendar` : undefined}
|
|
500
|
+
/>
|
|
501
|
+
</View>
|
|
502
|
+
</PickerDialog>
|
|
503
|
+
)
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** Material 3's time picker dialog: the dial; OK applies the time. */
|
|
507
|
+
function AndroidTimeDialog({ shared, open, onClose, partID }: { shared: Shared; open: boolean; onClose: () => void; partID?: string }) {
|
|
508
|
+
const { value } = shared
|
|
509
|
+
const now = value ?? new Date()
|
|
510
|
+
const [draft, setDraft] = useState({ hours: now.getHours(), minutes: now.getMinutes() })
|
|
511
|
+
const [wasOpen, setWasOpen] = useState(open)
|
|
512
|
+
if (wasOpen !== open) {
|
|
513
|
+
setWasOpen(open)
|
|
514
|
+
if (open) setDraft({ hours: now.getHours(), minutes: now.getMinutes() })
|
|
515
|
+
}
|
|
516
|
+
return (
|
|
517
|
+
<PickerDialog
|
|
518
|
+
open={open}
|
|
519
|
+
onClose={onClose}
|
|
520
|
+
onOk={() => shared.set(withTime(value, draft.hours, draft.minutes))}
|
|
521
|
+
cancelLabel={shared.cancelLabel}
|
|
522
|
+
okLabel={shared.okLabel}
|
|
523
|
+
label={shared.timeTitle}
|
|
524
|
+
testID={partID ? `${partID}-dialog` : undefined}
|
|
525
|
+
>
|
|
526
|
+
<TimeDial
|
|
527
|
+
hours={draft.hours}
|
|
528
|
+
minutes={draft.minutes}
|
|
529
|
+
onChange={(hours, minutes) => setDraft({ hours, minutes })}
|
|
530
|
+
hour12={shared.hour12}
|
|
531
|
+
locale={shared.locale}
|
|
532
|
+
title={shared.timeTitle}
|
|
533
|
+
testID={partID ? `${partID}-dial` : undefined}
|
|
534
|
+
/>
|
|
535
|
+
</PickerDialog>
|
|
536
|
+
)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** The calendar in place; with the time, a Time row under it whose value opens the time's picker. */
|
|
540
|
+
function InlinePicker({ shared, mode, timeLabel, style }: { shared: Shared; mode: DatePickerMode; timeLabel: string; style?: StyleProp<ViewStyle> }) {
|
|
541
|
+
const theme = useTheme()
|
|
542
|
+
const [width, setWidth] = useState(0)
|
|
543
|
+
const { value, testID } = shared
|
|
544
|
+
const android = theme.isAndroid
|
|
545
|
+
const timeRow: ReactNode =
|
|
546
|
+
mode === "datetime" ? (
|
|
547
|
+
// iOS 27.2's inline picker with the time (dates-uikit-inline-time): no line over the row, its centre 51 under the
|
|
548
|
+
// last week's, the capsule 11 above the card's edge, the label Semibold; Android a row under a line
|
|
549
|
+
<View style={[styles.timeRow, android ? [styles.androidTimeRow, { borderTopColor: theme.material.outlineVariant }] : styles.iosTimeRow]}>
|
|
550
|
+
<Text style={android ? styles.androidRowLabel : [styles.rowLabel, theme.font("semibold")]} color={android ? theme.material.onSurface : theme.colors.text}>
|
|
551
|
+
{timeLabel}
|
|
552
|
+
</Text>
|
|
553
|
+
<RowEndContext.Provider value={{ fill: () => {} }}>
|
|
554
|
+
<Part shared={{ ...shared, ariaLabel: timeLabel }} kind="time" first={false} />
|
|
555
|
+
</RowEndContext.Provider>
|
|
556
|
+
</View>
|
|
557
|
+
) : null
|
|
558
|
+
return (
|
|
559
|
+
<View
|
|
560
|
+
style={[styles.inline, android ? null : styles.iosInline, mode === "datetime" && !android ? { paddingBottom: 8 } : null, style, { opacity: shared.disabled ? 0.4 : 1 }]}
|
|
561
|
+
pointerEvents={shared.disabled ? "none" : "auto"}
|
|
562
|
+
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
|
|
563
|
+
testID={testID}
|
|
564
|
+
>
|
|
565
|
+
{width > 0 ? (
|
|
566
|
+
<View style={android ? null : { paddingHorizontal: IOS_CALENDAR_INSET }}>
|
|
567
|
+
<Calendar
|
|
568
|
+
value={value}
|
|
569
|
+
onSelect={(day) => shared.set(withDay(value, day))}
|
|
570
|
+
disabled={shared.disabled}
|
|
571
|
+
min={shared.min}
|
|
572
|
+
max={shared.max}
|
|
573
|
+
locale={shared.locale}
|
|
574
|
+
weekStartsOn={shared.weekStartsOn}
|
|
575
|
+
width={android ? width : width - 2 * IOS_CALENDAR_INSET}
|
|
576
|
+
testID={testID ? `${testID}-calendar` : undefined}
|
|
577
|
+
/>
|
|
578
|
+
</View>
|
|
579
|
+
) : null}
|
|
580
|
+
{timeRow}
|
|
581
|
+
</View>
|
|
582
|
+
)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const ROW_SLOP = { top: 15, bottom: 15, left: 8, right: 8 }
|
|
586
|
+
|
|
587
|
+
const styles = StyleSheet.create({
|
|
588
|
+
parts: { flexDirection: "row", alignItems: "center", gap: 8, flexShrink: 1 },
|
|
589
|
+
atEnd: { flexGrow: 1, justifyContent: "flex-end" },
|
|
590
|
+
capsule: { height: 34, borderRadius: 17, paddingHorizontal: 12, justifyContent: "center" },
|
|
591
|
+
capsuleText: { fontSize: 17, lineHeight: 22 },
|
|
592
|
+
androidValue: { fontSize: 16, lineHeight: 20 },
|
|
593
|
+
field: { flexDirection: "row", alignItems: "center", flexGrow: 1, minWidth: 140 },
|
|
594
|
+
grow: { flex: 1, minWidth: 0 },
|
|
595
|
+
androidHeader: { paddingStart: 24, paddingEnd: 12, paddingTop: 16, paddingBottom: 12 },
|
|
596
|
+
androidSupporting: { fontSize: 14, lineHeight: 20, letterSpacing: 0.1 },
|
|
597
|
+
androidHeadline: { fontSize: 32, lineHeight: 40, marginTop: 36 },
|
|
598
|
+
androidCalendar: { paddingHorizontal: 12, paddingBottom: 8 },
|
|
599
|
+
inline: { alignSelf: "stretch" },
|
|
600
|
+
timeRow: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 16 },
|
|
601
|
+
iosTimeRow: { minHeight: 40, marginTop: 9 },
|
|
602
|
+
androidTimeRow: { minHeight: 52, marginTop: 8, borderTopWidth: StyleSheet.hairlineWidth },
|
|
603
|
+
// As iOS 27.2's inline picker in a cell: the month's title centred 34 under the top, the last week (or the Time row's
|
|
604
|
+
// capsule) 11 above the bottom
|
|
605
|
+
iosInline: { paddingTop: 12, paddingBottom: 11 },
|
|
606
|
+
rowLabel: { fontSize: 17, lineHeight: 22 },
|
|
607
|
+
androidRowLabel: { fontSize: 16, lineHeight: 24 },
|
|
608
|
+
})
|