Skip to content

Instantly share code, notes, and snippets.

@jmysliv
Created August 30, 2021 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmysliv/87b15453aab173a63a4d22fcc5b1d603 to your computer and use it in GitHub Desktop.
Save jmysliv/87b15453aab173a63a4d22fcc5b1d603 to your computer and use it in GitHub Desktop.
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