@marktiderman/genesis-ui-native 3.11.0 → 3.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -0
- package/dist/{chunk-ZUR4PK77.js → chunk-47CDRP43.js} +45 -166
- package/dist/chunk-47CDRP43.js.map +1 -0
- package/dist/{chunk-SG7LYX7Z.js → chunk-BQYT6TH2.js} +14 -6
- package/dist/chunk-BQYT6TH2.js.map +1 -0
- package/dist/{chunk-EAHK7D7N.js → chunk-C2ARUGT7.js} +146 -42
- package/dist/chunk-C2ARUGT7.js.map +1 -0
- package/dist/{chunk-VEWSY4TZ.js → chunk-D4UU6KIF.js} +236 -21
- package/dist/chunk-D4UU6KIF.js.map +1 -0
- package/dist/{chunk-GGH3ZQNO.js → chunk-TUDJDIGC.js} +4 -4
- package/dist/{chunk-GGH3ZQNO.js.map → chunk-TUDJDIGC.js.map} +1 -1
- package/dist/{chunk-PMS2V46D.js → chunk-VHUUOH46.js} +2 -2
- package/dist/{chunk-PMS2V46D.js.map → chunk-VHUUOH46.js.map} +1 -1
- package/dist/{chunk-IWWXERKQ.js → chunk-X5CPLUPZ.js} +1236 -136
- package/dist/chunk-X5CPLUPZ.js.map +1 -0
- package/dist/components/index.d.ts +510 -25
- package/dist/components/index.js +5 -5
- package/dist/data/index.d.ts +167 -33
- package/dist/data/index.js +1082 -760
- package/dist/data/index.js.map +1 -1
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.js +1 -1
- package/dist/icons/lucide.d.ts +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.js +7 -7
- package/dist/layouts/index.d.ts +79 -6
- package/dist/layouts/index.js +5 -5
- package/dist/{native-modules-Bkm1jHO-.d.ts → native-modules-6JnyeGR-.d.ts} +55 -1
- package/package.json +70 -4
- package/src/components/__tests__/combobox.test.tsx +20 -0
- package/src/components/__tests__/number-input.test.tsx +112 -1
- package/src/components/__tests__/parity-controls.test.tsx +366 -0
- package/src/components/__tests__/parity-display.test.tsx +295 -0
- package/src/components/__tests__/twin-props.test.tsx +114 -0
- package/src/components/badge.tsx +3 -0
- package/src/components/button-group.tsx +50 -0
- package/src/components/calendar.tsx +183 -0
- package/src/components/carousel.tsx +175 -0
- package/src/components/chart.tsx +268 -0
- package/src/components/chip-group.tsx +34 -14
- package/src/components/choice-card.tsx +56 -19
- package/src/components/combobox.tsx +6 -5
- package/src/components/empty-state.tsx +40 -0
- package/src/components/flatten-children.ts +13 -0
- package/src/components/index.ts +49 -1
- package/src/components/inline-alert.tsx +3 -1
- package/src/components/input-group.tsx +99 -0
- package/src/components/input-otp.tsx +124 -0
- package/src/components/input.tsx +40 -12
- package/src/components/list-item.tsx +20 -4
- package/src/components/number-input.tsx +45 -15
- package/src/components/radio-group.tsx +5 -0
- package/src/components/resource.tsx +227 -0
- package/src/components/settings-row.tsx +194 -0
- package/src/components/slider.tsx +3 -9
- package/src/components/sortable-list.tsx +287 -0
- package/src/components/stat-card.tsx +106 -0
- package/src/components/stepper.tsx +4 -2
- package/src/components/widget-tile.tsx +66 -0
- package/src/components/workspace-switcher.tsx +217 -0
- package/src/data/NativeCustomView.tsx +117 -0
- package/src/data/NativeFormFields.tsx +312 -0
- package/src/data/NativeRecordPeek.tsx +130 -0
- package/src/data/NativeResourceFormField.tsx +32 -0
- package/src/data/__tests__/parity-data.test.tsx +350 -0
- package/src/data/index.ts +13 -0
- package/src/layouts/__tests__/keyboard-scroll.test.tsx +58 -2
- package/src/layouts/__tests__/notifications-page.test.tsx +12 -0
- package/src/layouts/index.ts +13 -0
- package/src/layouts/keyboard.tsx +22 -2
- package/src/layouts/notifications-page.tsx +13 -35
- package/src/layouts/pages.tsx +262 -0
- package/src/native-modules.tsx +37 -0
- package/dist/chunk-EAHK7D7N.js.map +0 -1
- package/dist/chunk-IWWXERKQ.js.map +0 -1
- package/dist/chunk-SG7LYX7Z.js.map +0 -1
- package/dist/chunk-VEWSY4TZ.js.map +0 -1
- package/dist/chunk-ZUR4PK77.js.map +0 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* InputGroup — a text field with content fixed inside its border: the twin of genesis-ui's
|
|
3
|
+
* InputGroup (InputGroupPrefix / InputGroupSuffix). It is NativeInput with `leading` and
|
|
4
|
+
* `trailing` (`prefix` and `suffix` here, as on the web), so it keeps the input's label, error
|
|
5
|
+
* and placeholder colour.
|
|
6
|
+
*
|
|
7
|
+
* PasswordToggleField — a password field with a Show / Hide button that keeps the typed text:
|
|
8
|
+
* the twin of genesis-ui's PasswordToggleField, on the same input group. Toggling keeps the cursor
|
|
9
|
+
* where it was (Android otherwise moves it to the start). On iOS, the system clears a hidden
|
|
10
|
+
* password field when typing resumes after it was shown and hidden again; that rule is the OS's
|
|
11
|
+
* (it guards against a shoulder-surfed edit), and the field follows it.
|
|
12
|
+
*
|
|
13
|
+
* @stability Beta
|
|
14
|
+
*/
|
|
15
|
+
import { useRef, useState, type ReactNode } from "react";
|
|
16
|
+
import { Pressable, Text } from "react-native";
|
|
17
|
+
import { NativeInput, type NativeInputProps } from "./input";
|
|
18
|
+
|
|
19
|
+
type Selection = { start: number; end: number };
|
|
20
|
+
|
|
21
|
+
export interface NativeInputGroupProps extends Omit<NativeInputProps, "leading" | "trailing"> {
|
|
22
|
+
/** Before the text: an icon, "$", "https://". */
|
|
23
|
+
prefix?: ReactNode;
|
|
24
|
+
/** After the text: a unit, a button. */
|
|
25
|
+
suffix?: ReactNode;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function NativeInputGroup({ prefix, suffix, ...props }: NativeInputGroupProps) {
|
|
29
|
+
const text = (node: ReactNode) =>
|
|
30
|
+
typeof node === "string" || typeof node === "number" ? <Text className="text-base text-muted-foreground">{node}</Text> : node;
|
|
31
|
+
return <NativeInput {...props} leading={prefix != null ? text(prefix) : undefined} trailing={suffix != null ? text(suffix) : undefined} />;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface NativePasswordToggleFieldProps extends Omit<NativeInputProps, "secureTextEntry" | "trailing"> {
|
|
35
|
+
/** Shown (uncontrolled start). Default false: the password is hidden. */
|
|
36
|
+
defaultVisible?: boolean;
|
|
37
|
+
/** Shown, to control it. */
|
|
38
|
+
visible?: boolean;
|
|
39
|
+
onVisibleChange?: (visible: boolean) => void;
|
|
40
|
+
/** The button's words. Default "Show password" / "Hide password" (read) and "Show" / "Hide" (shown). */
|
|
41
|
+
labels?: { show?: string; hide?: string; showAccessible?: string; hideAccessible?: string };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function NativePasswordToggleField({
|
|
45
|
+
defaultVisible = false,
|
|
46
|
+
visible: visibleProp,
|
|
47
|
+
onVisibleChange,
|
|
48
|
+
labels = {},
|
|
49
|
+
testID,
|
|
50
|
+
onSelectionChange,
|
|
51
|
+
onChangeText,
|
|
52
|
+
...props
|
|
53
|
+
}: NativePasswordToggleFieldProps) {
|
|
54
|
+
const [visibleState, setVisibleState] = useState(defaultVisible);
|
|
55
|
+
const visible = visibleProp ?? visibleState;
|
|
56
|
+
// The cursor, kept so a toggle can put it back; held only until the person moves it or types.
|
|
57
|
+
const selection = useRef<Selection | null>(null);
|
|
58
|
+
const [restore, setRestore] = useState<Selection | null>(null);
|
|
59
|
+
const toggle = () => {
|
|
60
|
+
if (selection.current) setRestore(selection.current);
|
|
61
|
+
if (visibleProp === undefined) setVisibleState(!visible);
|
|
62
|
+
onVisibleChange?.(!visible);
|
|
63
|
+
};
|
|
64
|
+
const id = testID ?? "native-password-field";
|
|
65
|
+
return (
|
|
66
|
+
<NativeInput
|
|
67
|
+
autoCapitalize="none"
|
|
68
|
+
autoCorrect={false}
|
|
69
|
+
textContentType="password"
|
|
70
|
+
autoComplete="password"
|
|
71
|
+
{...props}
|
|
72
|
+
testID={id}
|
|
73
|
+
secureTextEntry={!visible}
|
|
74
|
+
{...(restore ? { selection: restore } : {})}
|
|
75
|
+
onSelectionChange={(e) => {
|
|
76
|
+
selection.current = e.nativeEvent.selection;
|
|
77
|
+
if (restore && (e.nativeEvent.selection.start !== restore.start || e.nativeEvent.selection.end !== restore.end)) setRestore(null);
|
|
78
|
+
onSelectionChange?.(e);
|
|
79
|
+
}}
|
|
80
|
+
onChangeText={(text) => {
|
|
81
|
+
if (restore) setRestore(null);
|
|
82
|
+
onChangeText?.(text);
|
|
83
|
+
}}
|
|
84
|
+
trailing={
|
|
85
|
+
<Pressable
|
|
86
|
+
testID={`${id}-toggle`}
|
|
87
|
+
accessibilityRole="button"
|
|
88
|
+
// A plain button whose name says what it does next; no checked state, which VoiceOver reads as a checkbox's.
|
|
89
|
+
accessibilityLabel={visible ? (labels.hideAccessible ?? "Hide password") : (labels.showAccessible ?? "Show password")}
|
|
90
|
+
onPress={toggle}
|
|
91
|
+
hitSlop={8}
|
|
92
|
+
className="min-h-[36px] min-w-[44px] items-center justify-center px-2"
|
|
93
|
+
>
|
|
94
|
+
<Text className="text-sm font-medium text-primary-text">{visible ? (labels.hide ?? "Hide") : (labels.show ?? "Show")}</Text>
|
|
95
|
+
</Pressable>
|
|
96
|
+
}
|
|
97
|
+
/>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* InputOTP — a one-time code in boxes: the twin of genesis-ui's InputOTP.
|
|
3
|
+
*
|
|
4
|
+
* One real text field sits over the boxes, so the system's code autofill (iOS's "From Messages",
|
|
5
|
+
* Android's SMS retriever with `autoComplete="sms-otp"`), paste and the screen reader all work on
|
|
6
|
+
* a single field; the boxes only draw what it holds and are hidden from screen readers. The field
|
|
7
|
+
* is all but invisible (transparent text, no caret, 1% opacity) rather than at 0 opacity, which
|
|
8
|
+
* iOS drops from VoiceOver; it is the one labelled element, and a screen reader reads its text.
|
|
9
|
+
* The characters it keeps and the caret's box come from genesis-core's `sanitizeOtp` and
|
|
10
|
+
* `otpSlots`. The field has no native length cap: `sanitizeOtp` keeps the first `maxLength`
|
|
11
|
+
* characters after it strips spaces and dashes, so a pasted "123-456" stays whole. `pattern` is
|
|
12
|
+
* tested against the whole code, as the web's InputOTP (input-otp) tests it: a change the pattern
|
|
13
|
+
* refuses ("12a" in a digits field) leaves the code as it was, on both.
|
|
14
|
+
*
|
|
15
|
+
* @stability Beta
|
|
16
|
+
*/
|
|
17
|
+
import { useRef, useState } from "react";
|
|
18
|
+
import { Pressable, Text, TextInput, View } from "react-native";
|
|
19
|
+
import { OTP_PATTERN_DIGITS, otpComplete, otpSlots, sanitizeOtp } from "@marktiderman/genesis-core/headless";
|
|
20
|
+
import { cn } from "../utils";
|
|
21
|
+
|
|
22
|
+
export interface NativeInputOTPProps {
|
|
23
|
+
/** How many characters. Default 6. */
|
|
24
|
+
maxLength?: number;
|
|
25
|
+
value: string;
|
|
26
|
+
onChange: (value: string) => void;
|
|
27
|
+
/** Called once every box is filled. */
|
|
28
|
+
onComplete?: (value: string) => void;
|
|
29
|
+
/**
|
|
30
|
+
* What the code may hold, as a RegExp source tested against the whole code typed so far (as
|
|
31
|
+
* input-otp's `pattern`). Default digits (`OTP_PATTERN_DIGITS`, the same as input-otp's
|
|
32
|
+
* `REGEXP_ONLY_DIGITS`). Match a partial code too: "^[A-Z0-9]+$", not "^[A-Z0-9]{8}$".
|
|
33
|
+
*/
|
|
34
|
+
pattern?: string;
|
|
35
|
+
/** Draws a separator after these box indexes ([2] for 123-456). */
|
|
36
|
+
separatorAfter?: number[];
|
|
37
|
+
disabled?: boolean;
|
|
38
|
+
autoFocus?: boolean;
|
|
39
|
+
/** Read by screen readers. Default "One-time code". */
|
|
40
|
+
accessibilityLabel?: string;
|
|
41
|
+
/** Read after the label. Default "{maxLength} characters". */
|
|
42
|
+
accessibilityHint?: string;
|
|
43
|
+
className?: string;
|
|
44
|
+
testID?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** iOS leaves views at or under 0.01 opacity out of VoiceOver; just above it, the field stays reachable. */
|
|
48
|
+
const HIDDEN_BUT_READABLE = 0.011;
|
|
49
|
+
|
|
50
|
+
export function NativeInputOTP({
|
|
51
|
+
maxLength = 6,
|
|
52
|
+
value,
|
|
53
|
+
onChange,
|
|
54
|
+
onComplete,
|
|
55
|
+
pattern = OTP_PATTERN_DIGITS,
|
|
56
|
+
separatorAfter = [],
|
|
57
|
+
disabled,
|
|
58
|
+
autoFocus,
|
|
59
|
+
accessibilityLabel = "One-time code",
|
|
60
|
+
accessibilityHint,
|
|
61
|
+
className,
|
|
62
|
+
testID = "native-input-otp",
|
|
63
|
+
}: NativeInputOTPProps) {
|
|
64
|
+
const input = useRef<{ focus?: () => void } | null>(null);
|
|
65
|
+
const [focused, setFocused] = useState(false);
|
|
66
|
+
const digits = pattern === OTP_PATTERN_DIGITS;
|
|
67
|
+
const slots = otpSlots(value, maxLength, focused);
|
|
68
|
+
|
|
69
|
+
const change = (text: string) => {
|
|
70
|
+
const next = sanitizeOtp(text, maxLength, pattern);
|
|
71
|
+
// Refused by the pattern: the field is controlled, so it keeps the code it had.
|
|
72
|
+
if (next === null) return;
|
|
73
|
+
onChange(next);
|
|
74
|
+
if (otpComplete(next, maxLength) && !otpComplete(value, maxLength)) onComplete?.(next);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<Pressable
|
|
79
|
+
testID={testID}
|
|
80
|
+
accessible={false}
|
|
81
|
+
disabled={disabled}
|
|
82
|
+
onPress={() => input.current?.focus?.()}
|
|
83
|
+
className={cn("relative flex-row items-center self-start", disabled && "opacity-50", className)}
|
|
84
|
+
>
|
|
85
|
+
{slots.map((slot, i) => (
|
|
86
|
+
<View key={i} className="flex-row items-center" accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
|
|
87
|
+
<View
|
|
88
|
+
testID={`${testID}-slot-${i}`}
|
|
89
|
+
className={cn(
|
|
90
|
+
"h-12 w-10 items-center justify-center border-y border-r border-input bg-background",
|
|
91
|
+
(i === 0 || separatorAfter.includes(i - 1)) && "rounded-l-md border-l",
|
|
92
|
+
(i === maxLength - 1 || separatorAfter.includes(i)) && "rounded-r-md",
|
|
93
|
+
slot.isActive && "border-2 border-ring"
|
|
94
|
+
)}
|
|
95
|
+
>
|
|
96
|
+
<Text className="text-lg font-medium text-foreground">{slot.char ?? ""}</Text>
|
|
97
|
+
</View>
|
|
98
|
+
{separatorAfter.includes(i) && i < maxLength - 1 ? (
|
|
99
|
+
<Text className="mx-1 text-lg text-muted-foreground">·</Text>
|
|
100
|
+
) : null}
|
|
101
|
+
</View>
|
|
102
|
+
))}
|
|
103
|
+
<TextInput
|
|
104
|
+
ref={input}
|
|
105
|
+
testID={`${testID}-input`}
|
|
106
|
+
value={value}
|
|
107
|
+
onChangeText={change}
|
|
108
|
+
onFocus={() => setFocused(true)}
|
|
109
|
+
onBlur={() => setFocused(false)}
|
|
110
|
+
editable={!disabled}
|
|
111
|
+
autoFocus={autoFocus}
|
|
112
|
+
keyboardType={digits ? "number-pad" : "default"}
|
|
113
|
+
autoCapitalize="characters"
|
|
114
|
+
textContentType="oneTimeCode"
|
|
115
|
+
autoComplete={digits ? "sms-otp" : "one-time-code"}
|
|
116
|
+
accessibilityLabel={accessibilityLabel}
|
|
117
|
+
accessibilityHint={accessibilityHint ?? `${maxLength} characters`}
|
|
118
|
+
caretHidden
|
|
119
|
+
className="absolute inset-0 text-transparent"
|
|
120
|
+
style={{ opacity: HIDDEN_BUT_READABLE }}
|
|
121
|
+
/>
|
|
122
|
+
</Pressable>
|
|
123
|
+
);
|
|
124
|
+
}
|
package/src/components/input.tsx
CHANGED
|
@@ -13,6 +13,13 @@ export interface NativeInputProps extends TextInputProps {
|
|
|
13
13
|
inputClassName?: string;
|
|
14
14
|
label?: string;
|
|
15
15
|
error?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Content fixed before the text, inside the field's border: an icon, a currency sign, "https://".
|
|
18
|
+
* With `leading` or `trailing` the field is an input group (NativeInputGroup).
|
|
19
|
+
*/
|
|
20
|
+
leading?: React.ReactNode;
|
|
21
|
+
/** Content fixed after the text, inside the field's border: a unit, a clear or show button. */
|
|
22
|
+
trailing?: React.ReactNode;
|
|
16
23
|
/** The underlying TextInput (React 19 passes `ref` as a prop). */
|
|
17
24
|
ref?: Ref<ComponentRef<typeof TextInput>> | undefined;
|
|
18
25
|
}
|
|
@@ -24,8 +31,26 @@ function NativeInput({
|
|
|
24
31
|
error,
|
|
25
32
|
editable = true,
|
|
26
33
|
placeholderTextColor,
|
|
34
|
+
leading,
|
|
35
|
+
trailing,
|
|
27
36
|
...props
|
|
28
37
|
}: NativeInputProps) {
|
|
38
|
+
const grouped = leading != null || trailing != null;
|
|
39
|
+
const input = (
|
|
40
|
+
<TextInput
|
|
41
|
+
className={cn(
|
|
42
|
+
"h-10 rounded-md border border-input bg-background px-3 text-base text-foreground",
|
|
43
|
+
!editable && !grouped && "opacity-50",
|
|
44
|
+
error && "border-destructive",
|
|
45
|
+
grouped && "h-full min-w-0 flex-1 border-0 bg-transparent",
|
|
46
|
+
inputClassName
|
|
47
|
+
)}
|
|
48
|
+
editable={editable}
|
|
49
|
+
placeholderTextColor={placeholderTextColor ?? nativeColors.neutral[400]}
|
|
50
|
+
accessibilityLabel={label}
|
|
51
|
+
{...props}
|
|
52
|
+
/>
|
|
53
|
+
);
|
|
29
54
|
return (
|
|
30
55
|
<View className={cn("gap-1.5", className)}>
|
|
31
56
|
{label ? (
|
|
@@ -33,18 +58,21 @@ function NativeInput({
|
|
|
33
58
|
{label}
|
|
34
59
|
</Text>
|
|
35
60
|
) : null}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
61
|
+
{grouped ? (
|
|
62
|
+
<View
|
|
63
|
+
className={cn(
|
|
64
|
+
"h-10 flex-row items-center rounded-md border border-input bg-background",
|
|
65
|
+
!editable && "opacity-50",
|
|
66
|
+
error && "border-destructive"
|
|
67
|
+
)}
|
|
68
|
+
>
|
|
69
|
+
{leading != null ? <View className="pl-3">{leading}</View> : null}
|
|
70
|
+
{input}
|
|
71
|
+
{trailing != null ? <View className="pr-1">{trailing}</View> : null}
|
|
72
|
+
</View>
|
|
73
|
+
) : (
|
|
74
|
+
input
|
|
75
|
+
)}
|
|
48
76
|
{error ? (
|
|
49
77
|
<Text className="text-sm text-destructive">{error}</Text>
|
|
50
78
|
) : null}
|
|
@@ -29,6 +29,19 @@ export interface NativeListItemProps {
|
|
|
29
29
|
/** When provided, the row becomes pressable. */
|
|
30
30
|
onPress?: () => void;
|
|
31
31
|
disabled?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* What a pressable row is to a screen reader. Default `button`; a row that flips its switch
|
|
34
|
+
* (NativeToggleRow) is a `switch`, and one choice of several (NativeWorkspaceSwitcher) a
|
|
35
|
+
* `radio`, each with `accessibilityState.checked`.
|
|
36
|
+
*/
|
|
37
|
+
accessibilityRole?: "button" | "switch" | "link" | "menuitem" | "radio";
|
|
38
|
+
/**
|
|
39
|
+
* What a screen reader reads for the row. Default its title and subtitle. The row is one
|
|
40
|
+
* element, so a `trailing` badge's own label is not read: put its words here too.
|
|
41
|
+
*/
|
|
42
|
+
accessibilityLabel?: string;
|
|
43
|
+
/** Merged over the row's own (`disabled`). */
|
|
44
|
+
accessibilityState?: { checked?: boolean; selected?: boolean };
|
|
32
45
|
className?: string;
|
|
33
46
|
testID?: string;
|
|
34
47
|
}
|
|
@@ -41,6 +54,9 @@ export function NativeListItem({
|
|
|
41
54
|
showChevron,
|
|
42
55
|
onPress,
|
|
43
56
|
disabled,
|
|
57
|
+
accessibilityRole = "button",
|
|
58
|
+
accessibilityState,
|
|
59
|
+
accessibilityLabel,
|
|
44
60
|
className,
|
|
45
61
|
testID,
|
|
46
62
|
}: NativeListItemProps) {
|
|
@@ -69,9 +85,9 @@ export function NativeListItem({
|
|
|
69
85
|
return (
|
|
70
86
|
<Pressable
|
|
71
87
|
testID={testID ?? "native-list-item"}
|
|
72
|
-
accessibilityRole=
|
|
73
|
-
accessibilityLabel={subtitle ? `${title}, ${subtitle}` : title}
|
|
74
|
-
accessibilityState={{ disabled }}
|
|
88
|
+
accessibilityRole={accessibilityRole}
|
|
89
|
+
accessibilityLabel={accessibilityLabel ?? (subtitle ? `${title}, ${subtitle}` : title)}
|
|
90
|
+
accessibilityState={{ disabled, ...accessibilityState }}
|
|
75
91
|
disabled={disabled}
|
|
76
92
|
onPress={onPress}
|
|
77
93
|
className={cn(
|
|
@@ -88,7 +104,7 @@ export function NativeListItem({
|
|
|
88
104
|
return (
|
|
89
105
|
<View
|
|
90
106
|
testID={testID ?? "native-list-item"}
|
|
91
|
-
accessibilityLabel={subtitle ? `${title}, ${subtitle}` : title}
|
|
107
|
+
accessibilityLabel={accessibilityLabel ?? (subtitle ? `${title}, ${subtitle}` : title)}
|
|
92
108
|
className={cn(
|
|
93
109
|
"min-h-[44px] flex-row items-center bg-background px-4 py-3",
|
|
94
110
|
className
|
|
@@ -11,8 +11,9 @@
|
|
|
11
11
|
*
|
|
12
12
|
* @stability Beta
|
|
13
13
|
*/
|
|
14
|
-
import { useCallback } from "react";
|
|
14
|
+
import { useCallback, useRef, useState } from "react";
|
|
15
15
|
import { Pressable, Text, TextInput, View } from "react-native";
|
|
16
|
+
import { canStepNumber, clampNumber, commitNumberDraft, readNumberDraft, stepNumber } from "@marktiderman/genesis-core/headless";
|
|
16
17
|
import { cn } from "../utils";
|
|
17
18
|
|
|
18
19
|
export interface NativeNumberInputProps {
|
|
@@ -24,6 +25,11 @@ export interface NativeNumberInputProps {
|
|
|
24
25
|
step?: number;
|
|
25
26
|
/** Decimal places shown. Default 0 (integer). */
|
|
26
27
|
precision?: number;
|
|
28
|
+
/**
|
|
29
|
+
* The locale whose marks typed numbers use ("de-DE" reads "1.234,5"). Default: worked out from
|
|
30
|
+
* the text ("1,234" is 1234, "1,5" is 1.5), genesis-core's `parseNumberInput`, as on the web.
|
|
31
|
+
*/
|
|
32
|
+
locale?: string;
|
|
27
33
|
/** Disable both stepper buttons and the input. */
|
|
28
34
|
disabled?: boolean;
|
|
29
35
|
/** Disable just the typed input; keep stepper. */
|
|
@@ -33,12 +39,6 @@ export interface NativeNumberInputProps {
|
|
|
33
39
|
testID?: string;
|
|
34
40
|
}
|
|
35
41
|
|
|
36
|
-
function clamp(v: number, min?: number, max?: number) {
|
|
37
|
-
if (min !== undefined && v < min) return min;
|
|
38
|
-
if (max !== undefined && v > max) return max;
|
|
39
|
-
return v;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
42
|
export function NativeNumberInput({
|
|
43
43
|
value,
|
|
44
44
|
onChange,
|
|
@@ -46,6 +46,7 @@ export function NativeNumberInput({
|
|
|
46
46
|
max,
|
|
47
47
|
step = 1,
|
|
48
48
|
precision = 0,
|
|
49
|
+
locale,
|
|
49
50
|
disabled,
|
|
50
51
|
readOnlyInput,
|
|
51
52
|
className,
|
|
@@ -55,13 +56,32 @@ export function NativeNumberInput({
|
|
|
55
56
|
const setValue = useCallback(
|
|
56
57
|
(next: number) => {
|
|
57
58
|
if (Number.isNaN(next)) return;
|
|
58
|
-
onChange(
|
|
59
|
+
onChange(clampNumber(next, min, max));
|
|
59
60
|
},
|
|
60
61
|
[max, min, onChange]
|
|
61
62
|
);
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
// What's typed: the field shows it, and a number that is ready (in range, within the precision,
|
|
65
|
+
// not a comma that may still become a group) is passed on as it is typed. Ending the edit (blur,
|
|
66
|
+
// submit, or the keyboard dismissed: FormPage's actions dismiss it before they run) commits what's
|
|
67
|
+
// typed, rounded and clamped, and shows the value again, as on the web. A ref keeps the draft so
|
|
68
|
+
// the edit's several ending events commit it once.
|
|
69
|
+
const [draft, setDraftState] = useState<string | null>(null);
|
|
70
|
+
const draftRef = useRef<string | null>(null);
|
|
71
|
+
const setDraft = (next: string | null) => {
|
|
72
|
+
draftRef.current = next;
|
|
73
|
+
setDraftState(next);
|
|
74
|
+
};
|
|
75
|
+
const commit = () => {
|
|
76
|
+
const text = draftRef.current;
|
|
77
|
+
if (text === null) return;
|
|
78
|
+
setDraft(null);
|
|
79
|
+
const typed = commitNumberDraft(text, { min, max, precision, locale });
|
|
80
|
+
if (typed !== null && typed !== value) onChange(typed);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const decBlocked = disabled || !canStepNumber(value, -1, { min, max });
|
|
84
|
+
const incBlocked = disabled || !canStepNumber(value, 1, { min, max });
|
|
65
85
|
|
|
66
86
|
return (
|
|
67
87
|
<View
|
|
@@ -78,7 +98,10 @@ export function NativeNumberInput({
|
|
|
78
98
|
accessibilityRole="button"
|
|
79
99
|
accessibilityLabel="Decrement"
|
|
80
100
|
disabled={decBlocked}
|
|
81
|
-
onPress={() =>
|
|
101
|
+
onPress={() => {
|
|
102
|
+
setDraft(null);
|
|
103
|
+
setValue(stepNumber(value, -1, { step, min, max }));
|
|
104
|
+
}}
|
|
82
105
|
className={cn(
|
|
83
106
|
"min-h-[44px] min-w-[44px] items-center justify-center px-3",
|
|
84
107
|
decBlocked && "opacity-40"
|
|
@@ -88,14 +111,18 @@ export function NativeNumberInput({
|
|
|
88
111
|
</Pressable>
|
|
89
112
|
<TextInput
|
|
90
113
|
testID={testID ? `${testID}-field` : "number-input-field"}
|
|
91
|
-
value={value.toFixed(precision)}
|
|
114
|
+
value={draft ?? value.toFixed(precision)}
|
|
92
115
|
editable={!disabled && !readOnlyInput}
|
|
93
116
|
keyboardType="numeric"
|
|
94
117
|
accessibilityLabel="Numeric value"
|
|
95
118
|
onChangeText={(t) => {
|
|
96
|
-
|
|
97
|
-
|
|
119
|
+
setDraft(t);
|
|
120
|
+
const typed = readNumberDraft(t, { min, max, precision, locale });
|
|
121
|
+
if (typed.ready && typed.value !== null && typed.value !== value) onChange(typed.value);
|
|
98
122
|
}}
|
|
123
|
+
onBlur={commit}
|
|
124
|
+
onEndEditing={commit}
|
|
125
|
+
onSubmitEditing={commit}
|
|
99
126
|
className={cn(
|
|
100
127
|
"min-w-[64px] flex-1 border-x border-input bg-background text-center text-base text-foreground",
|
|
101
128
|
inputClassName
|
|
@@ -106,7 +133,10 @@ export function NativeNumberInput({
|
|
|
106
133
|
accessibilityRole="button"
|
|
107
134
|
accessibilityLabel="Increment"
|
|
108
135
|
disabled={incBlocked}
|
|
109
|
-
onPress={() =>
|
|
136
|
+
onPress={() => {
|
|
137
|
+
setDraft(null);
|
|
138
|
+
setValue(stepNumber(value, 1, { step, min, max }));
|
|
139
|
+
}}
|
|
110
140
|
className={cn(
|
|
111
141
|
"min-h-[44px] min-w-[44px] items-center justify-center px-3",
|
|
112
142
|
incBlocked && "opacity-40"
|
|
@@ -21,6 +21,11 @@ function useRadioGroup() {
|
|
|
21
21
|
return context;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/** The group a radio item sits in, or null outside one (NativeChoiceCard reads it). */
|
|
25
|
+
export function useNativeRadioGroupContext(): RadioGroupContextValue | null {
|
|
26
|
+
return useContext(RadioGroupContext);
|
|
27
|
+
}
|
|
28
|
+
|
|
24
29
|
export interface NativeRadioGroupProps {
|
|
25
30
|
className?: string;
|
|
26
31
|
value?: string;
|