The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { requireNativeView } from "expo"; |
| 2 | import { useState, type Ref } from "react"; |
| 3 | import { processColor, View, type NativeSyntheticEvent, type ViewProps } from "react-native"; |
| 4 | |
| 5 | import { cn } from "../../lib/cn"; |
| 6 | import { flattenThemeColor } from "../../lib/mobileTheme"; |
| 7 | import { useUniwindTheme } from "../../lib/useUniwindTheme"; |
| 8 | import { SecretRevealButton } from "./SecretRevealButton"; |
| 9 | import type { SecretTextInputProps } from "./SecretTextInput.types"; |
| 10 | |
| 11 | interface NativeSecretInputRef { |
| 12 | focus: () => Promise<void>; |
| 13 | } |
| 14 | |
| 15 | interface 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; |
| 29 | } |
| 30 | |
| 31 | const NativeSecretInput = requireNativeView<NativeSecretInputProps>("T3SecretInput"); |
| 32 | |
| 33 | const FONT_SIZE = 16; |
| 34 | const LINE_HEIGHT = 24; |
| 35 | |
| 36 | function nativeColor(value: string) { |
| 37 | const color = processColor(value); |
| 38 | return typeof color === "number" ? color : 0; |
| 39 | } |
| 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 | */ |
| 46 | export 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 | ); |
| 87 | } |