From b86dda189d5cda14a0ce41fe977a8573b2d6d992 Mon Sep 17 00:00:00 2001 From: pfqgauxfb <3521106529@qq.com> Date: Tue, 3 Jun 2025 09:25:38 +0800 Subject: [PATCH] feng --- src/components/Comments/index.js | 97 ++++++++++++++++++ src/components/Comments/styles.module.css | 114 ++++++++++++++++++++++ src/theme/DocItem/index.js | 61 ++++++++++++ src/theme/DocItem/styles.module.css | 19 ++++ 4 files changed, 291 insertions(+) create mode 100644 src/components/Comments/index.js create mode 100644 src/components/Comments/styles.module.css create mode 100644 src/theme/DocItem/index.js create mode 100644 src/theme/DocItem/styles.module.css diff --git a/src/components/Comments/index.js b/src/components/Comments/index.js new file mode 100644 index 0000000..fbf44c9 --- /dev/null +++ b/src/components/Comments/index.js @@ -0,0 +1,97 @@ +import React, { useState, useEffect } from 'react'; +import styles from './styles.module.css'; + +const Comments = () => { + const [comments, setComments] = useState([]); + const [newComment, setNewComment] = useState(''); + const [name, setName] = useState(''); + + // 从 localStorage 加载评论 + useEffect(() => { + const savedComments = localStorage.getItem('docComments'); + if (savedComments) { + setComments(JSON.parse(savedComments)); + } + }, []); + + // 保存评论到 localStorage + const saveComments = (newComments) => { + localStorage.setItem('docComments', JSON.stringify(newComments)); + }; + + const handleSubmit = (e) => { + e.preventDefault(); + if (!newComment.trim() || !name.trim()) return; + + const comment = { + id: Date.now(), + name, + content: newComment, + date: new Date().toLocaleString(), + }; + + const updatedComments = [...comments, comment]; + setComments(updatedComments); + saveComments(updatedComments); + setNewComment(''); + }; + + const handleDelete = (commentId) => { + const updatedComments = comments.filter(comment => comment.id !== commentId); + setComments(updatedComments); + saveComments(updatedComments); + }; + + return ( +
+

评论

+ +
+
+ setName(e.target.value)} + placeholder="您的名字" + className={styles.input} + required + /> +
+
+