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

78 lines
2.6 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 = {
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-26 10:31:29 +08:00
onAdd(content);
2025-03-26 10:39:24 +08:00
setAddedContents([...addedContents, content]);
2025-03-26 10:31:29 +08:00
setContent('');
}
}
2025-03-26 13:00:55 +08:00
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}
className="p-2 border border-gray-200 rounded bg-gray-50 mb-2 last:mb-0"
>
{item}
</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;