|
| 1 | +import './index.css' |
| 2 | +import React, { createRef } from 'react' |
| 3 | +const avatar = 'https://p9-passport.byteacctimg.com/img/user-avatar/4e9e751e2b32fb8afbbf559a296ccbf2~300x300.image' |
| 4 | + |
| 5 | +// 时间格式化 |
| 6 | +function formatDate (time) { |
| 7 | + return `${time.getFullYear()}-${time.getMonth() + 1}-${time.getDate()}` |
| 8 | +} |
| 9 | + |
| 10 | +class Comment extends React.Component { |
| 11 | + commentInput = createRef() |
| 12 | + state = { |
| 13 | + // hot: 热度排序 time: 时间排序 |
| 14 | + tabs: [ |
| 15 | + { |
| 16 | + id: 1, |
| 17 | + name: '热度', |
| 18 | + type: 'hot' |
| 19 | + }, |
| 20 | + { |
| 21 | + id: 2, |
| 22 | + name: '时间', |
| 23 | + type: 'time' |
| 24 | + } |
| 25 | + ], |
| 26 | + active: 'hot', |
| 27 | + list: [ |
| 28 | + { |
| 29 | + id: 1, |
| 30 | + author: '刘德华', |
| 31 | + comment: '给我一杯忘情水', |
| 32 | + time: new Date('2021年10月10日 09:09:00'), |
| 33 | + // 1: 点赞 0:无态度 -1:踩 |
| 34 | + attitude: 1 |
| 35 | + }, |
| 36 | + { |
| 37 | + id: 2, |
| 38 | + author: '周杰伦', |
| 39 | + comment: '哎哟,不错哦', |
| 40 | + time: new Date('2021年10月11日 09:09:00'), |
| 41 | + // 1: 点赞 0:无态度 -1:踩 |
| 42 | + attitude: 0 |
| 43 | + }, |
| 44 | + { |
| 45 | + id: 3, |
| 46 | + author: '五月天', |
| 47 | + comment: '不打扰,是我的温柔', |
| 48 | + time: new Date('2021年10月11日 10:09:00'), |
| 49 | + // 1: 点赞 0:无态度 -1:踩 |
| 50 | + attitude: -1 |
| 51 | + } |
| 52 | + ], |
| 53 | + commentText: '' |
| 54 | + } |
| 55 | + toggleTab = (e, type) => { |
| 56 | + type === 'hot' ? this.setState({ active: 'hot' }) : this.setState({ active: 'time' }) |
| 57 | + } |
| 58 | + comment = () => { |
| 59 | + // let comment = this.commentInput.current.value //非受控组件 |
| 60 | + let comment = this.state.commentText |
| 61 | + if (!comment) return |
| 62 | + let list = [...this.state.list, { |
| 63 | + id: Date.now(), |
| 64 | + author: '刘德华', |
| 65 | + comment, |
| 66 | + time: new Date(), |
| 67 | + // 1: 点赞 0:无态度 -1:踩 |
| 68 | + attitude: 1 |
| 69 | + },] |
| 70 | + this.setState({ list }) |
| 71 | + } |
| 72 | + change = (e) => { |
| 73 | + this.setState({ |
| 74 | + commentText: e.target.value |
| 75 | + }) |
| 76 | + } |
| 77 | + del (e, id) { |
| 78 | + let list = this.state.list.filter(item => item.id !== id) |
| 79 | + this.setState({ list }) |
| 80 | + } |
| 81 | + toggleLike = (e, id, attitude) => { |
| 82 | + let list = this.state.list.map(item => { |
| 83 | + if (item.id === id) { |
| 84 | + attitude === 'like' ? item.attitude === 1 ? item.attitude = 0 : item.attitude = 1 : item.attitude === -1 ? item.attitude = 0 : item.attitude = -1 |
| 85 | + return item |
| 86 | + } else { |
| 87 | + return item |
| 88 | + } |
| 89 | + }) |
| 90 | + this.setState({ |
| 91 | + list |
| 92 | + }) |
| 93 | + |
| 94 | + } |
| 95 | + render () { |
| 96 | + return ( |
| 97 | + <div className="App"> |
| 98 | + <div className="comment-container"> |
| 99 | + {/* 评论数 */} |
| 100 | + <div className="comment-head"> |
| 101 | + <span>{this.state.list.length} 评论</span> |
| 102 | + </div> |
| 103 | + {/* 排序 */} |
| 104 | + <div className="tabs-order"> |
| 105 | + <ul className="sort-container"> |
| 106 | + { |
| 107 | + this.state.tabs.map(item => ( |
| 108 | + <li onClick={(e) => this.toggleTab(e, item.type)} key={item.id} className={item.type === this.state.active ? 'on' : ''}>按{item.name}排序</li> |
| 109 | + )) |
| 110 | + } |
| 111 | + </ul> |
| 112 | + </div> |
| 113 | + |
| 114 | + {/* 添加评论 */} |
| 115 | + <div className="comment-send"> |
| 116 | + <div className="user-face"> |
| 117 | + <img className="user-head" src={avatar} alt="" /> |
| 118 | + </div> |
| 119 | + <div className="textarea-container"> |
| 120 | + <textarea |
| 121 | + cols="80" |
| 122 | + rows="5" |
| 123 | + placeholder="发条友善的评论" |
| 124 | + className="ipt-txt" |
| 125 | + ref={this.commentInput} |
| 126 | + value={this.state.commentText} |
| 127 | + onChange={this.change} |
| 128 | + /> |
| 129 | + <button className="comment-submit" onClick={this.comment}>发表评论</button> |
| 130 | + </div> |
| 131 | + <div className="comment-emoji"> |
| 132 | + <i className="face"></i> |
| 133 | + <span className="text">表情</span> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + |
| 137 | + {/* 评论列表 */} |
| 138 | + <div className="comment-list"> |
| 139 | + { |
| 140 | + this.state.list.map(item => ( |
| 141 | + <div className="list-item" key={item.id}> |
| 142 | + <div className="user-face"> |
| 143 | + <img className="user-head" src={avatar} alt="" /> |
| 144 | + </div> |
| 145 | + <div className="comment"> |
| 146 | + <div className="user">{item.author}</div> |
| 147 | + <p className="text">{item.comment}</p> |
| 148 | + <div className="info"> |
| 149 | + <span className="time">{formatDate(item.time)}</span> |
| 150 | + <span className={item.attitude === 1 ? 'like liked' : 'like'} onClick={(e) => this.toggleLike(e, item.id, 'like')}> |
| 151 | + <i className="icon" /> |
| 152 | + </span> |
| 153 | + <span className={item.attitude === -1 ? 'hate hated' : 'hate'} onClick={(e) => this.toggleLike(e, item.id, 'hate')}> |
| 154 | + <i className="icon" /> |
| 155 | + </span> |
| 156 | + <span className="reply btn-hover" onClick={(e) => this.del(e, item.id)}>删除</span> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + )) |
| 161 | + } |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + </div>) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +export default Comment |
0 commit comments