How to create a MultiSelect Switch in FlatList
Example:
import React from 'react';
import {
View,
StyleSheet,
Text,
FlatList,
Switch,
SafeAreaView,
TouchableOpacity,
Modal,
} from 'react-native';
export default props => {
const [data, setData] = React.useState([
{label: 'temperature', selected: false},
{label: 'humidity'},
{label: 'light'},
{label: 'move'},
{label: 'sound'},
{label: 'carbon dioxide'},
{label: 'air pollution'},
]);
onUpdateValue = (index, value) => {
data[index].selected = value;
return setData([...data]);
};
renderItem = ({item, index}) => (
<ItemRenderer
key={index}
index={index}
selected={item.selected}
label={item.label}
onUpdateValue={onUpdateValue}
/>
);
return (
<SafeAreaView style={styles.container}>
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={item => item.label}
/>
</SafeAreaView>
);
};
const ItemRenderer = ({index, label, selected, onUpdateValue}) => (
<View style={styles.item}>
<Text style={styles.title}>{label}</Text>
<Switch
value={selected}
onValueChange={value => onUpdateValue(index, value)}
/>
</View>
);
const styles = StyleSheet.create({
container: {
flex: 1,
},
listWrapper: {
flex: 1,
shadowOffset: {width: 0, height: 4},
shadowOpacity: 0.5,
elevation: 10,
shadowRadius: 5,
},
listContainer: {
flex: 1,
backgroundColor: '#FFF',
borderTopLeftRadius: 15,
borderTopRightRadius: 15,
},
item: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
padding: 12,
borderBottomWidth: 1,
borderBottomColor: '#CCCCCC55',
},
tabHeading: {
padding: 20,
flexDirection: 'row',
},
title: {
textTransform: 'capitalize',
color: '#fff',
},
});
No comments:
Post a Comment