React_antd_Comment(评论)_typescript

嵌套评论

actions:在评论项项展示操作项列表;
author:显示为作者的名字;
avatar:显示评论头像元素,常为antdAvatar或者src;
children:嵌套子项;
content:评论内容;
datetime:显示时间;

  • index.tsx
import React, { Component } from 'react';
import { Comment, Avatar } from 'antd';

interface ICode {
  children?: any
  onClickComment: any
}

const ExampleComment: React.FC<ICode> = (code) => {
  const { children, onClickComment } = code;

  return (
    <Comment
      actions={[<span key="comment-nested-reply-to" onClick={onClickComment}>Reply to</span>]}
      author={<a>Han Solo</a>}
      avatar={
        <Avatar
          src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
          alt="Han Solo"
        />
      }
      content={
        <p>
          We supply a series of design principles, practical patterns and high quality design
          resources (Sketch and Axure).
        </p>
      }
    >
      {children}
    </Comment>
  );
}

export class Index extends Component {
  render() {
    return (
      <div>
        <ExampleComment onClickComment = { this.exampleCommentClickedHandler }>
          <ExampleComment  onClickComment = { this.exampleCommentClickedHandler }>
          </ExampleComment>
        </ExampleComment>,
      </div>
    )
  }
  exampleCommentClickedHandler = () => {
    console.log("**********")
  }
}

export default Index;

在这里插入图片描述

  • 回复框
import React, { Component } from 'react';
import { Comment, Avatar, Form, Button, List, Input } from 'antd';
import moment from 'moment';

const { TextArea } = Input;

/**
 * 评论: 回复框
 */

interface ICode {
  submitting?: boolean,
  value?: string
}

interface IState extends ICode {
  comments: [],
}

interface IEditor extends ICode {
  onChangeComment: any,
  onClickComment: any,
}
/** 表单 */
const Editor: React.FC<IEditor> = (props) => {
  const { onChangeComment, onClickComment, value, submitting } = props;
  return (
    <>
      <Form.Item>
        <TextArea rows={4} onChange={onChangeComment} value={value} />
      </Form.Item>
      <Form.Item>
        <Button htmlType="submit" loading={submitting} onClick={onClickComment} type="primary"> Add Comment </Button>
      </Form.Item>
    </>
  );
}

/** 列表 */
const CommentList: React.FC<IState> = (state) => {
  const { comments } = state;
  return (
    <List
      dataSource={comments}
      header={`${comments.length} ${comments.length > 1 ? 'replies' : 'reply'}`}
      itemLayout="horizontal"
      renderItem={props => <Comment {...props} />}
    />
  );
}

class Index extends Component<IState> {
  state: IState = {
    comments: [],
    submitting: false,
    value: '',
  }
  render() {
    const { comments, submitting, value } = this.state;
    return (

      <>
        {
          comments.length > 0 && <CommentList comments={comments} />
        }
        <Comment
          avatar={
            <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"/>
          }
          content={
            <Editor
              onChangeComment={this.editorCommentChangedHandler}
              onClickComment={this.editorCommentClickedHandler}
              submitting={submitting}
              value={value} />
          }
        />
      </>
    )
  }

  editorCommentChangedHandler = (e: any) => {
    this.setState({
      value: e.target.value,
    });
  };

  editorCommentClickedHandler = () => {
    if (!this.state.value) {
      return;
    }

    this.setState({
      submitting: true,
    });
    /** 定时器 */
    setTimeout(() => {
      this.setState({
        submitting: false,
        value: '',
        comments: [
          {
            author: 'Tao Solo',
            avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
            content: <p>{this.state.value}</p>,
            datetime: moment().fromNow(),
          },
          ...this.state.comments,//组合
        ],
      });
    }, 1000);
  }

}

export default Index;

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值