This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import { Button, View, Text, ScrollView, TextInput } from 'react-native'; | |
interface EventParticipant { | |
name: string; | |
id: string; | |
} | |
const styles = { | |
participantView: { | |
borderBottomColor: 'black', | |
width: '100%', | |
borderBottomWidth: 1, | |
padding: 10, | |
display: 'flex', | |
flexDirection: 'row', | |
alignItems: 'center', | |
justifyContent: 'space-between', | |
backgroundColor: '#fffbeb', | |
}, | |
listView: { | |
display: 'flex', | |
flexDirection: 'column', | |
alignItems: 'center', | |
justifyContent: 'space-between', | |
height: '100%', | |
paddingBottom: 30, | |
}, | |
bottomRow: { | |
width: '100%', | |
display: 'flex', | |
flexDirection: 'row', | |
justifyContent: 'space-between', | |
alignItems: 'center', | |
padding: 10, | |
}, | |
textInput: { | |
display: 'flex', | |
flexDirection: 'row', | |
alignItems: 'center', | |
}, | |
} | |
function Participant({ | |
name, | |
onRemove, | |
}: { | |
name: string; | |
onRemove: () => void; | |
}): React.ReactElement { | |
return ( | |
<View | |
style={[styles.participantView]}> | |
<Text>{name}</Text> | |
<Button title="Remove" color="red" onPress={onRemove} /> | |
</View> | |
); | |
} | |
export default function AnimatedListExample(): React.ReactElement { | |
const [inputValue, setInputValue] = useState(''); | |
const [participantList, setParticipantList] = useState<EventParticipant[]>( | |
[] | |
); | |
const addParticipant = () => { | |
setParticipantList( | |
[{ name: inputValue, id: Date.now().toString() }].concat(participantList) | |
); | |
setInputValue(''); | |
}; | |
const removeParticipant = (id: string) => { | |
setParticipantList( | |
participantList.filter((participant) => participant.id !== id) | |
); | |
}; | |
return ( | |
<View | |
style={[styles.listView]}> | |
<ScrollView style={[{ width: '100%' }]}> | |
{participantList.map((participant) => ( | |
<Participant | |
key={participant.id} | |
name={participant.name} | |
onRemove={() => removeParticipant(participant.id)} | |
/> | |
))} | |
</ScrollView> | |
<View | |
style={[ styles.bottomRow]}> | |
<View | |
style={[styles.textInput]}> | |
<Text>Add participant: </Text> | |
<TextInput | |
placeholder="Name" | |
value={inputValue} | |
onChangeText={setInputValue} | |
/> | |
</View> | |
<Button | |
title="Add" | |
disabled={inputValue === ''} | |
onPress={addParticipant} | |
/> | |
</View> | |
</View> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment