So, I am trying to create Registration and Login form separated by only a toggle switch inside a card and for that I declared two different useForm() instances.
const {control, handleSubmit, formState: { errors }, reset, getValues} = useForm({
resolver: yupResolver(validatorSchema),
defaultValues: {
'username':"",
'email':"",
'password':"",
'confirmPassword':""
}, mode:'onBlur'
})
const [addUser, {loading, error}] = useMutation(REGISTER_USER, {
update (proxy, result) {
console.log(result)
navigate('/home')
},
variables: {
username: getValues("username"),
email: getValues("email"),
password: getValues("password"),
role: dropdown
}
})
const submit = (data) => {
console.log(data)
addUser()
reset()
}
And
const {control: authControl, handleSubmit: onSubmit, reset: res, formState: { errors: errors2 },getValues: getAuthValues} = useForm({
resolver: yupResolver(loginValidatorSchema),
defaultValues: {
'loginUsername': "",
'loginPassword': ""
},
mode: 'all'
})
isSignup ? console.log(errors) : console.log(errors2)
const [loginUser] = useMutation(AUTHENTICATE_USER,{
update(_,result) {
navigate('/home')
},
variables: {
username: getAuthValues('loginUsername'),
password: getAuthValues('loginPassword')
}
})
const authSubmit = (data) => {
console.log(data)
loginUser()
res()
}
I'm using them inside a React native card in the following way-
const element = <TextInput.Icon name="lock-outline" />
return (
<SafeAreaView>
<ScrollView contentContainerStyle={Styles.container}>
<Image
style={
Styles.image
}
source={assets.kotaco}
/>
<Card containerStyle={{
borderRadius: "20px",
overflow: "hidden"
}}>
<Card.Title style={{color: '#dc1846'}}>{isSignup?'Sign up': 'Sign in'}</Card.Title>
<View style={{
marginLeft: "250px",
overflow: "hidden"
}}>
<Switch
onValueChange={toggleSwitch}
value={isSignup}
/>
</View>
{
isSignup ?
<View>
<Controller
control={control}
name="username"
render={({field: {onChange, value, onBlur}}) => (
<TextInput
placeholder=' username'
style={Styles.input}
value={value}
//left={<TextInput.Icon name="alert-circle" size={20} color={'red'} />}
left={element}
onBlur={onBlur}
onChangeText={(value) => onChange(value)}
autoFocus half
/>
)}
/>
<Controller
control={control}
name="email"
render={({field: {onChange, value, onBlur}}) => (
<TextInput
placeholder=' email'
style={Styles.input}
value={value}
onBlur={onBlur}
keyboardType='email-address'
onChangeText={(value) => onChange(value)}
autoFocus half
/>
)}
/>
<Controller
control={control}
name="password"
render={({field:{onChange, value, onBlur}}) => (
<TextInput
placeholder=' password'
style={Styles.input}
value={value}
onBlur={onBlur}
secureTextEntry={showPassword}
right={<TextInput.Icon name={showPassword ? "eye" : "eye-off"} onPress={() => setShowPassword(!showPassword)} />}
onChangeText={(value) => onChange(value)}
autoFocus half
/>
)}
/>
<Controller
control={control}
name="confirmPassword"
render={({field:{onChange, value, onBlur}}) => (
<TextInput
placeholder=' confirm password'
style={Styles.input}
value={value}
type='password'
onBlur={onBlur}
secureTextEntry={showPassword}
onChangeText={(value) => onChange(value)}
autoFocus half
/>
)}
/>
<View style={{flexDirection:"row", marginTop: 10, marginBottom: 10}}>
<Button
title="Sign up"
type="solid"
titleStyle={{ fontWeight: '700' }}
buttonStyle={{
backgroundColor: "rgba(199, 43, 98, 1)",
borderColor: 'transparent',
borderWidth: 0,
borderRadius: 30,
}}
containerStyle={{
width: 100,
marginRight: 50
}}
onPress={ handleSubmit(submit)}
/>
<Dropdown
style={Styles.dropdown}
iconStyle={Styles.icon}
data={roles}
labelField="label"
valueField="value"
placeholder=" Select a role"
value={dropdown}
onChange={item => setDropdown(item.value)}
renderLeftIcon={() => (
<Icon
style={Styles.icon}
color="#fc2d79"
name="person-circle-outline"
size={20}
/>
)}
/>
</View>
</View>
:
<View>
<Controller
control={authControl}
name="loginUsername"
render={({field:{onChange, value, onBlur}}) => (
<TextInput
placeholder=' username'
style={Styles.input}
value={value}
onBlur={onBlur}
onChangeText={(value) => onChange(value)}
autoFocus half
/>
)}
/>
<Controller
control={authControl}
name="loginPassword"
render={({field:{onChange, value, onBlur}}) => (
<TextInput
placeholder=' password'
style={Styles.input}
value={value}
type='password'
onBlur={onBlur}
secureTextEntry={showPassword}
onChangeText={(value) => onChange(value)}
autoFocus half
/>
)}
/>
<View style={{alignSelf:'center',marginTop: 15, marginBottom: 15}}>
<Button
title="Sign in"
type="solid"
titleStyle={{ fontWeight: '700' }}
buttonStyle={{
backgroundColor: "rgba(199, 43, 98, 1)",
borderColor: 'transparent',
borderWidth: 0,
borderRadius: 30,
}}
containerStyle={{
width: 100,
//marginRight: 50,
}}
onPress={onSubmit(authSubmit)}
/>
</View>
</View>
}
</Card>
</ScrollView>
</SafeAreaView>
)
}
While I'm able to type, validate and submit the data entered in the registration form, I am unable to type anything in the Login form, I changed the name of most of the props when I declared a second instance of useForm(), where am I going wrong?
The listStyle where I set the height of my list it's not working on Autocomplete.
I'm using react-native-autocomplete-input.
Check the Example:
<HideKeyboard>
<SafeAreaView style={mainStyle.container}>
<StatusBar style="light" backgroundColor="#0a3f88" />
<SimpleLineIcons style={mainStyle.menuIconHome} name="menu" size={28} color="black" onPress={() => {Keyboard.dismiss(), navigation.openDrawer()}}/>
<Image style={mainStyle.logo} source={logo}></Image>
<Autocomplete
style={{
backgroundColor:"transparent",
textAlign:"center",
}}
onChangeText={(text) => searchFilterFunction(text)}
data={filteredDataSource}
placeholder="Nome da empresa..."
autoFocus={true}
listStyle={{
maxHeight:20,
}}
containerStyle={{
paddingHorizontal:40,
position:"absolute",
top:"45%",
alignSelf:"center",
}}
inputContainerStyle={{
height:40,
zIndex:999,
}}
flatListProps={{
keyExtractor: (item, index) => index.toString(),
renderItem: ({ item }) =>
<TouchableOpacity
onPress={()=>{
navigation.navigate("Projeto",{
item:item,
});
}}
>
<View style={{
height:50,
borderBottomWidth:0.4,
}}>
<Text style={{
color:"#0a3f88",
fontWeight:"bold",
marginLeft:10,
}}>{item}</Text>
</View>
</TouchableOpacity>
}}
/>
<Image style={mainStyle.cmlagos} source={camaraLagos}></Image>
</SafeAreaView>
</HideKeyboard>
As you can see in the prinscreen below , the listStyle have no effect.
Already tried to use ScrollView outside of the Autocomplete but it's the same.
Any suggestions ?
Solved!
Instead of use the listStyle, we can use style:{height: 150} inside of flatListProps.
I'm trying to create a new simple app. While in this process, I've decided to start with a login page and a sign up page. These pages have the same styling, where the top third of the screen is a container with a logo in it, and the bottom two thirds is a form with input fields.
The problem that I'm stuck at now is that everything looks great, but when you press one of the inputs then the keyboard covers most of the inputs. I've done a little research and I've tried to apply both ScrollView and KeyboardAvoidingView, but neither of these seem to work properly.
This is how my page is set up:
<View style={styles.screen}>
{this.state.loading && (
<View style={styles.loading}>
<ActivityIndicator
color={primaryColor}
size="large"
/>
</View>
)}
<View style={styles.logoContainer}>
<Image source={require('../../../assets/logo.png')} style={styles.logo} />
</View>
<View style={styles.formContainer}>
<KeyboardAvoidingView
behavior={'padding'}
enabled
style={styles.form}
>
<FloatingLabelInput
blurOnSubmit={false}
editable={true}
keyboardType={'email-address'}
label="Email"
onChangeText={this.handleEmailChange}
onSubmitEditing={() => this.passwordInput && this.passwordInput.focus()}
ref={(input) => { this.emailInput = input; }}
returnKeyType="next"
value={this.state.email}
/>
<FloatingLabelInput
editable={true}
label="Password"
onChangeText={this.handlePasswordChange}
onSubmitEditing={() => this.signup()}
ref={(input) => { this.passwordInput = input; }}
secureTextEntry={true}
value={this.state.password}
/>
</KeyboardAvoidingView>
<View style={styles.buttonContainer}>
<Button buttonFunction={() => this.signup()} buttonStyle={'primary'} buttonText={'Sign Up'} />
</View>
</View>
</View>
I feel like I've exhausted most solutions, but I must be missing something crucial.
I had added a Content component in your code, which solves your problem. I don't know why issue had gone by removing KeyboardAvoidingView, but if you want to use KeyboardAvoidingView you can do like this.
<View style={styles.container}>
<Content>
<View style={{ alignItems: "center", marginTop: "50%" }}>
<Image source={require('../assets/CustomLogo1.png')} style={{ marginLeft: 10, marginBottom: 20, height: 200, width: 200 }} />
</View>
<View>
<KeyboardAvoidingView behavior={Platform.Os == "ios" ? "padding" : "height"}>
<FloatingLabelInput
blurOnSubmit={false}
editable={true}
keyboardType={'email-address'}
label="Email"
onChangeText={this.handleEmailChange}
onSubmitEditing={() => this.passwordInput && this.passwordInput.focus()}
ref={(input) => { this.emailInput = input; }}
returnKeyType="next"
value="sample mail"
/>
<FloatingLabelInput
editable={true}
label="Password"
onChangeText={this.handlePasswordChange}
onSubmitEditing={() => this.signup()}
ref={(input) => { this.passwordInput = input; }}
secureTextEntry={true}
value="password"
/>
</KeyboardAvoidingView>
<View style={{ marginTop: 20 }}>
<Button buttonFunction={() => this.signup()} title="sign up" />
</View>
</View>
</Content>
</View>
Initial login page when there is a problem in focusing password:
After making some necessary changes it will be like this:
Just add a content component which can be imported from native base after your View component. I think removing KeyBoardAvoidingView will not fix the issue for smaller screens.
Hope this helps!
I want to display multiple FAB based on numColumns
Unable to render the FAB object inside the Flatlist
<View style={styles.search_field_container}>
<FlatList
data={formatData(this.state.users, numColumns)}
numColumns={numColumns}
keyExtractor={item => item.id}
renderItem={({item}) => this.renderitem(item)}
/>
renderitem = item => {
return (
<Container>
<Header />
<View style={{ flex: 1 }}>
<Fab
active={this.state.active}
direction="up"
containerStyle={{ }}
style={{ backgroundColor: '#5067FF' }}
position="bottomRight"
onPress={() => this.setState({ active: !this.state.active })}>
<Icon name="share" />
<Button style={{ backgroundColor: '#34A34F' }}>
<Icon name="logo-whatsapp" />
</Button>
<Button style={{ backgroundColor: '#3B5998' }}>
<Icon name="logo-facebook" />
</Button>
<Button disabled style={{ backgroundColor: '#DD5144' }}>
<Icon name="mail" />
</Button>
</Fab>
</View>
</Container>
);
};
}
This is my code:
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<KeyboardAvoidingView style={styles.container} behavior="padding" enabled>
<Text style={styles.title}>Login</Text>
<ActivityIndicator size="large" color="#0000ff" animating={this.state.isProcessing} />
<View style={styles.inputContainer}>
<TextInput
style={styles.textInput}
onChangeText={(email) => this.setState({ email })}
value={this.state.email}
keyboardType="email-address"
textContentType="emailAddress"
placeholder="Email"
editable={!this.state.isProcessing}
/>
</View>
<View style={styles.inputContainer}>
<TextInput
style={styles.textInput}
onChangeText={(password) => this.setState({ password })}
value={this.state.password}
secureTextEntry={true}
placeholder="Password"
editable={!this.state.isProcessing}
/>
</View>
<View style={styles.inputContainer}>
<TouchableOpacity
style={styles.button}
onPress={this.logIn.bind(this)}
disabled={this.state.isProcessing}
>
<Text style={{ color: '#fefffe' }}>Login</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
</TouchableWithoutFeedback>
Keyboard.dismiss can work properly, but KeyboardAvoidingView does not work completely.
If I touch the TextInput, KeyboardAvoidingView will not adjust position based on the position of the keyboard.
Why does this happened?
Please help me. Thanks a lot.