react todolist 3

react todolist 3_第1张图片

功能:1.输入框输入事项,点击add,list增加一条,输入框清空并且聚焦;

2.点击当前checkbox,勾选文字为已完成,取消勾选文字为未完成;

父组件TodoList.js

分成了三个子组件

TodoHeader,TodoInput,List

 

import React, { Component } from 'react';
import{TodoInput,TodoHeader,List} from './index.js'

class TodoList extends Component {
  constructor(){
    super()
    this.state={
      title2:"今日事今日毕",
      todos:[{
        id:1,
        title:"吃饭",
        isComleted:true
      },
      { 
        id:2,
        title:"学习",
        isComleted:true
      },
      { 
        id:3,
        title:"睡觉",
        isComleted:false
      },
    ]
    }
  }
  render() {
    return (
      
{this.state.title2}
); } addTodo=(todoTitle)=>{ console.log('子元素传值',todoTitle) //注意不能用push这样写,因为push返回的是数组的长度 // this.setState({ // todos:this.state.todos.concat({ // id:Math.random(), // title:todoTitle, // isComleted:false // }) // }) //用push这样写 // const newTodos = [...this.state.todos] // newTodos.push({ // id:Math.random(), // title:todoTitle, // isComleted:false // }) // this.setState({ // todos:newTodos // }) //第三种新值添加进数组的方法 const newlist = { id:Math.random(), title:todoTitle, isComleted:false } this.setState({ todos:[...this.state.todos,newlist] }) } //点击checkbox取反 onisComletedChange=(id)=>{ console.log(id) const data = this.state.todos.map(item=>{ if(item.id===id){ item.isComleted=!item.isComleted } return item }) this.setState({ todos:data }) } } export default TodoList

TodoHeader.js

import React, { Component } from 'react';

export default class TodoHeader extends Component {
  render() {
    console.log(this.props) //{title: "待办事项", children: "今日事今日毕"}
    return (
      

{this.props.title}

{this.props.children}

); } }

TodoInput.js

import React, { Component,createRef } from 'react';

export default class TodoInput extends Component {
  constructor(){
    super()
    this.state={
        inputValue:''
    }
    this.handleAdd = this.handleAdd.bind(this)
    this.handleKeyUp = this.handleKeyUp.bind(this)
    //定义createRef
    this.inputDom = createRef()
  }
  render() {
    return (
      
); } //点击添加从state中获取最新值 输入框聚焦this.inputDom.current.focus() handleAdd(){ if(this.state.inputValue ===''){ return }else{ //接收父组件addTodo事件,并将input参数传递给父元素 this.props.addTodo(this.state.inputValue) console.log('inputDom',this.inputDom) this.setState({ inputValue:"" },()=>{ this.inputDom.current.focus() }) } } //获取input的值,并交给state handleInput(e){ // console.log(e.currentTarget.value) this.setState({ inputValue:e.currentTarget.value }) } //回车输入 handleKeyUp(e){ // console.log(e.keyCode) if(e.keyCode ===13){ this.handleAdd() this.setState({ inputValue:"" }) } } }

List.js

import React, { Component } from 'react';
//引入类型检查插件
import PropTypes from 'prop-types'

export default class List extends Component {
 //父元素类型验证 验证todo是否是array,以及内部各项的数据类型验证
  static propTypes = {
      todo:PropTypes.arrayOf(PropTypes.shape({
          id:PropTypes.number.isRequired,
          title:PropTypes.string.isRequired,
          isComleted:PropTypes.bool.isRequired,
      })).isRequired
  }
  render() {
    console.log(this.props.todo) //父组件传来的todo
    let todo = this.props.todo
    return (
      
{ todo.map(item=>{ return
{item.title} {item.isComleted?'已完成':'未完成'}
}) }
); } handleBoxChange(id){ // console.log(id) this.props.onisComletedChange(id) //接收父元素的事件并传值 } }

 

你可能感兴趣的:(React,案例)