简体   繁体   中英

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

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.

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!

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM