Created
July 28, 2021 10:50
-
-
Save ikyamelia/a2224668b21eed0714d9cca5fc0ef264 to your computer and use it in GitHub Desktop.
assessmentglints
This file contains hidden or 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 ReactDOM from 'react-dom'; | |
const style = { | |
table: { | |
borderCollapse: 'collapse' | |
}, | |
tableCell: { | |
border: '1px solid gray', | |
margin: 0, | |
padding: '5px 10px', | |
width: 'max-content', | |
minWidth: '150px' | |
}, | |
form: { | |
container: { | |
padding: '20px', | |
border: '1px solid #F0F8FF', | |
borderRadius: '15px', | |
width: 'max-content', | |
marginBottom: '40px' | |
}, | |
inputs: { | |
marginBottom: '5px' | |
}, | |
submitBtn: { | |
marginTop: '10px', | |
padding: '10px 15px', | |
border:'none', | |
backgroundColor: 'lightseagreen', | |
fontSize: '14px', | |
borderRadius: '5px' | |
} | |
} | |
} | |
function PhoneBookForm({ addEntryToPhoneBook }) { | |
const [firstname, setfirstname] = useState(''); | |
const [lastname, setlastname] = useState(''); | |
const [phone, setphone] = useState(''); | |
return ( | |
<form onSubmit={e => { | |
const data = { | |
firstname: firstname, | |
lastname: lastname, | |
phone: phone, | |
} | |
addEntryToPhoneBook(data) | |
setfirstname(''); | |
setlastname(''); | |
setphone(''); | |
e.preventDefault() }} style={style.form.container}> | |
<label>First name:</label> | |
<br /> | |
<input | |
style={style.form.inputs} | |
className='userFirstname' | |
name='userFirstname' | |
value={firstname} | |
onChange={event => setfirstname(event.target.value)} | |
type='text' | |
/> | |
<br/> | |
<label>Last name:</label> | |
<br /> | |
<input | |
style={style.form.inputs} | |
className='userLastname' | |
name='userLastname' | |
value={lastname} | |
onChange={event => setlastname(event.target.value)} | |
type='text' | |
/> | |
<br /> | |
<label>Phone:</label> | |
<br /> | |
<input | |
style={style.form.inputs} | |
className='userPhone' | |
name='userPhone' | |
value={phone} | |
onChange={event => setphone(event.target.value)} | |
type='text' | |
/> | |
<br/> | |
<input | |
style={style.form.submitBtn} | |
className='submitButton' | |
type='submit' | |
value='Add User' | |
/> | |
</form> | |
) | |
} | |
function InformationTable({listData}) { | |
return ( | |
<table style={style.table} className='informationTable'> | |
<thead> | |
<tr> | |
<th style={style.tableCell}>First name</th> | |
<th style={style.tableCell}>Last name</th> | |
<th style={style.tableCell}>Phone</th> | |
</tr> | |
</thead> | |
<tbody> | |
{listData.map((data, index) => ( | |
<tr key={index}> | |
<td style={style.tableCell}>{data.firstname}</td> | |
<td style={style.tableCell}>{data.lastname}</td> | |
<td style={style.tableCell}>{data.phone}</td> | |
</tr> | |
))} | |
</tbody> | |
</table> | |
); | |
} | |
function Application(props) { | |
const [phoneBook, setPhoneBook] = useState([]); | |
return ( | |
<section> | |
<PhoneBookForm | |
addEntryToPhoneBook={data => { | |
setPhoneBook([...phoneBook, data]); | |
}}/> | |
<InformationTable listData={phoneBook} /> | |
</section> | |
); | |
} | |
ReactDOM.render( | |
<Application />, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment