vi src/Contact.js
import React from 'react';
const Contact = ({contact,item}) =>
<ul>
<li> {contact.name} {contact.phone} {item}</li>
</ul>
export default Contact;
vi src/ContactsList.js
import React from 'react';
import Contact from './Contact';
class ContactsList extends React.Component{
constructor(props){
super(props);
this.state ={
search:'',
contacts: props.contacts
};
}
updateSearch(event){
// console.log(event.target.value);
this.setState({search:event.target.value.substr(0,20)});
}
addContact(event){
event.preventDefault();
let name=this.refs.name.value;
let phone= this.refs.phone.value;
let id = Math.floor((Math.random()*100)+1);
console.log(this.refs.name.value);
this.setState({
contacts: this.state.contacts.concat({id,name,phone})
})
this.refs.name.value = '';
this.refs.phone.value = '';
}
render(){
let filteredContacts = this.state.contacts.filter(
(contact) => {
return contact.name.toLowerCase().indexOf(this.state.search.toLowerCase())!= -1;
}
);
console.log(this.props.contacts);
return(
<div>
<input type="text"
placeholder ="search Name"
value={this.state.search}
onChange={this.updateSearch.bind(this)}
/>
<form onSubmit={this.addContact.bind(this)}>
<input type="text" ref="name" />
<input type="text" ref="phone" />
<button type="submit" > Add New Contact</button>
</form>
<ul>
{filteredContacts.map((contact)=>{
return <Contact contact={contact} item="hello" key={contact.id}/>
})}
</ul>
</div>
)
}
}
export default ContactsList;