Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SecretTextInput.android.tsx

87 lines3.2 KB

1import { requireNativeView } from "expo";
2import { useState, type Ref } from "react";
3import { processColor, View, type NativeSyntheticEvent, type ViewProps } from "react-native";
4​
5import { cn } from "../../lib/cn";
6import { flattenThemeColor } from "../../lib/mobileTheme";
7import { useUniwindTheme } from "../../lib/useUniwindTheme";
8import { SecretRevealButton } from "./SecretRevealButton";
9import type { SecretTextInputProps } from "./SecretTextInput.types";
10​
11interface NativeSecretInputRef {
12 focus: () => Promise<void>;
14​
15interface NativeSecretInputProps extends ViewProps {
16 readonly ref?: Ref<NativeSecretInputRef>;
17 readonly inputLabel: string;
18 readonly value: string;
19 readonly placeholder: string;
20 readonly textColor: number;
21 readonly placeholderColor: number;
22 readonly cursorColor: number;
23 readonly fontSize: number;
24 readonly editable: boolean;
25 readonly revealed: boolean;
26 readonly returnKey: "next" | "done";
27 readonly onChangeText: (event: NativeSyntheticEvent<{ readonly value: string }>) => void;
28 readonly onSubmit: (event: NativeSyntheticEvent<{ readonly value: string }>) => void;
30​
31const NativeSecretInput = requireNativeView<NativeSecretInputProps>("T3SecretInput");
32​
33const FONT_SIZE = 16;
34const LINE_HEIGHT = 24;
35​
36function nativeColor(value: string) {
37 const color = processColor(value);
38 return typeof color === "number" ? color : 0;
40​
41/**
42 * Android's React Native TextInput cannot opt out of credential autofill, so password managers
43 * offer logins above the keyboard. This field is a native EditText that reports no autofill type.
44 * The frame matches AppTextInput's.
45 */
46export function SecretTextInput({ onMount, secret = true, ...props }: SecretTextInputProps) {
47 const theme = useUniwindTheme();
48 const surface = theme["--color-input"];
49 const [revealed, setRevealed] = useState(false);
50 return (
51 <View
52 className={cn(
53 "min-h-13.5 flex-row items-center gap-1 rounded-2xl border border-input-border bg-input pl-3.5",
54 secret ? "pr-1.5" : "pr-3.5",
55 )}
56 >
57 <NativeSecretInput
58 ref={
59 onMount === undefined
60 ? undefined
61 : (native: NativeSecretInputRef | null) =>
62 onMount(
63 native === null
64 ? null
65 : { focus: () => void native.focus().catch(() => undefined) },
66 )
67 }
68 inputLabel={props.accessibilityLabel}
69 value={props.value}
70 placeholder={props.placeholder}
71 textColor={nativeColor(flattenThemeColor(theme["--color-foreground"], surface))}
72 placeholderColor={nativeColor(flattenThemeColor(theme["--color-placeholder"], surface))}
73 cursorColor={nativeColor(flattenThemeColor(theme["--color-focus"], surface))}
74 fontSize={FONT_SIZE}
75 editable={props.editable}
76 revealed={!secret || revealed}
77 returnKey={props.returnKeyType ?? "done"}
78 onChangeText={(event) => props.onChangeText(event.nativeEvent.value)}
79 onSubmit={(event) => props.onSubmitEditing(event.nativeEvent.value)}
80 style={{ flex: 1, height: LINE_HEIGHT }}
81 />
82 {secret ? (
83 <SecretRevealButton revealed={revealed} onPress={() => setRevealed(!revealed)} />
84 ) : null}
85 </View>
86 );