Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 1 | import React, { useState } from 'react'; |
| 2 | import './CommentForm.css'; |
| 3 | |
| 4 | const CommentForm = ({ onSubmit, onCancel }) => { |
| 5 | const [content, setContent] = useState(''); |
| 6 | const [isFocused, setIsFocused] = useState(false); |
| 7 | |
| 8 | const handleSubmit = (e) => { |
| 9 | e.preventDefault(); |
| 10 | if (content.trim()) { |
| 11 | onSubmit(content); |
| 12 | setContent(''); |
| 13 | } |
| 14 | }; |
| 15 | |
| 16 | return ( |
| 17 | <div className="comment-form-container"> |
| 18 | <form className="comment-form" onSubmit={handleSubmit}> |
| 19 | <div className={`form-group ${isFocused ? 'focused' : ''}`}> |
| 20 | <textarea |
| 21 | className="comment-input" |
| 22 | placeholder="分享你的想法..." |
| 23 | value={content} |
| 24 | onChange={(e) => setContent(e.target.value)} |
| 25 | onFocus={() => setIsFocused(true)} |
| 26 | onBlur={() => setIsFocused(false)} |
| 27 | required |
| 28 | /> |
| 29 | <div className="floating-label">添加评论</div> |
| 30 | </div> |
| 31 | |
| 32 | <div className="form-actions"> |
| 33 | <button |
| 34 | type="button" |
| 35 | className="cancel-button" |
| 36 | onClick={onCancel} |
| 37 | > |
| 38 | 取消 |
| 39 | </button> |
| 40 | <button |
| 41 | type="submit" |
| 42 | className="submit-button" |
| 43 | disabled={!content.trim()} |
| 44 | > |
| 45 | 发布评论 |
| 46 | </button> |
| 47 | </div> |
| 48 | </form> |
| 49 | </div> |
| 50 | ); |
| 51 | }; |
| 52 | |
| 53 | export default CommentForm; |