staff_data/apps/web/src/app/main/staffinfo_write/infoCard.tsx

91 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-03-26 13:00:55 +08:00
import { Input, Button } from 'antd';
import React, { useState, useRef, useEffect } from 'react';
2025-03-26 10:31:29 +08:00
type InfoCardProps = {
2025-03-27 08:59:02 +08:00
onAdd: (content: string[]) => void;
2025-03-26 13:00:55 +08:00
onHeightChange?: (height: number) => void; // 添加高度变化回调
2025-03-26 10:31:29 +08:00
}
2025-03-26 13:00:55 +08:00
const InfoCard: React.FC<InfoCardProps> = ({ onAdd, onHeightChange }) => {
2025-03-26 10:31:29 +08:00
const [content, setContent] = useState('');
2025-03-26 10:39:24 +08:00
const [addedContents, setAddedContents] = useState<string[]>([]);
2025-03-26 13:00:55 +08:00
const contentContainerRef = useRef<HTMLDivElement>(null);
// 监控内容区域高度变化并通知父组件
useEffect(() => {
if (!contentContainerRef.current) return;
// 使用ResizeObserver监控元素大小变化
const resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
const height = entry.contentRect.height;
// 通知父组件高度变化
onHeightChange && onHeightChange(height + 20); // 添加一些缓冲空间
}
});
resizeObserver.observe(contentContainerRef.current);
return () => {
if (contentContainerRef.current) {
resizeObserver.unobserve(contentContainerRef.current);
}
};
}, [addedContents, onHeightChange]);
2025-03-26 10:31:29 +08:00
const handleAdd = () => {
2025-03-26 10:39:24 +08:00
if (content) {
2025-03-27 08:59:02 +08:00
const newContents = [...addedContents, content]; // 创建包含所有内容的新数组
onAdd(newContents); // 将累积数组传递给父组件
setAddedContents(newContents);
2025-03-26 10:31:29 +08:00
setContent('');
}
}
2025-03-26 13:00:55 +08:00
2025-03-26 13:07:23 +08:00
const handleDelete = (index: number) => {
const newContents = addedContents.filter((_, i) => i !== index);
setAddedContents(newContents);
}
2025-03-26 10:31:29 +08:00
return (
2025-03-26 13:00:55 +08:00
<div className="w-full">
<div className="flex items-center mb-3 w-full">
2025-03-26 10:31:29 +08:00
<Input
2025-03-26 10:39:24 +08:00
placeholder='请输入内容'
value={content}
onChange={(e) => setContent(e.target.value)}
2025-03-26 13:00:55 +08:00
className="flex-1 mr-2"
onPressEnter={handleAdd}
2025-03-26 10:39:24 +08:00
/>
2025-03-26 13:00:55 +08:00
<Button
type='primary'
onClick={handleAdd}
className="shrink-0"
>
</Button>
</div>
{/* 内容容器 */}
<div ref={contentContainerRef} className="w-full bg-white">
2025-03-26 10:39:24 +08:00
{addedContents.map((item, index) => (
2025-03-26 13:00:55 +08:00
<div
key={index}
2025-03-26 13:07:23 +08:00
className="flex justify-between items-center p-2 border border-gray-200 rounded bg-gray-50 mb-2 last:mb-0"
2025-03-26 13:00:55 +08:00
>
2025-03-26 13:07:23 +08:00
<span>{item}</span>
<Button
type="text"
onClick={() => handleDelete(index)}
className="text-red-500"
>
X
</Button>
2025-03-26 13:00:55 +08:00
</div>
2025-03-26 10:39:24 +08:00
))}
</div>
</div>
2025-03-26 10:31:29 +08:00
);
}
2025-03-26 13:00:55 +08:00
2025-03-26 10:31:29 +08:00
export default InfoCard;