@plexui/react-native 0.2.4 → 0.2.6
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/Badge/Badge.js +10 -0
- 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/components/PageControl/PageControl.js +49 -22
- package/dist/es/components/ScrollEdge/ScrollEdge.js +27 -0
- package/dist/es/components/TabBar/TabBar.js +16 -13
- package/dist/es/index.js +2 -0
- package/dist/es/internal/BarBackdrop.js +54 -0
- package/dist/types/components/Badge/Badge.d.ts +1 -1
- 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/components/PageControl/PageControl.d.ts +12 -1
- package/dist/types/components/ScrollEdge/ScrollEdge.d.ts +19 -0
- package/dist/types/components/TabBar/TabBar.d.ts +14 -4
- package/dist/types/index.d.ts +3 -1
- package/dist/types/internal/BarBackdrop.d.ts +9 -0
- package/package.json +1 -1
- package/src/components/Badge/Badge.tsx +9 -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/components/PageControl/PageControl.tsx +71 -24
- package/src/components/ScrollEdge/ScrollEdge.tsx +48 -0
- package/src/components/TabBar/TabBar.tsx +33 -15
- package/src/index.ts +3 -0
- package/src/internal/BarBackdrop.tsx +73 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { useRef, useState } from "react"
|
|
2
|
+
import { Platform, Pressable, StyleSheet, View, type GestureResponderEvent } from "react-native"
|
|
3
|
+
import Svg, { Circle, Line } from "react-native-svg"
|
|
4
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
5
|
+
import { Text } from "../Text/Text"
|
|
6
|
+
import { pad2, periodLabels } from "./dates"
|
|
7
|
+
|
|
8
|
+
export type TimeDialProps = {
|
|
9
|
+
hours: number
|
|
10
|
+
minutes: number
|
|
11
|
+
onChange: (hours: number, minutes: number) => void
|
|
12
|
+
hour12: boolean
|
|
13
|
+
locale?: string
|
|
14
|
+
/** The header over the time ("Select time"). */
|
|
15
|
+
title: string
|
|
16
|
+
hoursLabel?: string
|
|
17
|
+
minutesLabel?: string
|
|
18
|
+
testID?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const DIAL = 256
|
|
22
|
+
const OUTER = 101
|
|
23
|
+
const INNER = 69
|
|
24
|
+
const KNOB = 48
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Android's time picker dial (Material 3 TimePicker): "Select time" in Medium 12, the hour and the minute in Regular
|
|
28
|
+
* 57 boxes 96 by 80 (the one being set in the primary container, the other in the highest surface container), AM and
|
|
29
|
+
* PM stacked beside them on a 12-hour clock, and the 256dp dial: a 48dp knob in the primary color on a line from the
|
|
30
|
+
* middle, the numbers 101dp out (a 24-hour clock adds 00 and 13–23 69dp out). A tap or a drag on the dial sets the
|
|
31
|
+
* hour, then it turns to the minutes.
|
|
32
|
+
*/
|
|
33
|
+
export function TimeDial({ hours, minutes, onChange, hour12, locale, title, hoursLabel = "Hours", minutesLabel = "Minutes", testID }: TimeDialProps) {
|
|
34
|
+
const theme = useTheme()
|
|
35
|
+
const m = theme.material
|
|
36
|
+
const [part, setPart] = useState<"hours" | "minutes">("hours")
|
|
37
|
+
const pm = hours >= 12
|
|
38
|
+
const [am, pmLabel] = periodLabels(locale)
|
|
39
|
+
const shownHour = hour12 ? (hours % 12 === 0 ? 12 : hours % 12) : hours
|
|
40
|
+
const center = DIAL / 2
|
|
41
|
+
// The value under the knob: its angle from 12 o'clock and its ring
|
|
42
|
+
const inner = part === "hours" && !hour12 && (hours === 0 || hours > 12)
|
|
43
|
+
const angle = part === "hours" ? ((hours % 12) / 12) * 360 : (minutes / 60) * 360
|
|
44
|
+
const radius = inner ? INNER : OUTER
|
|
45
|
+
const knob = { x: center + radius * Math.sin((angle * Math.PI) / 180), y: center - radius * Math.cos((angle * Math.PI) / 180) }
|
|
46
|
+
const dragging = useRef(false)
|
|
47
|
+
const pick = (event: GestureResponderEvent, done: boolean) => {
|
|
48
|
+
const x = event.nativeEvent.locationX - center
|
|
49
|
+
const y = event.nativeEvent.locationY - center
|
|
50
|
+
let degrees = (Math.atan2(x, -y) * 180) / Math.PI
|
|
51
|
+
if (degrees < 0) degrees += 360
|
|
52
|
+
if (part === "hours") {
|
|
53
|
+
const slot = Math.round(degrees / 30) % 12
|
|
54
|
+
const ringInner = !hour12 && Math.hypot(x, y) < (OUTER + INNER) / 2
|
|
55
|
+
// Outer ring 12, 1…11; inner 00, 13…23 (Compose's 24-hour dial); a 12-hour clock keeps its half of the day
|
|
56
|
+
const next = hour12 ? (slot % 12) + (pm ? 12 : 0) : ringInner ? (slot === 0 ? 0 : slot + 12) : slot === 0 ? 12 : slot
|
|
57
|
+
if (next !== hours) onChange(next, minutes)
|
|
58
|
+
if (done) setPart("minutes")
|
|
59
|
+
} else {
|
|
60
|
+
const next = Math.round(degrees / 6) % 60
|
|
61
|
+
if (next !== minutes) onChange(hours, next)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const numbers =
|
|
65
|
+
part === "minutes"
|
|
66
|
+
? Array.from({ length: 12 }, (_, slot) => ({ label: pad2(slot * 5), slot, ring: OUTER, value: slot * 5 }))
|
|
67
|
+
: [
|
|
68
|
+
...Array.from({ length: 12 }, (_, slot) => ({ label: String(slot === 0 ? 12 : slot), slot, ring: OUTER, value: slot === 0 ? 12 : slot })),
|
|
69
|
+
...(hour12 ? [] : Array.from({ length: 12 }, (_, slot) => ({ label: slot === 0 ? "00" : String(slot + 12), slot, ring: INNER, value: slot === 0 ? 0 : slot + 12 }))),
|
|
70
|
+
]
|
|
71
|
+
const chosenValue = part === "hours" ? (hour12 ? shownHour : hours) : minutes
|
|
72
|
+
const box = (which: "hours" | "minutes") => {
|
|
73
|
+
const active = part === which
|
|
74
|
+
return (
|
|
75
|
+
<Pressable
|
|
76
|
+
accessibilityRole="button"
|
|
77
|
+
accessibilityLabel={`${which === "hours" ? hoursLabel : minutesLabel}, ${which === "hours" ? shownHour : pad2(minutes)}`}
|
|
78
|
+
accessibilityState={{ selected: active }}
|
|
79
|
+
onPress={() => setPart(which)}
|
|
80
|
+
testID={testID ? `${testID}-${which}` : undefined}
|
|
81
|
+
style={[styles.box, { backgroundColor: active ? m.primaryContainer : m.surfaceContainerHighest }]}
|
|
82
|
+
>
|
|
83
|
+
<Text color={active ? m.onPrimaryContainer : m.onSurface} style={styles.boxText} tabularNums>
|
|
84
|
+
{which === "hours" ? (hour12 ? String(shownHour) : pad2(hours)) : pad2(minutes)}
|
|
85
|
+
</Text>
|
|
86
|
+
</Pressable>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
const period = (afternoon: boolean) => {
|
|
90
|
+
const active = pm === afternoon
|
|
91
|
+
return (
|
|
92
|
+
<Pressable
|
|
93
|
+
accessibilityRole="button"
|
|
94
|
+
accessibilityState={{ selected: active }}
|
|
95
|
+
onPress={() => {
|
|
96
|
+
if (active) return
|
|
97
|
+
onChange(afternoon ? hours + 12 : hours - 12, minutes)
|
|
98
|
+
}}
|
|
99
|
+
style={[styles.period, active ? { backgroundColor: m.tertiaryContainer } : null]}
|
|
100
|
+
>
|
|
101
|
+
<Text color={active ? m.onTertiaryContainer : m.onSurfaceVariant} style={[styles.periodText, theme.font("medium")]}>
|
|
102
|
+
{afternoon ? pmLabel : am}
|
|
103
|
+
</Text>
|
|
104
|
+
</Pressable>
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
return (
|
|
108
|
+
<View style={styles.root} testID={testID}>
|
|
109
|
+
<Text color={m.onSurfaceVariant} style={[styles.title, theme.font("medium")]}>
|
|
110
|
+
{title}
|
|
111
|
+
</Text>
|
|
112
|
+
<View style={styles.display}>
|
|
113
|
+
{box("hours")}
|
|
114
|
+
<Text color={m.onSurface} style={styles.colon}>
|
|
115
|
+
:
|
|
116
|
+
</Text>
|
|
117
|
+
{box("minutes")}
|
|
118
|
+
{hour12 ? (
|
|
119
|
+
<View style={[styles.periods, { borderColor: m.outline }]}>
|
|
120
|
+
{period(false)}
|
|
121
|
+
<View style={{ height: 1, backgroundColor: m.outline }} />
|
|
122
|
+
{period(true)}
|
|
123
|
+
</View>
|
|
124
|
+
) : null}
|
|
125
|
+
</View>
|
|
126
|
+
<View
|
|
127
|
+
accessible
|
|
128
|
+
accessibilityRole="adjustable"
|
|
129
|
+
accessibilityLabel={part === "hours" ? hoursLabel : minutesLabel}
|
|
130
|
+
accessibilityValue={{ text: part === "hours" ? String(shownHour) : pad2(minutes) }}
|
|
131
|
+
accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
|
|
132
|
+
onAccessibilityAction={(event) => {
|
|
133
|
+
const up = event.nativeEvent.actionName === "increment" ? 1 : -1
|
|
134
|
+
if (part === "hours") onChange((hours + up + 24) % 24, minutes)
|
|
135
|
+
else onChange(hours, (minutes + up + 60) % 60)
|
|
136
|
+
}}
|
|
137
|
+
// The web's keyboard: the arrows step the hour or the minute, Enter moves on to the minutes
|
|
138
|
+
{...((Platform.OS === "web"
|
|
139
|
+
? {
|
|
140
|
+
tabIndex: 0,
|
|
141
|
+
onKeyDown: (event: { key: string; preventDefault: () => void }) => {
|
|
142
|
+
const up = event.key === "ArrowUp" || event.key === "ArrowRight" ? 1 : event.key === "ArrowDown" || event.key === "ArrowLeft" ? -1 : 0
|
|
143
|
+
if (event.key === "Enter" && part === "hours") setPart("minutes")
|
|
144
|
+
else if (!up) return
|
|
145
|
+
else if (part === "hours") onChange((hours + up + 24) % 24, minutes)
|
|
146
|
+
else onChange(hours, (minutes + up + 60) % 60)
|
|
147
|
+
event.preventDefault()
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
: null) as object)}
|
|
151
|
+
onStartShouldSetResponder={() => true}
|
|
152
|
+
onMoveShouldSetResponder={() => true}
|
|
153
|
+
onResponderGrant={(event) => {
|
|
154
|
+
dragging.current = true
|
|
155
|
+
pick(event, false)
|
|
156
|
+
}}
|
|
157
|
+
onResponderMove={(event) => pick(event, false)}
|
|
158
|
+
onResponderRelease={(event) => {
|
|
159
|
+
dragging.current = false
|
|
160
|
+
pick(event, true)
|
|
161
|
+
}}
|
|
162
|
+
testID={testID ? `${testID}-dial` : undefined}
|
|
163
|
+
style={[styles.dial, { backgroundColor: m.surfaceContainerHighest }]}
|
|
164
|
+
>
|
|
165
|
+
<Svg width={DIAL} height={DIAL} style={StyleSheet.absoluteFill} pointerEvents="none">
|
|
166
|
+
<Line x1={center} y1={center} x2={knob.x} y2={knob.y} stroke={m.primary} strokeWidth={2} />
|
|
167
|
+
<Circle cx={center} cy={center} r={4} fill={m.primary} />
|
|
168
|
+
<Circle cx={knob.x} cy={knob.y} r={KNOB / 2} fill={m.primary} />
|
|
169
|
+
</Svg>
|
|
170
|
+
{numbers.map(({ label, slot, ring, value }) => {
|
|
171
|
+
const x = center + ring * Math.sin((slot * 30 * Math.PI) / 180)
|
|
172
|
+
const y = center - ring * Math.cos((slot * 30 * Math.PI) / 180)
|
|
173
|
+
const chosen = value === chosenValue && ring === radius
|
|
174
|
+
return (
|
|
175
|
+
<View key={`${ring}-${slot}`} pointerEvents="none" style={[styles.number, { left: x - KNOB / 2, top: y - KNOB / 2 }]}>
|
|
176
|
+
<Text color={chosen ? m.onPrimary : m.onSurface} style={ring === INNER ? styles.innerText : styles.numberText}>
|
|
177
|
+
{label}
|
|
178
|
+
</Text>
|
|
179
|
+
</View>
|
|
180
|
+
)
|
|
181
|
+
})}
|
|
182
|
+
</View>
|
|
183
|
+
</View>
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const styles = StyleSheet.create({
|
|
188
|
+
root: { paddingHorizontal: 24, paddingTop: 24, alignItems: "center" },
|
|
189
|
+
title: { alignSelf: "flex-start", fontSize: 12, lineHeight: 16, letterSpacing: 0.5, marginBottom: 20 },
|
|
190
|
+
display: { flexDirection: "row", alignItems: "center", height: 80 },
|
|
191
|
+
box: { width: 96, height: 80, borderRadius: 8, alignItems: "center", justifyContent: "center" },
|
|
192
|
+
boxText: { fontSize: 57, lineHeight: 64 },
|
|
193
|
+
colon: { width: 24, textAlign: "center", fontSize: 57, lineHeight: 64 },
|
|
194
|
+
periods: { width: 52, height: 80, marginStart: 12, borderWidth: 1, borderRadius: 8, overflow: "hidden" },
|
|
195
|
+
period: { flex: 1, alignItems: "center", justifyContent: "center" },
|
|
196
|
+
periodText: { fontSize: 16, lineHeight: 24, letterSpacing: 0.15 },
|
|
197
|
+
dial: { width: DIAL, height: DIAL, borderRadius: DIAL / 2, marginTop: 36 },
|
|
198
|
+
number: { position: "absolute", width: KNOB, height: KNOB, alignItems: "center", justifyContent: "center" },
|
|
199
|
+
numberText: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5 },
|
|
200
|
+
innerText: { fontSize: 14, lineHeight: 20, letterSpacing: 0.25 },
|
|
201
|
+
})
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react"
|
|
2
|
+
import { Animated, Platform, Pressable, ScrollView, StyleSheet, View, type NativeScrollEvent, type NativeSyntheticEvent } from "react-native"
|
|
3
|
+
import { withAlpha } from "../../theme/material"
|
|
4
|
+
import { useTheme } from "../../theme/ThemeProvider"
|
|
5
|
+
|
|
6
|
+
export type WheelItem = { label: string; disabled?: boolean }
|
|
7
|
+
|
|
8
|
+
export type WheelColumn = {
|
|
9
|
+
key: string
|
|
10
|
+
items: WheelItem[]
|
|
11
|
+
/** The item at the band. */
|
|
12
|
+
index: number
|
|
13
|
+
onChange: (index: number) => void
|
|
14
|
+
/** The column's width. */
|
|
15
|
+
width: number
|
|
16
|
+
/** The column's name, read by a screen reader ("Hours"). */
|
|
17
|
+
label: string
|
|
18
|
+
/** Where its labels stand in the column. @default "center" */
|
|
19
|
+
align?: "start" | "center" | "end"
|
|
20
|
+
/** The labels' inset from the column's side they stand at. @default 4 */
|
|
21
|
+
inset?: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type WheelsProps = {
|
|
25
|
+
columns: WheelColumn[]
|
|
26
|
+
/** The wheels' height. @default 202 on iOS (a compact time picker's popover), 144 on Android */
|
|
27
|
+
height?: number
|
|
28
|
+
/** iOS: the band's inset from the wheels' sides. @default 20 */
|
|
29
|
+
bandInset?: number
|
|
30
|
+
/** Takes no turns, presses or keys. */
|
|
31
|
+
disabled?: boolean
|
|
32
|
+
testID?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** iOS: a row of UIDatePicker's wheels, 32 apart (the band of iOS 27.2, 32 tall). Android: a NumberPicker's 48dp rows. */
|
|
36
|
+
export const IOS_WHEEL_ROW = 32
|
|
37
|
+
export const ANDROID_WHEEL_ROW = 48
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The wheels of a time (or date) picker. On iOS UIDatePicker's `.wheels` as iOS 27.2 draws them in a compact time
|
|
41
|
+
* picker's popover: rows 32 apart in Regular 22, a 32 band of the ink at 7% across the columns 20 in from the sides,
|
|
42
|
+
* the rows on the band in the ink and the others in its 35% gray, turning away like a cylinder's. On Android the
|
|
43
|
+
* spinner mode of its DatePicker and TimePicker: three 48dp rows, the middle one between two lines in the ink, the
|
|
44
|
+
* others faded. A wheel stops on a row; a tap on a row turns it there; VoiceOver and TalkBack turn it one row at a time.
|
|
45
|
+
*/
|
|
46
|
+
export function Wheels({ columns, height: heightProp, bandInset = 20, disabled = false, testID }: WheelsProps) {
|
|
47
|
+
const theme = useTheme()
|
|
48
|
+
const android = theme.isAndroid
|
|
49
|
+
const row = android ? ANDROID_WHEEL_ROW : IOS_WHEEL_ROW
|
|
50
|
+
const height = heightProp ?? (android ? row * 3 : 202)
|
|
51
|
+
const ink = android ? theme.material.onSurface : theme.colors.text
|
|
52
|
+
const band = (height - row) / 2
|
|
53
|
+
// Columns wider than the room narrow in step, so the wheels keep inside the band
|
|
54
|
+
const [room, setRoom] = useState(0)
|
|
55
|
+
const total = columns.reduce((sum, column) => sum + column.width, 0)
|
|
56
|
+
const fit = room > 0 ? Math.min(1, (room - 2 * (android ? 0 : bandInset)) / total) : 1
|
|
57
|
+
return (
|
|
58
|
+
<View style={[styles.wheels, { height }]} testID={testID} onLayout={(event) => setRoom(event.nativeEvent.layout.width)}>
|
|
59
|
+
{/* The band: the ink at 7% (12% at night) on iOS, two lines on Android */}
|
|
60
|
+
<View
|
|
61
|
+
pointerEvents="none"
|
|
62
|
+
style={[
|
|
63
|
+
styles.band,
|
|
64
|
+
{ top: band, height: row },
|
|
65
|
+
android
|
|
66
|
+
? { borderTopWidth: 2, borderBottomWidth: 2, borderColor: theme.material.outline, left: 0, right: 0 }
|
|
67
|
+
: { left: bandInset, right: bandInset, borderRadius: 8, backgroundColor: withAlpha(ink, theme.scheme === "dark" ? 0.12 : 0.07) },
|
|
68
|
+
]}
|
|
69
|
+
/>
|
|
70
|
+
{columns.map((column) => (
|
|
71
|
+
<Wheel
|
|
72
|
+
key={column.key}
|
|
73
|
+
column={fit < 1 ? { ...column, width: Math.floor(column.width * fit) } : column}
|
|
74
|
+
disabled={disabled}
|
|
75
|
+
row={row}
|
|
76
|
+
height={height}
|
|
77
|
+
ink={ink} testID={testID ? `${testID}-${column.key}` : undefined} />
|
|
78
|
+
))}
|
|
79
|
+
</View>
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function Wheel({ column, row, height, ink, disabled, testID }: { column: WheelColumn; row: number; height: number; ink: string; disabled: boolean; testID?: string }) {
|
|
84
|
+
const theme = useTheme()
|
|
85
|
+
const android = theme.isAndroid
|
|
86
|
+
const { items, index, onChange, width, label, align = "center", inset = 4 } = column
|
|
87
|
+
const scroll = useRef<{ scrollTo: (options: { y: number; animated?: boolean }) => void } | null>(null)
|
|
88
|
+
const [offset] = useState(() => new Animated.Value(index * row))
|
|
89
|
+
const last = useRef(index * row)
|
|
90
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
91
|
+
const touching = useRef(false)
|
|
92
|
+
const pad = (height - row) / 2
|
|
93
|
+
const web = Platform.OS === "web"
|
|
94
|
+
const nearestEnabled = (target: number) => {
|
|
95
|
+
const clamped = Math.max(0, Math.min(items.length - 1, target))
|
|
96
|
+
if (!items[clamped]?.disabled) return clamped
|
|
97
|
+
for (let step = 1; step < items.length; step += 1) {
|
|
98
|
+
if (!items[clamped - step]?.disabled && clamped - step >= 0) return clamped - step
|
|
99
|
+
if (!items[clamped + step]?.disabled && clamped + step < items.length) return clamped + step
|
|
100
|
+
}
|
|
101
|
+
return clamped
|
|
102
|
+
}
|
|
103
|
+
const turnTo = (next: number, animated: boolean) => {
|
|
104
|
+
scroll.current?.scrollTo({ y: next * row, animated: animated && !theme.reducedMotion })
|
|
105
|
+
}
|
|
106
|
+
// The wheel comes to rest on the row nearest the band, or the nearest one that can be chosen
|
|
107
|
+
const settle = () => {
|
|
108
|
+
timer.current = null
|
|
109
|
+
if (touching.current) return
|
|
110
|
+
const next = nearestEnabled(Math.round(last.current / row))
|
|
111
|
+
if (Math.abs(last.current - next * row) > 0.5) turnTo(next, true)
|
|
112
|
+
if (next !== index) onChange(next)
|
|
113
|
+
}
|
|
114
|
+
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
115
|
+
const y = event.nativeEvent.contentOffset.y
|
|
116
|
+
last.current = y
|
|
117
|
+
offset.setValue(y)
|
|
118
|
+
if (timer.current) clearTimeout(timer.current)
|
|
119
|
+
timer.current = setTimeout(settle, web ? 120 : 90)
|
|
120
|
+
}
|
|
121
|
+
useEffect(() => () => {
|
|
122
|
+
if (timer.current) clearTimeout(timer.current)
|
|
123
|
+
}, [])
|
|
124
|
+
// A value set from outside turns the wheel to it (a settle still waiting gives way to it)
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (touching.current) return
|
|
127
|
+
if (timer.current) {
|
|
128
|
+
clearTimeout(timer.current)
|
|
129
|
+
timer.current = null
|
|
130
|
+
}
|
|
131
|
+
if (Math.abs(last.current - index * row) > 0.5) turnTo(index, false)
|
|
132
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
133
|
+
}, [index, row])
|
|
134
|
+
const textAlign = align === "start" ? "left" : align === "end" ? "right" : "center"
|
|
135
|
+
const fontSize = android ? 20 : 22
|
|
136
|
+
const lineHeight = android ? 28 : 28
|
|
137
|
+
// A row's look by its distance from the band: it turns away and fades as on a cylinder (iOS); Android fades it
|
|
138
|
+
const rowStyle = (position: number) => {
|
|
139
|
+
const distance = Animated.divide(Animated.subtract(offset, position * row), row)
|
|
140
|
+
return {
|
|
141
|
+
opacity: distance.interpolate({ inputRange: [-3, -1, 0, 1, 3], outputRange: [0.25, android ? 0.6 : 0.85, 1, android ? 0.6 : 0.85, 0.25], extrapolate: "clamp" }),
|
|
142
|
+
transform: android
|
|
143
|
+
? []
|
|
144
|
+
: [
|
|
145
|
+
{ perspective: 600 },
|
|
146
|
+
{ rotateX: distance.interpolate({ inputRange: [-4, 4], outputRange: ["72deg", "-72deg"], extrapolate: "clamp" }) },
|
|
147
|
+
{ scale: distance.interpolate({ inputRange: [-3, 0, 3], outputRange: [0.9, 1, 0.9], extrapolate: "clamp" }) },
|
|
148
|
+
],
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const step = (direction: 1 | -1) => {
|
|
152
|
+
if (disabled) return
|
|
153
|
+
const next = nearestEnabled(index + direction)
|
|
154
|
+
if (next === index) return
|
|
155
|
+
turnTo(next, true)
|
|
156
|
+
onChange(next)
|
|
157
|
+
}
|
|
158
|
+
const gray = withAlpha(ink, android ? 0.38 : 0.35)
|
|
159
|
+
const labelFor = (item: WheelItem, position: number, color: string, pressable: boolean) => {
|
|
160
|
+
const text = (
|
|
161
|
+
<Animated.Text
|
|
162
|
+
numberOfLines={1}
|
|
163
|
+
style={[
|
|
164
|
+
{ width, height: row, fontSize, lineHeight: row, textAlign, color: item.disabled ? withAlpha(ink, 0.2) : color, paddingStart: align === "end" ? 4 : inset, paddingEnd: align === "start" ? 4 : align === "end" ? inset : 4 },
|
|
165
|
+
theme.font("regular"),
|
|
166
|
+
web ? ({ userSelect: "none" } as object) : null,
|
|
167
|
+
rowStyle(position),
|
|
168
|
+
]}
|
|
169
|
+
>
|
|
170
|
+
{item.label}
|
|
171
|
+
</Animated.Text>
|
|
172
|
+
)
|
|
173
|
+
if (!pressable) return <View key={position}>{text}</View>
|
|
174
|
+
return (
|
|
175
|
+
<Pressable
|
|
176
|
+
key={position}
|
|
177
|
+
disabled={item.disabled || disabled}
|
|
178
|
+
importantForAccessibility="no"
|
|
179
|
+
accessibilityElementsHidden
|
|
180
|
+
onPress={() => {
|
|
181
|
+
turnTo(position, true)
|
|
182
|
+
if (position !== index) onChange(position)
|
|
183
|
+
}}
|
|
184
|
+
>
|
|
185
|
+
{text}
|
|
186
|
+
</Pressable>
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
return (
|
|
190
|
+
<View
|
|
191
|
+
accessible
|
|
192
|
+
accessibilityRole="adjustable"
|
|
193
|
+
accessibilityLabel={label}
|
|
194
|
+
accessibilityValue={{ text: items[index]?.label }}
|
|
195
|
+
accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
|
|
196
|
+
onAccessibilityAction={(event) => step(event.nativeEvent.actionName === "increment" ? 1 : -1)}
|
|
197
|
+
{...((web
|
|
198
|
+
? {
|
|
199
|
+
tabIndex: disabled ? -1 : 0,
|
|
200
|
+
onKeyDown: (event: { key: string; preventDefault: () => void }) => {
|
|
201
|
+
if (event.key === "ArrowDown") step(1)
|
|
202
|
+
else if (event.key === "ArrowUp") step(-1)
|
|
203
|
+
else return
|
|
204
|
+
event.preventDefault()
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
: null) as object)}
|
|
208
|
+
style={{ width, height }}
|
|
209
|
+
testID={testID}
|
|
210
|
+
>
|
|
211
|
+
<ScrollView
|
|
212
|
+
ref={scroll as never}
|
|
213
|
+
scrollEnabled={!disabled}
|
|
214
|
+
showsVerticalScrollIndicator={false}
|
|
215
|
+
snapToInterval={row}
|
|
216
|
+
decelerationRate="fast"
|
|
217
|
+
scrollEventThrottle={16}
|
|
218
|
+
onScroll={onScroll}
|
|
219
|
+
onScrollBeginDrag={() => {
|
|
220
|
+
touching.current = true
|
|
221
|
+
}}
|
|
222
|
+
onScrollEndDrag={() => {
|
|
223
|
+
touching.current = false
|
|
224
|
+
if (timer.current) clearTimeout(timer.current)
|
|
225
|
+
timer.current = setTimeout(settle, 90)
|
|
226
|
+
}}
|
|
227
|
+
onLayout={() => turnTo(index, false)}
|
|
228
|
+
contentContainerStyle={{ paddingVertical: pad }}
|
|
229
|
+
style={web ? ({ scrollbarWidth: "none" } as object) : null}
|
|
230
|
+
>
|
|
231
|
+
{items.map((item, position) => labelFor(item, position, gray, true))}
|
|
232
|
+
</ScrollView>
|
|
233
|
+
{/* The rows on the band, in the ink: a copy clipped to the band, moving with the wheel */}
|
|
234
|
+
<View pointerEvents="none" style={[styles.clip, { top: pad, height: row, width }]}>
|
|
235
|
+
<Animated.View style={{ transform: [{ translateY: Animated.multiply(offset, -1) }] }}>
|
|
236
|
+
{items.map((item, position) => labelFor(item, position, ink, false))}
|
|
237
|
+
</Animated.View>
|
|
238
|
+
</View>
|
|
239
|
+
</View>
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const styles = StyleSheet.create({
|
|
244
|
+
wheels: { flexDirection: "row", justifyContent: "center", alignSelf: "stretch" },
|
|
245
|
+
band: { position: "absolute" },
|
|
246
|
+
clip: { position: "absolute", left: 0, overflow: "hidden" },
|
|
247
|
+
})
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/** Date helpers of DatePicker: plain `Date`s in the device's time zone, days compared by their calendar date. */
|
|
2
|
+
|
|
3
|
+
export function sameDay(a: Date | null | undefined, b: Date | null | undefined): boolean {
|
|
4
|
+
return a != null && b != null && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function startOfDay(date: Date): Date {
|
|
8
|
+
return new Date(date.getFullYear(), date.getMonth(), date.getDate())
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The first day of the month `offset` months from `date`'s. */
|
|
12
|
+
export function monthOf(date: Date, offset = 0): Date {
|
|
13
|
+
return new Date(date.getFullYear(), date.getMonth() + offset, 1)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A day lies outside `min`…`max` (compared by calendar day). */
|
|
17
|
+
export function dayOutside(day: Date, min?: Date, max?: Date): boolean {
|
|
18
|
+
const time = startOfDay(day).getTime()
|
|
19
|
+
return (min != null && time < startOfDay(min).getTime()) || (max != null && time > startOfDay(max).getTime())
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** A moment lies outside `min`…`max` (compared to the minute). */
|
|
23
|
+
export function timeOutside(date: Date, min?: Date, max?: Date): boolean {
|
|
24
|
+
return (min != null && date.getTime() < min.getTime()) || (max != null && date.getTime() > max.getTime())
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The date kept inside `min`…`max`. */
|
|
28
|
+
export function clampDate(date: Date, min?: Date, max?: Date): Date {
|
|
29
|
+
if (min != null && date.getTime() < min.getTime()) return new Date(min.getTime())
|
|
30
|
+
if (max != null && date.getTime() > max.getTime()) return new Date(max.getTime())
|
|
31
|
+
return date
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The weeks of a month, each of seven days; days of the months around it are `null`. */
|
|
35
|
+
export function monthWeeks(month: Date, weekStartsOn: number): (Date | null)[][] {
|
|
36
|
+
const year = month.getFullYear()
|
|
37
|
+
const index = month.getMonth()
|
|
38
|
+
const lead = (new Date(year, index, 1).getDay() - weekStartsOn + 7) % 7
|
|
39
|
+
const days = new Date(year, index + 1, 0).getDate()
|
|
40
|
+
const cells: (Date | null)[] = [...Array<null>(lead).fill(null)]
|
|
41
|
+
for (let day = 1; day <= days; day += 1) cells.push(new Date(year, index, day))
|
|
42
|
+
while (cells.length % 7) cells.push(null)
|
|
43
|
+
const weeks: (Date | null)[][] = []
|
|
44
|
+
for (let start = 0; start < cells.length; start += 7) weeks.push(cells.slice(start, start + 7))
|
|
45
|
+
return weeks
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** `value` on `day`: its time kept, the start of the day when there is no value yet. */
|
|
49
|
+
export function withDay(value: Date | null, day: Date): Date {
|
|
50
|
+
return new Date(day.getFullYear(), day.getMonth(), day.getDate(), value?.getHours() ?? 0, value?.getMinutes() ?? 0)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `value` at `hours`:`minutes`, today when there is no value yet. */
|
|
54
|
+
export function withTime(value: Date | null, hours: number, minutes: number): Date {
|
|
55
|
+
const base = value ?? new Date()
|
|
56
|
+
return new Date(base.getFullYear(), base.getMonth(), base.getDate(), hours, minutes)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The day a week starts on in the locale (0 Sunday … 6 Saturday), from `Intl.Locale`'s week info; Monday without it. */
|
|
60
|
+
export function localeWeekStart(locale?: string): number {
|
|
61
|
+
try {
|
|
62
|
+
const info = new Intl.Locale(locale ?? Intl.DateTimeFormat().resolvedOptions().locale) as Intl.Locale & {
|
|
63
|
+
weekInfo?: { firstDay: number }
|
|
64
|
+
getWeekInfo?: () => { firstDay: number }
|
|
65
|
+
}
|
|
66
|
+
const first = info.getWeekInfo?.().firstDay ?? info.weekInfo?.firstDay
|
|
67
|
+
if (first != null) return first % 7
|
|
68
|
+
} catch {
|
|
69
|
+
// An engine without Intl.Locale
|
|
70
|
+
}
|
|
71
|
+
return 1
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The locale reads time on a 12-hour clock. */
|
|
75
|
+
export function localeHour12(locale?: string): boolean {
|
|
76
|
+
try {
|
|
77
|
+
const options = new Intl.DateTimeFormat(locale, { hour: "numeric" }).resolvedOptions() as Intl.ResolvedDateTimeFormatOptions & { hourCycle?: string }
|
|
78
|
+
if (options.hourCycle) return options.hourCycle === "h11" || options.hourCycle === "h12"
|
|
79
|
+
return options.hour12 === true
|
|
80
|
+
} catch {
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function format(locale: string | undefined, options: Intl.DateTimeFormatOptions, date: Date): string {
|
|
86
|
+
try {
|
|
87
|
+
// The grid and the wheels are Gregorian: the names follow it in every locale (fa-IR, th-TH)
|
|
88
|
+
return new Intl.DateTimeFormat(locale, { calendar: "gregory", ...options }).format(date)
|
|
89
|
+
} catch {
|
|
90
|
+
return date.toDateString()
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The date as a compact picker shows it: "13 Nov 2026" (iOS) or "Nov 13, 2026", by the locale. */
|
|
95
|
+
export function formatDate(date: Date, locale?: string): string {
|
|
96
|
+
return format(locale, { day: "numeric", month: "short", year: "numeric" }, date)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The time: "09:00", or "9:00 AM" on a 12-hour clock. */
|
|
100
|
+
export function formatTime(date: Date, locale: string | undefined, hour12: boolean): string {
|
|
101
|
+
return format(locale, { hour: hour12 ? "numeric" : "2-digit", minute: "2-digit", hour12 }, date)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The month's title: "November 2026". */
|
|
105
|
+
export function formatMonth(month: Date, locale?: string): string {
|
|
106
|
+
return format(locale, { month: "long", year: "numeric" }, month)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Android's dialog headline: "Tue, Nov 24". */
|
|
110
|
+
export function formatHeadline(date: Date, locale?: string): string {
|
|
111
|
+
return format(locale, { weekday: "short", month: "short", day: "numeric" }, date)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A day on a date-and-time wheel: "Wed Nov 25". */
|
|
115
|
+
export function formatWheelDay(date: Date, locale?: string): string {
|
|
116
|
+
return format(locale, { weekday: "short", month: "short", day: "numeric" }, date).replace(/,/g, "")
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A day read out in full: "Tuesday, 24 November 2026". */
|
|
120
|
+
export function formatFullDay(date: Date, locale?: string): string {
|
|
121
|
+
return format(locale, { weekday: "long", day: "numeric", month: "long", year: "numeric" }, date)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The weekdays' labels from `weekStartsOn`: "MON" (`short`, iOS) or "M" (`narrow`, Android). */
|
|
125
|
+
export function weekdayLabels(locale: string | undefined, weekStartsOn: number, width: "short" | "narrow"): { label: string; name: string }[] {
|
|
126
|
+
// 2023-01-01 was a Sunday
|
|
127
|
+
return Array.from({ length: 7 }, (_, index) => {
|
|
128
|
+
const day = new Date(2023, 0, 1 + ((weekStartsOn + index) % 7))
|
|
129
|
+
return { label: format(locale, { weekday: width }, day), name: format(locale, { weekday: "long" }, day) }
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The month names of the locale, January first. */
|
|
134
|
+
export function monthNames(locale?: string): string[] {
|
|
135
|
+
return Array.from({ length: 12 }, (_, index) => format(locale, { month: "long" }, new Date(2023, index, 1)))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The order of day, month and year in the locale's dates, for the date wheels. */
|
|
139
|
+
export function dateOrder(locale?: string): ("day" | "month" | "year")[] {
|
|
140
|
+
try {
|
|
141
|
+
const parts = new Intl.DateTimeFormat(locale, { calendar: "gregory", day: "numeric", month: "long", year: "numeric" }).formatToParts(new Date(2023, 10, 24))
|
|
142
|
+
const order = parts.map((part) => part.type).filter((type): type is "day" | "month" | "year" => type === "day" || type === "month" || type === "year")
|
|
143
|
+
if (order.length === 3) return order
|
|
144
|
+
} catch {
|
|
145
|
+
// An engine without formatToParts
|
|
146
|
+
}
|
|
147
|
+
return ["month", "day", "year"]
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The locale's day period labels: ["AM", "PM"]. */
|
|
151
|
+
export function periodLabels(locale?: string): [string, string] {
|
|
152
|
+
try {
|
|
153
|
+
const read = (hour: number) =>
|
|
154
|
+
new Intl.DateTimeFormat(locale, { hour: "numeric", hour12: true }).formatToParts(new Date(2023, 0, 1, hour)).find((part) => part.type === "dayPeriod")?.value
|
|
155
|
+
return [read(9) ?? "AM", read(21) ?? "PM"]
|
|
156
|
+
} catch {
|
|
157
|
+
return ["AM", "PM"]
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export const pad2 = (value: number) => String(value).padStart(2, "0")
|
|
@@ -234,6 +234,52 @@ export function ChoiceDialog({ open, onClose, title, cancelLabel, okLabel, onOk,
|
|
|
234
234
|
)
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
+
export type PickerDialogProps = {
|
|
238
|
+
open: boolean
|
|
239
|
+
onClose: () => void
|
|
240
|
+
/** Applies the choice and closes the dialog. */
|
|
241
|
+
onOk: () => void
|
|
242
|
+
cancelLabel: string
|
|
243
|
+
okLabel: string
|
|
244
|
+
/** The dialog's name for a screen reader. */
|
|
245
|
+
label?: string
|
|
246
|
+
/** The picker, across the dialog's whole width (its own header in it). */
|
|
247
|
+
children: ReactNode
|
|
248
|
+
testID?: string
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Android's picker dialog: the dialog's panel holding a picker of its own (Material 3's date picker or time picker
|
|
253
|
+
* dial), Cancel and OK at the end. DatePicker's dialog on Android; not part of the public API.
|
|
254
|
+
*/
|
|
255
|
+
export function PickerDialog({ open, onClose, onOk, cancelLabel, okLabel, label, children, testID }: PickerDialogProps) {
|
|
256
|
+
const theme = useTheme()
|
|
257
|
+
const ids = { title: useId(), description: useId() }
|
|
258
|
+
const a11y =
|
|
259
|
+
Platform.OS === "web"
|
|
260
|
+
? { role: "dialog" as const, "aria-modal": true, "aria-label": label }
|
|
261
|
+
: { accessibilityViewIsModal: true, accessibilityLabel: label }
|
|
262
|
+
const run = (action: DialogAction) => {
|
|
263
|
+
if (!open) return
|
|
264
|
+
action.onPress?.()
|
|
265
|
+
onClose()
|
|
266
|
+
}
|
|
267
|
+
return (
|
|
268
|
+
<AndroidFrame open={open} onClose={onClose} dismissible a11y={a11y} testID={testID}>
|
|
269
|
+
<AndroidDialog
|
|
270
|
+
theme={theme}
|
|
271
|
+
actions={[
|
|
272
|
+
{ label: cancelLabel, variant: "cancel", testID: testID ? `${testID}-cancel` : undefined },
|
|
273
|
+
{ label: okLabel, preferred: true, onPress: onOk, testID: testID ? `${testID}-ok` : undefined },
|
|
274
|
+
]}
|
|
275
|
+
run={run}
|
|
276
|
+
ids={ids}
|
|
277
|
+
list={children}
|
|
278
|
+
/>
|
|
279
|
+
</AndroidFrame>
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
237
283
|
type PanelProps = {
|
|
238
284
|
theme: Theme
|
|
239
285
|
title: ReactNode
|