react-native-paper-login-template icon indicating copy to clipboard operation
react-native-paper-login-template copied to clipboard

How can I display the Show/Hide icon for the password in Textinput?

Open WO-Mehul opened this issue 5 years ago • 4 comments

I have a requirement for display password show/hide functionality. So, my question is how can I display an "eye" icon on the right side of "Textinput" for particular "Textinput"(only for password input not for others)

WO-Mehul avatar Jan 13 '21 02:01 WO-Mehul

Is there any update?

bczak avatar May 10 '21 18:05 bczak

Found the solution: Use TextInput.Icon inside TextInput component and use state to change visibility

bczak avatar May 11 '21 21:05 bczak

As @tashpjak said, you use TextInput.Icon. Here is an example:

import React, { useState } from "react";
import { TextInput } from "react-native-paper";

export default function MyComponent() {
  const [secureTextEntry, setSecureTextEntry] = useState(true);

  return (
    <TextInput
      label="Password"
      secureTextEntry={secureTextEntry}
      right={
        <TextInput.Icon
          name="eye"
          onPress={() => {
            setSecureTextEntry(!secureTextEntry);
            return false;
          }}
        />
      }
    />
  );
}

It's also worth noting that if you use it within a ScrollView, the keyboard may not function correctly. You may need to set keyboardShouldPersistTaps="handled" as described here: https://reactnative.dev/docs/scrollview#keyboardshouldpersisttaps

traviswimer avatar Dec 05 '21 20:12 traviswimer

keyboardShouldPersistTaps="handled"

Thanks man, this really helped me a lot

ricardoGlobant avatar Apr 13 '23 19:04 ricardoGlobant