@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
|
@@ -19,6 +19,16 @@ export function counterBadgeColors(theme, color) {
|
|
|
19
19
|
export function badgeColors(theme, variant, color) {
|
|
20
20
|
if (variant === "counter")
|
|
21
21
|
return counterBadgeColors(theme, color);
|
|
22
|
+
// The accent has a solid and a soft step and one text color (styles' accent tokens): soft on the soft fill, the
|
|
23
|
+
// outline's text and line in that text color
|
|
24
|
+
if (color === "accent") {
|
|
25
|
+
const text = theme.color("--color-text-accent");
|
|
26
|
+
if (variant === "solid")
|
|
27
|
+
return { background: theme.color("--color-background-accent-solid"), text: theme.color("--color-text-accent-solid", "#FFFFFF"), border: undefined };
|
|
28
|
+
if (variant === "outline")
|
|
29
|
+
return { background: "transparent", text, border: text };
|
|
30
|
+
return { background: theme.color("--color-background-accent-soft"), text, border: undefined };
|
|
31
|
+
}
|
|
22
32
|
if (variant === "outline")
|
|
23
33
|
return { background: "transparent", text: theme.color(`--color-text-${color}-outline`), border: theme.color(`--color-border-${color}-outline`) };
|
|
24
34
|
if (variant === "solid")
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo, useState } from "react";
|
|
3
|
+
import { Platform, Pressable, StyleSheet, View } from "react-native";
|
|
4
|
+
import ChevronLeftLg from "../../icons/plex/ChevronLeftLg";
|
|
5
|
+
import ChevronRightLg from "../../icons/plex/ChevronRightLg";
|
|
6
|
+
import { isRTL } from "../../internal/direction";
|
|
7
|
+
import { withAlpha } from "../../theme/material";
|
|
8
|
+
import { useTheme } from "../../theme/ThemeProvider";
|
|
9
|
+
import { MaterialChevronLeft, MaterialChevronRight } from "../Icon/material";
|
|
10
|
+
import { Text } from "../Text/Text";
|
|
11
|
+
import { dayOutside, formatFullDay, formatMonth, monthOf, monthWeeks, sameDay, weekdayLabels } from "./dates";
|
|
12
|
+
/** iOS: the corner's inset around the grid of a compact picker's calendar (the web's `--calendar-inner-padding`). */
|
|
13
|
+
export const IOS_CALENDAR_INSET = 10;
|
|
14
|
+
/**
|
|
15
|
+
* The month grid of DatePicker. On iOS the phone calendar of @plexui/ui (Calendar.module.css under 768px): the month's
|
|
16
|
+
* title Semibold 17 at the row's text inset, the two arrows as 44 targets at the end, the weekdays Semibold 13 in
|
|
17
|
+
* capitals in the tertiary label, the days Regular 17 in 44 cells side by side and row on row, the chosen day white on
|
|
18
|
+
* a 44 circle of the text's color, today a 4 dot under its digits, days out of range at 40%. On Android Material 3's
|
|
19
|
+
* date picker: the month at the start in Medium 14 with the arrows at the end, the weekdays' letters, the days in 40dp
|
|
20
|
+
* circles, the chosen one filled with the primary color and today ringed in it.
|
|
21
|
+
*/
|
|
22
|
+
export function Calendar({ value, onSelect, min, max, locale, weekStartsOn, width, disabled: off = false, previousLabel = "Previous month", nextLabel = "Next month", testID }) {
|
|
23
|
+
const theme = useTheme();
|
|
24
|
+
const android = theme.isAndroid;
|
|
25
|
+
const [month, setMonth] = useState(() => monthOf(value ?? new Date()));
|
|
26
|
+
// The value moved to another month from outside (a wheel, a reset): the calendar turns to it
|
|
27
|
+
const [shownFor, setShownFor] = useState(value?.getTime() ?? null);
|
|
28
|
+
if ((value?.getTime() ?? null) !== shownFor) {
|
|
29
|
+
setShownFor(value?.getTime() ?? null);
|
|
30
|
+
if (value != null && monthOf(value).getTime() !== month.getTime())
|
|
31
|
+
setMonth(monthOf(value));
|
|
32
|
+
}
|
|
33
|
+
const weeks = useMemo(() => monthWeeks(month, weekStartsOn), [month, weekStartsOn]);
|
|
34
|
+
const labels = useMemo(() => weekdayLabels(locale, weekStartsOn, android ? "narrow" : "short"), [locale, weekStartsOn, android]);
|
|
35
|
+
const today = new Date();
|
|
36
|
+
// The arrows stop at the months that hold no day in range
|
|
37
|
+
const canPrevious = min == null || monthOf(month, -1).getTime() >= monthOf(min).getTime();
|
|
38
|
+
const canNext = max == null || monthOf(month, 1).getTime() <= monthOf(max).getTime();
|
|
39
|
+
const rtl = isRTL();
|
|
40
|
+
const cell = android ? Math.min(48, width / 7) : Math.max(44, width / 7);
|
|
41
|
+
const m = theme.material;
|
|
42
|
+
const text = android ? m.onSurface : theme.colors.text;
|
|
43
|
+
const arrowColor = android ? m.onSurfaceVariant : text;
|
|
44
|
+
const arrow = (direction) => {
|
|
45
|
+
const enabled = !off && (direction < 0 ? canPrevious : canNext);
|
|
46
|
+
// In a right-to-left layout the earlier month is on the right, its arrow pointing right
|
|
47
|
+
const pointsLeft = (direction < 0) !== rtl;
|
|
48
|
+
const Icon = android ? (pointsLeft ? MaterialChevronLeft : MaterialChevronRight) : pointsLeft ? ChevronLeftLg : ChevronRightLg;
|
|
49
|
+
return (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: direction < 0 ? previousLabel : nextLabel, accessibilityState: { disabled: !enabled }, disabled: !enabled, onPress: () => setMonth((current) => monthOf(current, direction)), testID: testID ? `${testID}-${direction < 0 ? "previous" : "next"}` : undefined, style: ({ pressed }) => [
|
|
50
|
+
android ? styles.androidArrow : styles.arrow,
|
|
51
|
+
pressed ? { backgroundColor: android ? withAlpha(m.onSurfaceVariant, 0.1) : theme.color("--button-background-color-active", withAlpha(text, 0.1)) } : null,
|
|
52
|
+
{ opacity: enabled ? 1 : android ? 0.38 : 0.4 },
|
|
53
|
+
], children: _jsx(Icon, { size: 24, color: arrowColor }) }));
|
|
54
|
+
};
|
|
55
|
+
return (_jsxs(View, { style: { width }, testID: testID, children: [_jsxs(View, { style: [styles.header, android ? styles.androidHeader : { height: 44 }], children: [_jsx(Text, { accessibilityRole: "header", accessibilityLiveRegion: "polite", numberOfLines: 1, color: android ? m.onSurfaceVariant : text, style: [
|
|
56
|
+
styles.title,
|
|
57
|
+
android
|
|
58
|
+
? [styles.androidTitle, theme.font("medium")]
|
|
59
|
+
: [{ fontSize: 17, lineHeight: 22, paddingStart: Math.max(0, theme.length("--field-row-padding-inline", 16) - IOS_CALENDAR_INSET) }, theme.font("semibold")],
|
|
60
|
+
], children: formatMonth(month, locale) }), arrow(-1), arrow(1)] }), _jsx(View, { style: [styles.week, android ? { height: 48 } : styles.weekdays, android ? null : { marginBottom: 3 }], importantForAccessibility: "no-hide-descendants", accessibilityElementsHidden: true, children: labels.map(({ label, name }) => (_jsx(Text, { color: android ? m.onSurface : theme.colors.labelTertiary, style: [
|
|
61
|
+
{ width: cell, textAlign: "center" },
|
|
62
|
+
android ? styles.androidWeekday : [styles.weekday, theme.font("semibold")],
|
|
63
|
+
], children: android ? label : label.toUpperCase() }, name))) }), weeks.map((week, row) => (_jsx(View, { style: styles.week, children: week.map((day, column) => day == null ? (_jsx(View, { style: { width: cell, height: android ? 48 : 44 } }, column)) : (_jsx(Day, { day: day, cell: cell, chosen: sameDay(day, value), isToday: sameDay(day, today), disabled: dayOutside(day, min, max), locked: off, locale: locale, onSelect: onSelect, testID: testID ? `${testID}-day-${day.getDate()}` : undefined }, column))) }, row)))] }));
|
|
64
|
+
}
|
|
65
|
+
function Day({ day, cell, chosen, isToday, disabled, locked, locale, onSelect, testID }) {
|
|
66
|
+
const theme = useTheme();
|
|
67
|
+
const android = theme.isAndroid;
|
|
68
|
+
const m = theme.material;
|
|
69
|
+
const ink = theme.colors.text;
|
|
70
|
+
const size = android ? 40 : 44;
|
|
71
|
+
const digitColor = android
|
|
72
|
+
? chosen
|
|
73
|
+
? m.onPrimary
|
|
74
|
+
: disabled
|
|
75
|
+
? withAlpha(m.onSurface, 0.38)
|
|
76
|
+
: isToday
|
|
77
|
+
? m.primary
|
|
78
|
+
: m.onSurface
|
|
79
|
+
: chosen
|
|
80
|
+
? theme.colors.textInverse
|
|
81
|
+
: ink;
|
|
82
|
+
return (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: formatFullDay(day, locale), accessibilityState: { selected: chosen, disabled: disabled || locked }, disabled: disabled || locked, onPress: () => onSelect(day), testID: testID, style: [styles.cell, { width: cell, height: android ? 48 : 44 }], children: ({ pressed }) => (_jsxs(View, { style: [
|
|
83
|
+
styles.circle,
|
|
84
|
+
{ width: size, height: size, borderRadius: size / 2 },
|
|
85
|
+
chosen
|
|
86
|
+
? { backgroundColor: android ? m.primary : ink }
|
|
87
|
+
: android
|
|
88
|
+
? [isToday ? { borderWidth: 1, borderColor: m.primary } : null, pressed ? { backgroundColor: withAlpha(m.onSurface, 0.1) } : null]
|
|
89
|
+
: // iOS: the web's hover ring, shown while the finger is on the day
|
|
90
|
+
pressed
|
|
91
|
+
? { borderWidth: 1, borderColor: ink }
|
|
92
|
+
: null,
|
|
93
|
+
!android && disabled ? { opacity: 0.4 } : null,
|
|
94
|
+
], children: [_jsx(Text, { color: digitColor, style: android ? styles.androidDigit : styles.digit, children: day.getDate() }), isToday && !android ? _jsx(View, { style: [styles.dot, { backgroundColor: chosen ? theme.colors.textInverse : ink }] }) : null] })) }));
|
|
95
|
+
}
|
|
96
|
+
const styles = StyleSheet.create({
|
|
97
|
+
header: { flexDirection: "row", alignItems: "center" },
|
|
98
|
+
androidHeader: { height: 56, paddingStart: 12, paddingEnd: 0 },
|
|
99
|
+
title: { flex: 1 },
|
|
100
|
+
androidTitle: { fontSize: 14, lineHeight: 20, letterSpacing: 0.1 },
|
|
101
|
+
arrow: { width: 44, height: 44, borderRadius: 22, alignItems: "center", justifyContent: "center" },
|
|
102
|
+
androidArrow: { width: 48, height: 48, borderRadius: 24, alignItems: "center", justifyContent: "center" },
|
|
103
|
+
week: { flexDirection: "row", alignItems: "center", justifyContent: "center" },
|
|
104
|
+
weekdays: { height: 20 },
|
|
105
|
+
weekday: { fontSize: 13, lineHeight: 18 },
|
|
106
|
+
androidWeekday: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5 },
|
|
107
|
+
cell: { alignItems: "center", justifyContent: "center" },
|
|
108
|
+
circle: { alignItems: "center", justifyContent: "center" },
|
|
109
|
+
digit: { fontSize: 17, lineHeight: 22, ...(Platform.OS === "web" ? { userSelect: "none" } : null) },
|
|
110
|
+
androidDigit: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5, ...(Platform.OS === "web" ? { userSelect: "none" } : null) },
|
|
111
|
+
dot: { position: "absolute", bottom: 8, width: 4, height: 4, borderRadius: 2 },
|
|
112
|
+
});
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useContext, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { Pressable, StyleSheet, View, useWindowDimensions } from "react-native";
|
|
4
|
+
import { RowEndContext, RowHoldContext, rowHold } from "../../internal/row";
|
|
5
|
+
import { withAlpha } from "../../theme/material";
|
|
6
|
+
import { useTheme } from "../../theme/ThemeProvider";
|
|
7
|
+
import { PickerDialog } from "../Dialog/Dialog";
|
|
8
|
+
import { MaterialCalendarToday, MaterialSchedule } from "../Icon/material";
|
|
9
|
+
import { FieldOutline, fieldLook } from "../Input/Input";
|
|
10
|
+
import { Popover } from "../Popover/Popover";
|
|
11
|
+
import { Text } from "../Text/Text";
|
|
12
|
+
import { Calendar, IOS_CALENDAR_INSET } from "./Calendar";
|
|
13
|
+
import { clampDate, dateOrder, formatDate, formatHeadline, formatTime, formatWheelDay, localeHour12, localeWeekStart, monthNames, pad2, sameDay, periodLabels, startOfDay, timeOutside, withDay, withTime, } from "./dates";
|
|
14
|
+
import { TimeDial } from "./TimeDial";
|
|
15
|
+
import { Wheels } from "./Wheel";
|
|
16
|
+
/**
|
|
17
|
+
* A date and time picker. On iOS UIDatePicker of iOS 27.2: `compact`, the date and the time in two capsules 8 apart
|
|
18
|
+
* (34 tall, 12 in, the secondary soft fill, the text in the accent while open) that open the month's calendar and the
|
|
19
|
+
* time's wheels in the glass popover, at most 32 from the screen's edge; `inline`, the calendar in place (a Time row
|
|
20
|
+
* under it for both); `wheels`, the wheels. On Android Material 3's pickers: the value as a row's value in the accent
|
|
21
|
+
* (or an outlined field with its icon) that opens the date picker dialog (the headline date over the month) or the
|
|
22
|
+
* time picker dial, both applied with OK; `inline` the calendar in place, `wheels` Android's spinners. The value is a
|
|
23
|
+
* plain `Date`; `min` and `max` bound it.
|
|
24
|
+
*/
|
|
25
|
+
export function DatePicker({ value, onChange, mode = "date", display = "compact", min, max, locale, weekStartsOn, hour12, placeholder, disabled = false, "aria-label": ariaLabel, dateTitle = "Select date", timeTitle = "Select time", cancelLabel = "Cancel", okLabel = "OK", timeLabel = "Time", testID, style, }) {
|
|
26
|
+
const theme = useTheme();
|
|
27
|
+
const busy = useRef(false);
|
|
28
|
+
const shared = {
|
|
29
|
+
busy,
|
|
30
|
+
value,
|
|
31
|
+
set: (next) => onChange(clampDate(next, min, max)),
|
|
32
|
+
min,
|
|
33
|
+
max,
|
|
34
|
+
locale,
|
|
35
|
+
weekStartsOn: weekStartsOn ?? localeWeekStart(locale),
|
|
36
|
+
hour12: hour12 ?? localeHour12(locale),
|
|
37
|
+
disabled,
|
|
38
|
+
ariaLabel,
|
|
39
|
+
placeholder: { date: placeholder ?? "Select date", time: placeholder ?? "Select time" },
|
|
40
|
+
dateTitle,
|
|
41
|
+
timeTitle,
|
|
42
|
+
cancelLabel,
|
|
43
|
+
okLabel,
|
|
44
|
+
testID,
|
|
45
|
+
};
|
|
46
|
+
if (display === "wheels" || (display === "inline" && mode === "time")) {
|
|
47
|
+
return (_jsx(View, { style: style, pointerEvents: disabled ? "none" : "auto", children: _jsx(View, { style: { opacity: disabled ? 0.4 : 1 }, children: _jsx(Wheels, { columns: wheelColumns(shared, mode), height: theme.isAndroid ? undefined : 216, bandInset: 8, disabled: disabled, testID: testID }) }) }));
|
|
48
|
+
}
|
|
49
|
+
if (display === "inline")
|
|
50
|
+
return _jsx(InlinePicker, { shared: shared, mode: mode, timeLabel: timeLabel, style: style });
|
|
51
|
+
return _jsx(Compact, { shared: shared, mode: mode, style: style });
|
|
52
|
+
}
|
|
53
|
+
DatePicker.displayName = "DatePicker";
|
|
54
|
+
/** The compact parts, at a row's end: they take the room after the label (two capsules pass its 60%), set at its end. */
|
|
55
|
+
function Compact({ shared, mode, style }) {
|
|
56
|
+
const row = useContext(RowEndContext);
|
|
57
|
+
const fill = row?.fill;
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (!fill)
|
|
60
|
+
return;
|
|
61
|
+
fill(true);
|
|
62
|
+
return () => fill(false);
|
|
63
|
+
}, [fill]);
|
|
64
|
+
return (_jsxs(View, { style: [styles.parts, row ? styles.atEnd : null, style], testID: shared.testID, children: [mode !== "time" ? _jsx(Part, { shared: shared, kind: "date", first: true }) : null, mode !== "date" ? _jsx(Part, { shared: shared, kind: "time", first: mode === "time" }) : null] }));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The wheels, as UIDatePicker's `.wheels` (iOS 27.2, en_US, 402pt): a date's month, day and year in the locale's order,
|
|
68
|
+
* 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,
|
|
69
|
+
* AM / PM from 49 after the minute's centre; with both, one column of days ("Wed Nov 25", "Today") before the time.
|
|
70
|
+
*/
|
|
71
|
+
function wheelColumns(shared, mode) {
|
|
72
|
+
const { value, set, min, max, locale } = shared;
|
|
73
|
+
const base = value ?? clampDate(new Date(), min, max);
|
|
74
|
+
const columns = [];
|
|
75
|
+
if (mode === "datetime")
|
|
76
|
+
columns.push(dayColumn(shared, base));
|
|
77
|
+
else if (mode === "date") {
|
|
78
|
+
const names = monthNames(locale);
|
|
79
|
+
// The years stand still around today (not the value), so turning the wheel never moves its rows
|
|
80
|
+
const thisYear = new Date().getFullYear();
|
|
81
|
+
const yearFrom = Math.min(min?.getFullYear() ?? thisYear - 100, base.getFullYear());
|
|
82
|
+
const yearTo = Math.max(max?.getFullYear() ?? thisYear + 50, base.getFullYear());
|
|
83
|
+
const days = new Date(base.getFullYear(), base.getMonth() + 1, 0).getDate();
|
|
84
|
+
// A day that would fall out of the month (31 into February) is the month's last
|
|
85
|
+
const on = (year, month, day) => {
|
|
86
|
+
const last = new Date(year, month + 1, 0).getDate();
|
|
87
|
+
return new Date(year, month, Math.min(day, last), base.getHours(), base.getMinutes());
|
|
88
|
+
};
|
|
89
|
+
const dayOut = (date) => (min != null && startOfDay(date) < startOfDay(min)) || (max != null && startOfDay(date) > startOfDay(max));
|
|
90
|
+
const byPart = {
|
|
91
|
+
month: {
|
|
92
|
+
key: "month",
|
|
93
|
+
label: "Month",
|
|
94
|
+
width: 157,
|
|
95
|
+
items: names.map((label, month) => ({
|
|
96
|
+
label,
|
|
97
|
+
disabled: (min != null && new Date(base.getFullYear(), month + 1, 0) < startOfDay(min)) || (max != null && new Date(base.getFullYear(), month, 1) > startOfDay(max)),
|
|
98
|
+
})),
|
|
99
|
+
index: base.getMonth(),
|
|
100
|
+
onChange: (month) => set(on(base.getFullYear(), month, base.getDate())),
|
|
101
|
+
},
|
|
102
|
+
day: {
|
|
103
|
+
key: "day",
|
|
104
|
+
label: "Day",
|
|
105
|
+
width: 50,
|
|
106
|
+
items: Array.from({ length: days }, (_, index) => ({ label: String(index + 1), disabled: dayOut(on(base.getFullYear(), base.getMonth(), index + 1)) })),
|
|
107
|
+
index: base.getDate() - 1,
|
|
108
|
+
onChange: (index) => set(on(base.getFullYear(), base.getMonth(), index + 1)),
|
|
109
|
+
},
|
|
110
|
+
year: {
|
|
111
|
+
key: "year",
|
|
112
|
+
label: "Year",
|
|
113
|
+
width: 105,
|
|
114
|
+
items: Array.from({ length: yearTo - yearFrom + 1 }, (_, index) => ({ label: String(yearFrom + index) })),
|
|
115
|
+
index: base.getFullYear() - yearFrom,
|
|
116
|
+
onChange: (index) => set(on(yearFrom + index, base.getMonth(), base.getDate())),
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
for (const part of dateOrder(locale))
|
|
120
|
+
columns.push(byPart[part]);
|
|
121
|
+
}
|
|
122
|
+
if (mode !== "date")
|
|
123
|
+
columns.push(...timeColumns(shared, base, set));
|
|
124
|
+
return columns;
|
|
125
|
+
}
|
|
126
|
+
/** The days of a date-and-time wheel: a year each side of today (or `min` to `max`), "Today" for today. */
|
|
127
|
+
function dayColumn(shared, base) {
|
|
128
|
+
const { min, max, locale, set } = shared;
|
|
129
|
+
const today = startOfDay(new Date());
|
|
130
|
+
const from = min ? startOfDay(min) : new Date(today.getFullYear() - 1, today.getMonth(), today.getDate());
|
|
131
|
+
const to = max ? startOfDay(max) : new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
|
|
132
|
+
const first = startOfDay(base) < from ? startOfDay(base) : from;
|
|
133
|
+
const last = startOfDay(base) > to ? startOfDay(base) : to;
|
|
134
|
+
const days = [];
|
|
135
|
+
for (let day = first; day <= last; day = new Date(day.getFullYear(), day.getMonth(), day.getDate() + 1))
|
|
136
|
+
days.push(day);
|
|
137
|
+
return {
|
|
138
|
+
key: "day",
|
|
139
|
+
label: "Day",
|
|
140
|
+
width: 130,
|
|
141
|
+
align: "end",
|
|
142
|
+
items: days.map((day) => ({ label: sameDay(day, today) ? "Today" : formatWheelDay(day, locale) })),
|
|
143
|
+
index: Math.max(0, days.findIndex((day) => sameDay(day, base))),
|
|
144
|
+
onChange: (index) => set(withDay(base, days[index])),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function timeColumns(shared, base, set) {
|
|
148
|
+
const { min, max, locale, hour12 } = shared;
|
|
149
|
+
const hours = base.getHours();
|
|
150
|
+
const minutes = base.getMinutes();
|
|
151
|
+
// An hour can be chosen when any of its minutes lies in range
|
|
152
|
+
const hourOut = (hour) => (min != null && withTime(base, hour, 59) < min) || (max != null && withTime(base, hour, 0) > max);
|
|
153
|
+
const columns = [
|
|
154
|
+
hour12
|
|
155
|
+
? {
|
|
156
|
+
key: "hours",
|
|
157
|
+
label: "Hours",
|
|
158
|
+
width: 64,
|
|
159
|
+
items: Array.from({ length: 12 }, (_, index) => ({ label: String(index === 0 ? 12 : index), disabled: hourOut(index + (hours >= 12 ? 12 : 0)) })),
|
|
160
|
+
index: hours % 12,
|
|
161
|
+
onChange: (index) => set(withTime(base, index + (hours >= 12 ? 12 : 0), minutes)),
|
|
162
|
+
}
|
|
163
|
+
: {
|
|
164
|
+
key: "hours",
|
|
165
|
+
label: "Hours",
|
|
166
|
+
width: 64,
|
|
167
|
+
items: Array.from({ length: 24 }, (_, hour) => ({ label: pad2(hour), disabled: hourOut(hour) })),
|
|
168
|
+
index: hours,
|
|
169
|
+
onChange: (hour) => set(withTime(base, hour, minutes)),
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
key: "minutes",
|
|
173
|
+
label: "Minutes",
|
|
174
|
+
width: 64,
|
|
175
|
+
items: Array.from({ length: 60 }, (_, minute) => ({ label: pad2(minute), disabled: timeOutside(withTime(base, hours, minute), min, max) })),
|
|
176
|
+
index: minutes,
|
|
177
|
+
onChange: (minute) => set(withTime(base, hours, minute)),
|
|
178
|
+
},
|
|
179
|
+
];
|
|
180
|
+
if (hour12) {
|
|
181
|
+
const [am, pm] = periodLabels(locale);
|
|
182
|
+
// AM / PM from 49 after the minutes' centre: 17 into its column
|
|
183
|
+
columns.push({
|
|
184
|
+
key: "period",
|
|
185
|
+
label: "AM/PM",
|
|
186
|
+
width: 64,
|
|
187
|
+
align: "start",
|
|
188
|
+
inset: 17,
|
|
189
|
+
items: [
|
|
190
|
+
{ label: am, disabled: hourOut(hours % 12) && [...Array(12).keys()].every((hour) => hourOut(hour)) },
|
|
191
|
+
{ label: pm, disabled: [...Array(12).keys()].every((hour) => hourOut(hour + 12)) },
|
|
192
|
+
],
|
|
193
|
+
index: hours >= 12 ? 1 : 0,
|
|
194
|
+
onChange: (index) => set(withTime(base, (hours % 12) + (index ? 12 : 0), minutes)),
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return columns;
|
|
198
|
+
}
|
|
199
|
+
/** One part of a compact picker: the date or the time. */
|
|
200
|
+
function Part({ shared, kind, first }) {
|
|
201
|
+
const theme = useTheme();
|
|
202
|
+
const row = useContext(RowEndContext);
|
|
203
|
+
const hold = useContext(RowHoldContext);
|
|
204
|
+
const [open, setOwnOpen] = useState(false);
|
|
205
|
+
// Presses inside a picker's dialog or popover reach the row in React's tree: while one is open, and for the press
|
|
206
|
+
// that closes it, the row opens nothing
|
|
207
|
+
const setOpen = (next) => {
|
|
208
|
+
if (next) {
|
|
209
|
+
if (shared.busy.current)
|
|
210
|
+
return;
|
|
211
|
+
shared.busy.current = true;
|
|
212
|
+
}
|
|
213
|
+
else
|
|
214
|
+
setTimeout(() => (shared.busy.current = false), 0);
|
|
215
|
+
setOwnOpen(next);
|
|
216
|
+
};
|
|
217
|
+
const screen = useWindowDimensions().width;
|
|
218
|
+
const { value, disabled, locale, hour12, ariaLabel, testID } = shared;
|
|
219
|
+
if (disabled && open) {
|
|
220
|
+
shared.busy.current = false;
|
|
221
|
+
setOwnOpen(false);
|
|
222
|
+
}
|
|
223
|
+
const text = value == null ? null : kind === "date" ? formatDate(value, locale) : formatTime(value, locale, hour12);
|
|
224
|
+
const shown = text ?? shared.placeholder[kind];
|
|
225
|
+
const partID = testID ? `${testID}-${kind}` : undefined;
|
|
226
|
+
const a11y = {
|
|
227
|
+
accessibilityRole: "button",
|
|
228
|
+
accessibilityLabel: [ariaLabel, shown].filter(Boolean).join(", "),
|
|
229
|
+
accessibilityState: { disabled, expanded: open },
|
|
230
|
+
};
|
|
231
|
+
if (theme.isAndroid) {
|
|
232
|
+
return _jsx(AndroidPart, { shared: shared, kind: kind, first: first, open: open, setOpen: setOpen, shown: shown, filled: text != null, a11y: a11y, partID: partID });
|
|
233
|
+
}
|
|
234
|
+
// iOS: the capsule, and the popover it opens
|
|
235
|
+
const calendarWidth = 7 * 44 + 2 * IOS_CALENDAR_INSET;
|
|
236
|
+
// At most 32 from the screen's edge, as the compact picker's popover stands (less on a narrow screen)
|
|
237
|
+
const edge = Math.max(8, Math.min(32, (screen - calendarWidth) / 2));
|
|
238
|
+
// The popover's wheels: a 24-hour time 75 apart (iOS 27.2's compact time popover), a 12-hour one as the wheels
|
|
239
|
+
const columns = kind === "time" ? timeColumns(shared, value ?? clampDate(new Date(), shared.min, shared.max), shared.set).map((column) => (hour12 ? column : { ...column, width: 75 })) : [];
|
|
240
|
+
const wheelsWidth = Math.max(233, 40 + columns.reduce((sum, column) => sum + column.width, 0));
|
|
241
|
+
return (_jsxs(Popover, { open: open, onOpenChange: (next) => setOpen(next && !disabled), children: [_jsx(Popover.Trigger, { children: _jsx(Pressable, { ...a11y, disabled: disabled, ...rowHold(hold), testID: partID, style: ({ pressed }) => [
|
|
242
|
+
styles.capsule,
|
|
243
|
+
disabled
|
|
244
|
+
? null
|
|
245
|
+
: { backgroundColor: theme.color("--color-background-secondary-soft-alpha", withAlpha(theme.colors.text, 0.07)), opacity: pressed ? 0.6 : 1 },
|
|
246
|
+
], children: _jsx(Text, { numberOfLines: 1, color: disabled || text == null ? theme.colors.labelTertiary : open ? theme.colors.textAccent : theme.colors.text, style: styles.capsuleText, tabularNums: kind === "time", children: shown }) }) }), _jsx(Popover.Content, { width: kind === "date" ? calendarWidth : wheelsWidth, minWidth: "auto", align: row ? "end" : "center", alignOffset: 0, collisionPadding: { left: edge, right: edge, top: 20, bottom: 20 }, "aria-label": kind === "date" ? shared.dateTitle : shared.timeTitle, testID: partID ? `${partID}-popover` : undefined, style: kind === "date" ? { padding: IOS_CALENDAR_INSET } : { padding: 0 }, children: kind === "date" ? (_jsx(Calendar, { value: value, onSelect: (day) => {
|
|
247
|
+
shared.set(withDay(value, day));
|
|
248
|
+
setOpen(false);
|
|
249
|
+
}, min: shared.min, max: shared.max, locale: locale, weekStartsOn: shared.weekStartsOn, width: 7 * 44, testID: partID ? `${partID}-calendar` : undefined })) : (_jsx(Wheels, { columns: columns, testID: partID ? `${partID}-wheels` : undefined })) })] }));
|
|
250
|
+
}
|
|
251
|
+
/** Android: a row's value in the accent, or an outlined field with its icon; either opens Material's dialog. */
|
|
252
|
+
function AndroidPart({ shared, kind, first, open, setOpen, shown, filled, a11y, partID }) {
|
|
253
|
+
const theme = useTheme();
|
|
254
|
+
const m = theme.material;
|
|
255
|
+
const row = useContext(RowEndContext);
|
|
256
|
+
const hold = useContext(RowHoldContext);
|
|
257
|
+
const { disabled } = shared;
|
|
258
|
+
// In a row the first part takes the row's press, as an Android value row opens its picker from anywhere on it
|
|
259
|
+
const handRow = row != null && first && !disabled ? row.press : undefined;
|
|
260
|
+
useEffect(() => {
|
|
261
|
+
if (!handRow)
|
|
262
|
+
return;
|
|
263
|
+
handRow(() => setOpen(true));
|
|
264
|
+
return () => handRow(null);
|
|
265
|
+
}, [handRow, setOpen]);
|
|
266
|
+
const look = fieldLook(theme, { variant: "outline", size: "md", pill: false }, { focused: open, filled, disabled, invalid: false });
|
|
267
|
+
const Icon = kind === "date" ? MaterialCalendarToday : MaterialSchedule;
|
|
268
|
+
return (_jsxs(_Fragment, { children: [row != null ? (_jsx(Pressable, { ...a11y, disabled: disabled, ...rowHold(hold), onPress: () => setOpen(true), hitSlop: ROW_SLOP, testID: partID, children: _jsx(Text, { numberOfLines: 1, color: withAlpha(filled ? m.primary : withAlpha(m.onSurface, 0.35), disabled ? 0.5 : 1), style: styles.androidValue, tabularNums: kind === "time", children: shown }) })) : (_jsxs(Pressable, { ...a11y, disabled: disabled, onPress: () => setOpen(true), testID: partID, style: [styles.field, look.box, { gap: look.gap, paddingEnd: 12 }], children: [_jsx(FieldOutline, { frame: look.frame }), _jsx(Text, { numberOfLines: 1, color: filled ? look.text.color : look.placeholder, style: [styles.grow, { fontSize: look.text.fontSize, lineHeight: look.text.lineHeight }], children: shown }), _jsx(Icon, { size: 24, color: look.trailingIcon })] })), kind === "date" ? _jsx(AndroidDateDialog, { shared: shared, open: open, onClose: () => setOpen(false), partID: partID }) : _jsx(AndroidTimeDialog, { shared: shared, open: open, onClose: () => setOpen(false), partID: partID })] }));
|
|
269
|
+
}
|
|
270
|
+
/** Material 3's date picker dialog: "Select date", the headline date, a line, then the month; OK applies the day. */
|
|
271
|
+
function AndroidDateDialog({ shared, open, onClose, partID }) {
|
|
272
|
+
const theme = useTheme();
|
|
273
|
+
const m = theme.material;
|
|
274
|
+
const { value, locale } = shared;
|
|
275
|
+
const [draft, setDraft] = useState(value);
|
|
276
|
+
const [wasOpen, setWasOpen] = useState(open);
|
|
277
|
+
if (wasOpen !== open) {
|
|
278
|
+
setWasOpen(open);
|
|
279
|
+
if (open)
|
|
280
|
+
setDraft(value);
|
|
281
|
+
}
|
|
282
|
+
const width = Math.min(useWindowDimensions().width - 48, 356);
|
|
283
|
+
return (_jsxs(PickerDialog, { open: open, onClose: onClose, onOk: () => {
|
|
284
|
+
if (draft)
|
|
285
|
+
shared.set(withDay(value, draft));
|
|
286
|
+
}, cancelLabel: shared.cancelLabel, okLabel: shared.okLabel, label: shared.dateTitle, testID: partID ? `${partID}-dialog` : undefined, children: [_jsxs(View, { style: styles.androidHeader, children: [_jsx(Text, { color: m.onSurfaceVariant, style: [styles.androidSupporting, theme.font("medium")], children: shared.dateTitle }), _jsx(Text, { color: m.onSurfaceVariant, style: styles.androidHeadline, accessibilityLiveRegion: "polite", children: draft ? formatHeadline(draft, locale) : shared.placeholder.date })] }), _jsx(View, { style: { height: 1, backgroundColor: m.outlineVariant } }), _jsx(View, { style: styles.androidCalendar, children: _jsx(Calendar, { value: draft, onSelect: setDraft, min: shared.min, max: shared.max, locale: locale, weekStartsOn: shared.weekStartsOn, width: width - 24, testID: partID ? `${partID}-calendar` : undefined }) })] }));
|
|
287
|
+
}
|
|
288
|
+
/** Material 3's time picker dialog: the dial; OK applies the time. */
|
|
289
|
+
function AndroidTimeDialog({ shared, open, onClose, partID }) {
|
|
290
|
+
const { value } = shared;
|
|
291
|
+
const now = value ?? new Date();
|
|
292
|
+
const [draft, setDraft] = useState({ hours: now.getHours(), minutes: now.getMinutes() });
|
|
293
|
+
const [wasOpen, setWasOpen] = useState(open);
|
|
294
|
+
if (wasOpen !== open) {
|
|
295
|
+
setWasOpen(open);
|
|
296
|
+
if (open)
|
|
297
|
+
setDraft({ hours: now.getHours(), minutes: now.getMinutes() });
|
|
298
|
+
}
|
|
299
|
+
return (_jsx(PickerDialog, { open: open, onClose: onClose, onOk: () => shared.set(withTime(value, draft.hours, draft.minutes)), cancelLabel: shared.cancelLabel, okLabel: shared.okLabel, label: shared.timeTitle, testID: partID ? `${partID}-dialog` : undefined, children: _jsx(TimeDial, { hours: draft.hours, minutes: draft.minutes, onChange: (hours, minutes) => setDraft({ hours, minutes }), hour12: shared.hour12, locale: shared.locale, title: shared.timeTitle, testID: partID ? `${partID}-dial` : undefined }) }));
|
|
300
|
+
}
|
|
301
|
+
/** The calendar in place; with the time, a Time row under it whose value opens the time's picker. */
|
|
302
|
+
function InlinePicker({ shared, mode, timeLabel, style }) {
|
|
303
|
+
const theme = useTheme();
|
|
304
|
+
const [width, setWidth] = useState(0);
|
|
305
|
+
const { value, testID } = shared;
|
|
306
|
+
const android = theme.isAndroid;
|
|
307
|
+
const timeRow = mode === "datetime" ? (
|
|
308
|
+
// iOS 27.2's inline picker with the time (dates-uikit-inline-time): no line over the row, its centre 51 under the
|
|
309
|
+
// last week's, the capsule 11 above the card's edge, the label Semibold; Android a row under a line
|
|
310
|
+
_jsxs(View, { style: [styles.timeRow, android ? [styles.androidTimeRow, { borderTopColor: theme.material.outlineVariant }] : styles.iosTimeRow], children: [_jsx(Text, { style: android ? styles.androidRowLabel : [styles.rowLabel, theme.font("semibold")], color: android ? theme.material.onSurface : theme.colors.text, children: timeLabel }), _jsx(RowEndContext.Provider, { value: { fill: () => { } }, children: _jsx(Part, { shared: { ...shared, ariaLabel: timeLabel }, kind: "time", first: false }) })] })) : null;
|
|
311
|
+
return (_jsxs(View, { style: [styles.inline, android ? null : styles.iosInline, mode === "datetime" && !android ? { paddingBottom: 8 } : null, style, { opacity: shared.disabled ? 0.4 : 1 }], pointerEvents: shared.disabled ? "none" : "auto", onLayout: (event) => setWidth(event.nativeEvent.layout.width), testID: testID, children: [width > 0 ? (_jsx(View, { style: android ? null : { paddingHorizontal: IOS_CALENDAR_INSET }, children: _jsx(Calendar, { value: value, onSelect: (day) => shared.set(withDay(value, day)), disabled: shared.disabled, min: shared.min, max: shared.max, locale: shared.locale, weekStartsOn: shared.weekStartsOn, width: android ? width : width - 2 * IOS_CALENDAR_INSET, testID: testID ? `${testID}-calendar` : undefined }) })) : null, timeRow] }));
|
|
312
|
+
}
|
|
313
|
+
const ROW_SLOP = { top: 15, bottom: 15, left: 8, right: 8 };
|
|
314
|
+
const styles = StyleSheet.create({
|
|
315
|
+
parts: { flexDirection: "row", alignItems: "center", gap: 8, flexShrink: 1 },
|
|
316
|
+
atEnd: { flexGrow: 1, justifyContent: "flex-end" },
|
|
317
|
+
capsule: { height: 34, borderRadius: 17, paddingHorizontal: 12, justifyContent: "center" },
|
|
318
|
+
capsuleText: { fontSize: 17, lineHeight: 22 },
|
|
319
|
+
androidValue: { fontSize: 16, lineHeight: 20 },
|
|
320
|
+
field: { flexDirection: "row", alignItems: "center", flexGrow: 1, minWidth: 140 },
|
|
321
|
+
grow: { flex: 1, minWidth: 0 },
|
|
322
|
+
androidHeader: { paddingStart: 24, paddingEnd: 12, paddingTop: 16, paddingBottom: 12 },
|
|
323
|
+
androidSupporting: { fontSize: 14, lineHeight: 20, letterSpacing: 0.1 },
|
|
324
|
+
androidHeadline: { fontSize: 32, lineHeight: 40, marginTop: 36 },
|
|
325
|
+
androidCalendar: { paddingHorizontal: 12, paddingBottom: 8 },
|
|
326
|
+
inline: { alignSelf: "stretch" },
|
|
327
|
+
timeRow: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 16 },
|
|
328
|
+
iosTimeRow: { minHeight: 40, marginTop: 9 },
|
|
329
|
+
androidTimeRow: { minHeight: 52, marginTop: 8, borderTopWidth: StyleSheet.hairlineWidth },
|
|
330
|
+
// 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
|
|
331
|
+
// capsule) 11 above the bottom
|
|
332
|
+
iosInline: { paddingTop: 12, paddingBottom: 11 },
|
|
333
|
+
rowLabel: { fontSize: 17, lineHeight: 22 },
|
|
334
|
+
androidRowLabel: { fontSize: 16, lineHeight: 24 },
|
|
335
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useRef, useState } from "react";
|
|
3
|
+
import { Platform, Pressable, StyleSheet, View } from "react-native";
|
|
4
|
+
import Svg, { Circle, Line } from "react-native-svg";
|
|
5
|
+
import { useTheme } from "../../theme/ThemeProvider";
|
|
6
|
+
import { Text } from "../Text/Text";
|
|
7
|
+
import { pad2, periodLabels } from "./dates";
|
|
8
|
+
const DIAL = 256;
|
|
9
|
+
const OUTER = 101;
|
|
10
|
+
const INNER = 69;
|
|
11
|
+
const KNOB = 48;
|
|
12
|
+
/**
|
|
13
|
+
* Android's time picker dial (Material 3 TimePicker): "Select time" in Medium 12, the hour and the minute in Regular
|
|
14
|
+
* 57 boxes 96 by 80 (the one being set in the primary container, the other in the highest surface container), AM and
|
|
15
|
+
* 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
|
|
16
|
+
* 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
|
|
17
|
+
* hour, then it turns to the minutes.
|
|
18
|
+
*/
|
|
19
|
+
export function TimeDial({ hours, minutes, onChange, hour12, locale, title, hoursLabel = "Hours", minutesLabel = "Minutes", testID }) {
|
|
20
|
+
const theme = useTheme();
|
|
21
|
+
const m = theme.material;
|
|
22
|
+
const [part, setPart] = useState("hours");
|
|
23
|
+
const pm = hours >= 12;
|
|
24
|
+
const [am, pmLabel] = periodLabels(locale);
|
|
25
|
+
const shownHour = hour12 ? (hours % 12 === 0 ? 12 : hours % 12) : hours;
|
|
26
|
+
const center = DIAL / 2;
|
|
27
|
+
// The value under the knob: its angle from 12 o'clock and its ring
|
|
28
|
+
const inner = part === "hours" && !hour12 && (hours === 0 || hours > 12);
|
|
29
|
+
const angle = part === "hours" ? ((hours % 12) / 12) * 360 : (minutes / 60) * 360;
|
|
30
|
+
const radius = inner ? INNER : OUTER;
|
|
31
|
+
const knob = { x: center + radius * Math.sin((angle * Math.PI) / 180), y: center - radius * Math.cos((angle * Math.PI) / 180) };
|
|
32
|
+
const dragging = useRef(false);
|
|
33
|
+
const pick = (event, done) => {
|
|
34
|
+
const x = event.nativeEvent.locationX - center;
|
|
35
|
+
const y = event.nativeEvent.locationY - center;
|
|
36
|
+
let degrees = (Math.atan2(x, -y) * 180) / Math.PI;
|
|
37
|
+
if (degrees < 0)
|
|
38
|
+
degrees += 360;
|
|
39
|
+
if (part === "hours") {
|
|
40
|
+
const slot = Math.round(degrees / 30) % 12;
|
|
41
|
+
const ringInner = !hour12 && Math.hypot(x, y) < (OUTER + INNER) / 2;
|
|
42
|
+
// Outer ring 12, 1…11; inner 00, 13…23 (Compose's 24-hour dial); a 12-hour clock keeps its half of the day
|
|
43
|
+
const next = hour12 ? (slot % 12) + (pm ? 12 : 0) : ringInner ? (slot === 0 ? 0 : slot + 12) : slot === 0 ? 12 : slot;
|
|
44
|
+
if (next !== hours)
|
|
45
|
+
onChange(next, minutes);
|
|
46
|
+
if (done)
|
|
47
|
+
setPart("minutes");
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const next = Math.round(degrees / 6) % 60;
|
|
51
|
+
if (next !== minutes)
|
|
52
|
+
onChange(hours, next);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const numbers = part === "minutes"
|
|
56
|
+
? Array.from({ length: 12 }, (_, slot) => ({ label: pad2(slot * 5), slot, ring: OUTER, value: slot * 5 }))
|
|
57
|
+
: [
|
|
58
|
+
...Array.from({ length: 12 }, (_, slot) => ({ label: String(slot === 0 ? 12 : slot), slot, ring: OUTER, value: slot === 0 ? 12 : slot })),
|
|
59
|
+
...(hour12 ? [] : Array.from({ length: 12 }, (_, slot) => ({ label: slot === 0 ? "00" : String(slot + 12), slot, ring: INNER, value: slot === 0 ? 0 : slot + 12 }))),
|
|
60
|
+
];
|
|
61
|
+
const chosenValue = part === "hours" ? (hour12 ? shownHour : hours) : minutes;
|
|
62
|
+
const box = (which) => {
|
|
63
|
+
const active = part === which;
|
|
64
|
+
return (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: `${which === "hours" ? hoursLabel : minutesLabel}, ${which === "hours" ? shownHour : pad2(minutes)}`, accessibilityState: { selected: active }, onPress: () => setPart(which), testID: testID ? `${testID}-${which}` : undefined, style: [styles.box, { backgroundColor: active ? m.primaryContainer : m.surfaceContainerHighest }], children: _jsx(Text, { color: active ? m.onPrimaryContainer : m.onSurface, style: styles.boxText, tabularNums: true, children: which === "hours" ? (hour12 ? String(shownHour) : pad2(hours)) : pad2(minutes) }) }));
|
|
65
|
+
};
|
|
66
|
+
const period = (afternoon) => {
|
|
67
|
+
const active = pm === afternoon;
|
|
68
|
+
return (_jsx(Pressable, { accessibilityRole: "button", accessibilityState: { selected: active }, onPress: () => {
|
|
69
|
+
if (active)
|
|
70
|
+
return;
|
|
71
|
+
onChange(afternoon ? hours + 12 : hours - 12, minutes);
|
|
72
|
+
}, style: [styles.period, active ? { backgroundColor: m.tertiaryContainer } : null], children: _jsx(Text, { color: active ? m.onTertiaryContainer : m.onSurfaceVariant, style: [styles.periodText, theme.font("medium")], children: afternoon ? pmLabel : am }) }));
|
|
73
|
+
};
|
|
74
|
+
return (_jsxs(View, { style: styles.root, testID: testID, children: [_jsx(Text, { color: m.onSurfaceVariant, style: [styles.title, theme.font("medium")], children: title }), _jsxs(View, { style: styles.display, children: [box("hours"), _jsx(Text, { color: m.onSurface, style: styles.colon, children: ":" }), box("minutes"), hour12 ? (_jsxs(View, { style: [styles.periods, { borderColor: m.outline }], children: [period(false), _jsx(View, { style: { height: 1, backgroundColor: m.outline } }), period(true)] })) : null] }), _jsxs(View, { accessible: true, accessibilityRole: "adjustable", accessibilityLabel: part === "hours" ? hoursLabel : minutesLabel, accessibilityValue: { text: part === "hours" ? String(shownHour) : pad2(minutes) }, accessibilityActions: [{ name: "increment" }, { name: "decrement" }], onAccessibilityAction: (event) => {
|
|
75
|
+
const up = event.nativeEvent.actionName === "increment" ? 1 : -1;
|
|
76
|
+
if (part === "hours")
|
|
77
|
+
onChange((hours + up + 24) % 24, minutes);
|
|
78
|
+
else
|
|
79
|
+
onChange(hours, (minutes + up + 60) % 60);
|
|
80
|
+
}, ...(Platform.OS === "web"
|
|
81
|
+
? {
|
|
82
|
+
tabIndex: 0,
|
|
83
|
+
onKeyDown: (event) => {
|
|
84
|
+
const up = event.key === "ArrowUp" || event.key === "ArrowRight" ? 1 : event.key === "ArrowDown" || event.key === "ArrowLeft" ? -1 : 0;
|
|
85
|
+
if (event.key === "Enter" && part === "hours")
|
|
86
|
+
setPart("minutes");
|
|
87
|
+
else if (!up)
|
|
88
|
+
return;
|
|
89
|
+
else if (part === "hours")
|
|
90
|
+
onChange((hours + up + 24) % 24, minutes);
|
|
91
|
+
else
|
|
92
|
+
onChange(hours, (minutes + up + 60) % 60);
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
: null), onStartShouldSetResponder: () => true, onMoveShouldSetResponder: () => true, onResponderGrant: (event) => {
|
|
97
|
+
dragging.current = true;
|
|
98
|
+
pick(event, false);
|
|
99
|
+
}, onResponderMove: (event) => pick(event, false), onResponderRelease: (event) => {
|
|
100
|
+
dragging.current = false;
|
|
101
|
+
pick(event, true);
|
|
102
|
+
}, testID: testID ? `${testID}-dial` : undefined, style: [styles.dial, { backgroundColor: m.surfaceContainerHighest }], children: [_jsxs(Svg, { width: DIAL, height: DIAL, style: StyleSheet.absoluteFill, pointerEvents: "none", children: [_jsx(Line, { x1: center, y1: center, x2: knob.x, y2: knob.y, stroke: m.primary, strokeWidth: 2 }), _jsx(Circle, { cx: center, cy: center, r: 4, fill: m.primary }), _jsx(Circle, { cx: knob.x, cy: knob.y, r: KNOB / 2, fill: m.primary })] }), numbers.map(({ label, slot, ring, value }) => {
|
|
103
|
+
const x = center + ring * Math.sin((slot * 30 * Math.PI) / 180);
|
|
104
|
+
const y = center - ring * Math.cos((slot * 30 * Math.PI) / 180);
|
|
105
|
+
const chosen = value === chosenValue && ring === radius;
|
|
106
|
+
return (_jsx(View, { pointerEvents: "none", style: [styles.number, { left: x - KNOB / 2, top: y - KNOB / 2 }], children: _jsx(Text, { color: chosen ? m.onPrimary : m.onSurface, style: ring === INNER ? styles.innerText : styles.numberText, children: label }) }, `${ring}-${slot}`));
|
|
107
|
+
})] })] }));
|
|
108
|
+
}
|
|
109
|
+
const styles = StyleSheet.create({
|
|
110
|
+
root: { paddingHorizontal: 24, paddingTop: 24, alignItems: "center" },
|
|
111
|
+
title: { alignSelf: "flex-start", fontSize: 12, lineHeight: 16, letterSpacing: 0.5, marginBottom: 20 },
|
|
112
|
+
display: { flexDirection: "row", alignItems: "center", height: 80 },
|
|
113
|
+
box: { width: 96, height: 80, borderRadius: 8, alignItems: "center", justifyContent: "center" },
|
|
114
|
+
boxText: { fontSize: 57, lineHeight: 64 },
|
|
115
|
+
colon: { width: 24, textAlign: "center", fontSize: 57, lineHeight: 64 },
|
|
116
|
+
periods: { width: 52, height: 80, marginStart: 12, borderWidth: 1, borderRadius: 8, overflow: "hidden" },
|
|
117
|
+
period: { flex: 1, alignItems: "center", justifyContent: "center" },
|
|
118
|
+
periodText: { fontSize: 16, lineHeight: 24, letterSpacing: 0.15 },
|
|
119
|
+
dial: { width: DIAL, height: DIAL, borderRadius: DIAL / 2, marginTop: 36 },
|
|
120
|
+
number: { position: "absolute", width: KNOB, height: KNOB, alignItems: "center", justifyContent: "center" },
|
|
121
|
+
numberText: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5 },
|
|
122
|
+
innerText: { fontSize: 14, lineHeight: 20, letterSpacing: 0.25 },
|
|
123
|
+
});
|