15 Using Forms

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} key={contact.id}/>
            })}


            </ul>

            </div>
            )
    }
}
export default ContactsList;

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章