keyboardavoidingview not working in flatlist footer - react-native

i cannot make the flatlist stay away from my data entry field which is in its footer. here is my code:
import React, { useState, useEffect } from 'react';
import { View, Text, Alert , TextInput, Button, Platform, KeyboardAvoidingView,Animated,Easing} from 'react-native';
import { FlatList } from 'react-native-gesture-handler';
export function PlayAreaScreen({ route, navigation }) {
const [itemsToShow, setitemsToShow] = React.useState([{key:'0',name:"sdfsdfds"}]);
const PopulateTestData = () =>{
const DATA = [];
for (let index = 0; index < 6; index++) {
DATA.push({key:index.toString(), name:`index ${index}`});
}
setitemsToShow(DATA);
console.log(itemsToShow);
}
const MyFooter = (props) =>{
const [sometext, setsometext] = React.useState('');
return(
<View style={{borderWidth:1}}>
<TextInput value={sometext} onChangeText={(text) => setsometext(text)} placeholder="Enter data here"></TextInput>
</View>
)
}
React.useEffect(()=>{
PopulateTestData();
}, []);
return (
<KeyboardAvoidingView behavior={Platform.OS === "ios" ? "position" : "height"}>
<FlatList
data={itemsToShow}
ListFooterComponent = {MyFooter}
keyExtractor={(item) => item.key}
renderItem={(item) => <KeyboardAvoidingView style={{height:80, borderWidth:1}}><Text>{item.item.name}</Text></KeyboardAvoidingView>}
>
</FlatList>
</KeyboardAvoidingView>
)
}
basically it does not scroll the flatlist at all. it obviously works if i have just one or two items in the flatlist so it does not need to scroll to fit the keyboard.
here is a picture showing how the data entry field gets covered:
thanks,
Manish

let's try KeyboardAwareScrollView from react-native-keyboard-aware-scroll-view, some info here

Related

How to check bottomsheet is opened or closed

Here is my code:
if(refRBSheet.current?.open()){
refRBSheet.current.close();
}
In my React Native application I am implementing 'react-native-raw-bottom-sheet'. I want to check if the RBSheet is open, and if it is then I want to close it. The above code open's the RBSheet instead of closing.
As mentioned in the library documentation, it supports onOpen and onClose event functions. Use them to save the open/closed state of the bottom sheet.
import React, { useState, useRef } from 'react';
import { StyleSheet, SafeAreaView, View, Button } from 'react-native';
import RBSheet from 'react-native-raw-bottom-sheet';
const App = () => {
const [open, setOpen] = useState(false);
const refRBSheet = useRef();
const onPress = () => {
if (!open) {
refRBSheet.current.open();
} else {
refRBSheet.current.close();
}
};
return (
<SafeAreaView>
<View>
<Button title="Open" onPress={onPress} />
</View>
<RBSheet
ref={refRBSheet}
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
// .....
>
<Button title="Close" onPress={onPress} />
</RBSheet>
</SafeAreaView>
);
};

button onpress not working with own component

I have made a button component and want to add a onpress but it doesnt work. Can anyone explain me why its not working ?
Button Component.js
import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Dimensions } from 'react-native';
const width = Dimensions.get('window').width;
const Button = ({ title }) => {
return (
<TouchableOpacity>
<Text>{title}</Text>
</TouchableOpacity>
)
};
export default Button;
Main.js
...
<Button onPress={handleSubmit} title="Jetzt registrieren"/>
...
you need to extract you onPress prop
const Button = ({ title,onPress }) => {
return (
<TouchableOpacity onPress={onPress}>
<Text>{title}</Text>
</TouchableOpacity>
)
};

custom drawer focused from #react-navigation/drawer is not working correctly

I'm building app that requires custom drawer elements. I want to be able to highlight currently selected screen. I use Context to keep the state of the current screen selected and it changes indeed. changeDrawerElem is the function that keeps element number in Contex and I see that it changes onPress in console.log. However, elements of the CustomDrawerContent are rendered all focused regardless my attempt to control them. In docs it is said the property "focused" should take boolean value and based on that highlight focused element. react navigation version: 5 Any help is appreciated.
import React, { Component } from 'react';
import { useContext } from 'react';
import { Context } from '../context/settingContext';
import { StyleSheet, Text, View, ImageBackground, TouchableOpacity} from 'react-native';
import { DrawerContentScrollView, DrawerItemList, DrawerItem } from '#react-navigation/drawer';
import FontAwesome, { SolidIcons, RegularIcons, BrandIcons } from 'react-native-fontawesome';
import defaultStyles from '../css/defaultStyles';
function CustomDrawerContent (props){
const {state} = useContext(Context);
const {drawerElem} = state.find(elem => elem.hasOwnProperty('drawerElem'));
const {changeDrawerElem} = useContext(Context);
return(
<DrawerContentScrollView {...props}>
<DrawerItem
label="Головна"
onPress={() => {
changeDrawerElem(1);
props.navigation.navigate('StackNav', { screen: 'MainScreen'} )
}}
icon={() => <FontAwesome style={styles.drawerIcons} icon={SolidIcons.home} size={30} />}
inactiveTintColor = "#000000"
activeBackgroundColor = "#cdb9a5"
focused = { () => drawerElem == 1 ? true : false}
/>
<DrawerItem
label="Зміст"
onPress={() => {
changeDrawerElem(2);
props.navigation.navigate('StackNav', { screen: 'Content' });
}}
icon={() => <FontAwesome style={styles.drawerIcons} icon={SolidIcons.listOl} size={30} />}
inactiveTintColor = "#000000"
activeBackgroundColor = "#cdb9a5"
focused = { () => drawerElem == 2 ? true : false}
/>
</DrawerContentScrollView>
);
}
const styles = StyleSheet.create(defaultStyles);
export default CustomDrawerContent;
I found an answer which solved my problem, you need to change your focused props to
focused={props.state.index === props.state.routes.findIndex(e => e.name === 'Головна')}

Automatically uncheck checkbox when checking another react native

I have two checkboxes (from react-native elements), let's call them box a and box b, where it should only be possible to have one of them checked at a time (no multiple selection), iow - if box a is checked, it is not possible to check box b. So as of this moment, if I were to have checked box a by mistake, I need to uncheck box a manually by clicking it again, in order to check box b. However, I want to be able to automatically uncheck box a by clicking and checking box b - if that makes any sense.
I have tried to look at both issue 54111540 and others, but none of the answers there seem to help with what I want to achieve.
My code:
import React, { useState } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, } from 'react-native';
import { CheckBox } from 'react-native-elements';
import { Ionicons } from '#expo/vector-icons';
import { slider } from '../../components/SliderStyle';
import { useDispatch } from 'react-redux';
import { addfirstrole } from '../../redux/actions/index';
import store from '../../redux/store';
import * as firebase from 'firebase';
const RoleScreen = ({ navigation }) => {
const dispatch = useDispatch()
const addFirstRole = firstRole => dispatch(addfirstrole(firstRole))
const [landlordChecked, setLandlordChecked ] = useState(false)
const [tenantChecked, setTenantChecked] = useState(false)
const user = firebase.auth().currentUser
return (
<View>
<Text>Role screen</Text>
<CheckBox
title='Jeg er utleier'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={landlordChecked}
onPress={tenantChecked ? () => setLandlordChecked(false) : () => setLandlordChecked(!landlordChecked)}
/>
<CheckBox
title='Jeg er leietaker'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={tenantChecked}
onPress={landlordChecked ? () => setTenantChecked(false) : () => setTenantChecked(!tenantChecked)}
/>
<TouchableOpacity onPress={() => { navigation.navigate('Search'); addFirstRole(user.uid); console.log(store.getState()); }}>
<View style={slider.buttonStyle}>
<Text style={slider.textStyle}>Neste</Text>
<Ionicons name='ios-arrow-forward'style={slider.iconStyle} />
</View>
</TouchableOpacity>
</View>
)
}
const styles = StyleSheet.create({})
export default RoleScreen;
Try this, this will help you
import React, { useState } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, } from 'react-native';
import { CheckBox } from 'react-native-elements';
import { Ionicons } from '#expo/vector-icons';
import { useDispatch } from 'react-redux';
import * as firebase from 'firebase';
const RoleScreen = ({ navigation }) => {
const [landlordChecked, setLandlordChecked ] = useState(false)
const [tenantChecked, setTenantChecked] = useState(false)
return (
<View>
<Text>Role screen</Text>
<CheckBox
title='Jeg er utleier'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={landlordChecked}
onPress={landlordChecked ? () => setLandlordChecked(false) : () => [setLandlordChecked(true),setTenantChecked(false)]}
/>
<CheckBox
title='Jeg er leietaker'
checkedIcon='dot-circle-o'
uncheckedIcon='circle-o'
checked={tenantChecked}
onPress={tenantChecked ? () => setTenantChecked(false): () => [setTenantChecked(true),setLandlordChecked(false)]}
/>
</View>
)
}
const styles = StyleSheet.create({})
export default RoleScreen;
Change this according to your requirement. Feel free for doubts
this is How to uncheck other values if one is checked
this is a function we call with onPress
data3.checked is true or falses
data3.filterx.id is filter id
onPress statement is
{data4.map((item) => {
return (
<TouchableOpacity
style={{
flexDirection: "row",
alignItems: "center",
}}
key={item.filterx.id}
onPress={() => handleChange4(item.filterx.id)}
>
<CheckBox checked={item.checked} />
<Text>{item.filterx.title}</Text>
</TouchableOpacity>
);
})}
const handleChange3 = (id) => {
let temp = data3.map((data3) => {
if (id === data3.filterx.id) {
return { ...data3, checked: !data3.checked };
}
return data3;
});
for (var i = 0; i < data3.length; i++) {
data3[i].checked = false;
}
data3.checked = true;
console.log(temp);
setdata3(temp);
};

React Native Modal CallBack - Passing Props to Parent

I am using React Native Modal and am having difficulty passing back the props from the Modal to the class that called the Modal.
I have tried with the onRequestClose() and onPress() and setting a prop for callback data googlePlacesPassedBackLocation but no success. I am trying to retrieve pastLocations from GooglePlaces. I can see in GooglePlaces that the location is being generated correctly. I am using the useState hook in both the Modal class and the GooglePlaces class.
I have read that this is a difficult thing to do as react doesn't really support it. Is it possible at all?
import {StyleSheet, View, Modal, TouchableOpacity, Text, TouchableHighlight, Button } from 'react-native';
Parent Component:
const RegisterLocationScreen = ({navigation}) => {
var [pastLocations, setPastLocations] = useState([]);
var [pastModalVisible, setModalPastVisible] = useState(false);
const closeModal = (timeFrame) => {
setModalPastVisible(false);
};
const openGooglePastModal = () => {
setModalPastVisible(true)
};
return (
<Modal visible={pastModalVisible} animationType={'slide'} onRequestClose={() => closeModal(pastLocations)}>
<View style={styles.modalContainer}>
<SafeAreaView style = {styles.safeAreaViewStyle} forceInset={{top: 'always'}}>
<GooglePlaces timePeriod="Past" googlePlacesPassedBackLocation={googlePlacesPassedBackLocation} />
<TouchableOpacity style = {styles.buttonContainer} onPress={()=> closeModal('Past')}>
<Text style={styles.buttonText} >
Close Modal
</Text>
</TouchableOpacity>
</SafeAreaView>
</View>
</Modal>
<TouchableOpacity style = {styles.buttonModal} onPress={() => openGooglePastModal()} >
<Text style={styles.buttonModalText} >
Past Locations
</Text>
</TouchableOpacity>
Child Component:
import React, {useState} from 'react';
import {StyleSheet } from 'react-native'
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
const API_KEY = '123omittedforsecurity';
const GooglePlaces = ({timePeriod}) => {
var [pastLocations, setPastLocations] = useState([]);
const updateLocationArray = (data, details) =>{
setPastLocations([
...pastLocations,
details.geometry.location
]);
};
return (
<GooglePlacesAutocomplete
placeholder={timePeriod}
onPress={(data, details) => {
updateLocationArray(data, details);
}}
..../>