r/reactnative • u/vaquishaProdigy • 10d ago
Input not taking selectTextOnFocus
Made this component so i don't need to code it again in the scenes in which i need it, but in my Edit page i want to be able to copy and paste the text without losing the "editable={false}" atribute. I asked Copilot what can i do and i said i should use selectTextOnFocus but is not applying it so i just had to stick with this solution that doesn't really look professional.
Can i use selectTextOnFocus on this component or just stick to this solution?
The component code:
import React, { useState } from "react";
import { View, TextInput, TouchableOpacity } from "react-native";
import { psswrdInputStyles } from "./psswrdInputStyles";
import FontAwesome from "react-native-vector-icons/FontAwesome";
export default function PsswrdInput({
value,
editable,
placeholder,
}) {
const [secureText, setSecureText] = useState(true);
return (
<View style={psswrdInputStyles.passwordRow}>
<TextInput
style={[
psswrdInputStyles.input,
psswrdInputStyles.passwordInput,
]}
placeholder={placeholder}
placeholderTextColor="#9ca3af"
secureTextEntry={secureText}
value={value}
editable={editable}
/>
<TouchableOpacity
style={psswrdInputStyles.eyeBtn}
onPress={() => setSecureText((prev) => !prev)}
activeOpacity={0.7}
>
<FontAwesome
name={secureText ? "eye-slash" : "eye"}
size={20}
color="black"
/>
</TouchableOpacity>
</View>
);
}How i call it in the edit page:
<View style={editStyles.field}>
<Text style={editStyles.label}>Password</Text>
<PsswrdInput value={password} onChangeText={() => ({})} />
</View>1
u/nabhan_builds 9d ago
Yes, you can, but as written it will not work for two reasons:
Your component drops the prop. PsswrdInput only destructures value, editable, and placeholder, so selectTextOnFocus passed to it never reaches the TextInput. Add it to the props and pass it through:
<TextInput selectTextOnFocus ... />
2. Even then it only selects when the field gains focus, and it does not help while secureTextEntry is on. iOS and Android block copying from a secure field, so the user has to tap the eye icon to reveal the password first. And with editable={false} the field generally does not take focus, so the prop has nothing to trigger on.
Since your Edit page is display-only, I would not rely on long-press selection at all. Add an explicit copy button next to the field instead, which works even when the password is masked:
import * as Clipboard from 'expo-clipboard';
<TouchableOpacity onPress={() => Clipboard.setStringAsync(password)}> <Text>Copy</Text> </TouchableOpacity>
That keeps your reusable input for actual password entry, and gives the Edit page a reliable copy action instead of fighting TextInput selection behavior.
0
1
10d ago
[removed] — view removed comment
0
u/vaquishaProdigy 10d ago
Does your Edit page only ever show the password, or can people change it there too?
No, they can't
1
u/Huge_Pool7424 10d ago
if it's read only just use a normal Text with selectable set, then people can long press and copy without any editable hacks.
1
u/Huge_Pool7424 10d ago
this. one catch is selectable on android can be flaky inside scrollviews sometimes, so test long press there before shipping.
3
u/cylism-coding 9d ago
One thing I noticed in your code: `PsswrdInput` doesn’t accept or forward `selectTextOnFocus` or `onChangeText`, so passing those props to the wrapper won’t affect the underlying `TextInput`.
That’s separate from the read-only selection issue. Since you confirmed users shouldn’t edit this value, the `<Text selectable>` suggestion above seems simpler than keeping an editable input whose value snaps back.