19-评论demo

 

 

App.js

import React, {  PureComponent } from 'react'
import CommentInput from './components/CommentInput'
import CommentItem from './components/CommentItem'

export default class App extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      commentList: []
    }
  }

  render() {
    return (
      <div style={{ "width": "500px", "padding": "20px" }}>
        {
          this.state.commentList.map((item, index) => {
            return <CommentItem comment={ item } key={ item.id } removeItem={ e => this.removeComment(index) }/>
          })
        }
        <CommentInput submitComment={ info => this.submitComment(info)  }/>
      </div>
    )
  }

  submitComment(info) {
    console.log(info)

    this.setState({
      commentList: [...this.state.commentList, info]
    })
  }

  removeComment(index) {
    // const newCommentList = [...this.state.commentList]
    // newCommentList.splice(index, 1)
    // this.setState({
    //   commentList: newCommentList
    // })

    this.setState({
      commentList: this.state.commentList.filter((qitem, qindex) => qindex !== index)
    })
  }
}

 

import React, { PureComponent } from 'react'

import { Input, Button } from "antd"

import  moment  from 'moment'

export default class CommentInput extends PureComponent {
  constructor(props) {
    super(props)
    this.state = {
      content: ''
    }
  }
  render() {
    return (
      <div>
        <Input.TextArea rows={4} cols={30}
                        value={ this.state.content }
                        onChange={ e => this.handleChange(e) } />
        <Button type='primary' onClick={ e => this.addComment() }>添加评论</Button>
      </div>
    )
  }
  handleChange(e) {
    this.setState({
      content: e.target.value
    })
  }
  addComment() {
    const commentInfo = {
      id: moment().valueOf(),
      avatar: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.niutuku.com%2Fdesk%2F1208%2F1300%2Fntk-1300-31979.jpg&refer=http%3A%2F%2Fimg2.niutuku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668939799&t=4e22bdf05f93f12a9184f9ca5aa48bde",
      nickname: 'Tom',
      datetime: moment(),
      content: this.state.content
    }
    this.props.submitComment(commentInfo)
    this.setState({
      content: ''
    })
  }
}

 

import React, { PureComponent } from 'react'

import { Comment, Tooltip, Avatar } from 'antd'

import { DeleteOutlined } from '@ant-design/icons'

export default class CommentItem extends PureComponent {
  render() {
    const { nickname, avatar, content, datetime } = this.props.comment
    return (
      <div>
        {/* { this.props.comment.content } */}

        <Comment
          author={ nickname }
          // avatar={avatar}
          avatar = { <Avatar src={avatar} alt={ nickname }/> }
          content={ <p> {content} </p> }
          // datetime={ <span>{ datetime.format("YYY-MM-DD HH:mm:ss") }</span> }
          // datetime={ <span>{ datetime.fromNow() }</span> }
          datetime={
            <Tooltip title={datetime.format("YYY-MM-DD HH:mm:ss")}>
              <span>{ datetime.fromNow() }</span>
            </Tooltip>
          }
          actions={[
            <span onClick={e => this.removeItem()}><DeleteOutlined />删除</span>
          ]}
        />
      </div>
    )
  }

  removeItem() {
    this.props.removeItem()
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值