Home > Mobile >  Adding an eye icon on the right side of the input field in react native
Adding an eye icon on the right side of the input field in react native

Time:05-26

I am working on a react native project. I have a password field on the Login page and I want to add an eye icon on the right end of the input field. My code currently looks like this:

  <View><TextInput placeholder='Enter your Password' autoCorrect={false} secureTextEntry={true} textContentType='password'></TextInput></View>

This is a normal input field with a placeholder text.

CodePudding user response:

Please find the below code:

const [password, setPassword] = useState('');  
const [isPasswordSecure, setIsPasswordSecure] = useState(true);


<View style={styles.viewPassword}>
                    <TextInput
                      secureTextEntry={isPasswordSecure}
                      style={styles.textInputStyle}
                      right={
                        <TextInput.Icon
                          name={() => <MaterialCommunityIcons name={isPasswordSecure ? "eye-off" : "eye"} size={28} color={COLORS.black} />} // where <Icon /> is any component from vector-icons or anything else
                          onPress={() => { isPasswordSecure ? setIsPasswordSecure(false) : setIsPasswordSecure(true) }}
                        />
                      }
                      fontFamily={FONTS.ROBOTO_REGULAR}
                      fontSize={Theme.FONT_18PX}
                      selectionColor={COLORS.orange}
                      underlineColor={COLORS.orange}
                      label={StringsConstants.Password}
                      value={password}
                      onChangeText={text => setPassword(text)}
                      underlineColorAndroid="transparent"
                      theme={{ colors: { text: COLORS.black, primary: COLORS.orange, placeholder: COLORS.black } }}
                    />
</View>

I hope it will help you!

CodePudding user response:

you can put it and style it like this

<View>
<TextInput placeholder='Enter your Password' autoCorrect={false} secureTextEntry={true} textContentType='password' />
<EyeIconOrImage style={{
position: 'absolute',
right: 20,
}} />
</View>
  • Related