Files
vf_react/src/views/Dashboard/components/EventFormModal.tsx
zdl 750547645d refactor: 抽取 EventFormModal 通用弹窗组件,修复视图切换弹窗自动打开 bug
- 新建 EventFormModal.tsx 通用弹窗组件(约 500 行)
  - 支持通过 props 配置字段显示(日期、类型、状态、重要度、标签)
  - 支持两种 API 端点(investment-plans / calendar/events)
  - 支持两种股票输入模式(tag 标签形式 / text 逗号分隔)

- 重构 EventPanel.tsx 使用 EventFormModal
  - 使用 useRef 修复弹窗自动打开 bug(视图切换时不再误触发)
  - 移除内联 Modal 代码,减少约 200 行

- 重构 CalendarPanel.tsx 使用 EventFormModal
  - 添加事件功能改用 EventFormModal
  - 保留详情弹窗(只读展示当日事件列表)
  - 移除内联表单代码,减少约 100 行

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 12:11:14 +08:00

497 lines
15 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* EventFormModal - 通用事件表单弹窗组件
* 用于新建/编辑投资计划、复盘、日历事件等
*
* 通过 props 配置差异化行为:
* - 字段显示控制(日期选择器、类型、状态、重要度、标签等)
* - API 端点配置investment-plans 或 calendar/events
* - 主题颜色和标签文案
*/
import React, { useState, useEffect } from 'react';
import {
Button,
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
ModalFooter,
ModalBody,
ModalCloseButton,
VStack,
HStack,
Icon,
Input,
InputGroup,
InputLeftElement,
FormControl,
FormLabel,
Textarea,
Select,
Tag,
TagLabel,
TagLeftIcon,
TagCloseButton,
} from '@chakra-ui/react';
import {
FiSave,
FiCalendar,
FiTrendingUp,
FiHash,
} from 'react-icons/fi';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import { usePlanningData } from './PlanningContext';
import type { InvestmentEvent, EventType, EventStatus } from '@/types';
import { logger } from '@/utils/logger';
import { getApiBase } from '@/utils/apiConfig';
dayjs.locale('zh-cn');
/**
* 表单数据接口
*/
interface FormData {
date: string;
title: string;
content: string;
type: EventType;
stocks: string[];
tags: string[];
status: EventStatus;
importance: number;
}
/**
* EventFormModal Props
*/
export interface EventFormModalProps {
/** 弹窗是否打开 */
isOpen: boolean;
/** 关闭弹窗回调 */
onClose: () => void;
/** 模式:新建或编辑 */
mode: 'create' | 'edit';
/** 事件类型(新建时使用) */
eventType?: EventType;
/** 初始日期(新建时使用,如从日历点击) */
initialDate?: string;
/** 编辑时的原始事件数据 */
editingEvent?: InvestmentEvent | null;
/** 保存成功回调 */
onSuccess: () => void;
/** 主题颜色 */
colorScheme?: string;
/** 显示标签(如 "计划"、"复盘"、"事件" */
label?: string;
/** 是否显示日期选择器 */
showDatePicker?: boolean;
/** 是否显示类型选择 */
showTypeSelect?: boolean;
/** 是否显示状态选择 */
showStatusSelect?: boolean;
/** 是否显示重要度选择 */
showImportance?: boolean;
/** 是否显示标签输入 */
showTags?: boolean;
/** 股票输入方式:'tag' 为标签形式,'text' 为逗号分隔文本 */
stockInputMode?: 'tag' | 'text';
/** API 端点 */
apiEndpoint?: 'investment-plans' | 'calendar/events';
}
/**
* EventFormModal 组件
*/
export const EventFormModal: React.FC<EventFormModalProps> = ({
isOpen,
onClose,
mode,
eventType = 'plan',
initialDate,
editingEvent,
onSuccess,
colorScheme = 'purple',
label = '事件',
showDatePicker = true,
showTypeSelect = false,
showStatusSelect = true,
showImportance = false,
showTags = true,
stockInputMode = 'tag',
apiEndpoint = 'investment-plans',
}) => {
const { toast, secondaryText } = usePlanningData();
// 表单数据
const [formData, setFormData] = useState<FormData>({
date: initialDate || dayjs().format('YYYY-MM-DD'),
title: '',
content: '',
type: eventType,
stocks: [],
tags: [],
status: 'active',
importance: 3,
});
// 股票和标签输入框
const [stockInput, setStockInput] = useState<string>('');
const [tagInput, setTagInput] = useState<string>('');
// 保存中状态
const [saving, setSaving] = useState<boolean>(false);
// 初始化表单数据
useEffect(() => {
if (mode === 'edit' && editingEvent) {
setFormData({
date: dayjs(editingEvent.event_date || editingEvent.date).format('YYYY-MM-DD'),
title: editingEvent.title,
content: editingEvent.description || editingEvent.content || '',
type: editingEvent.type || eventType,
stocks: editingEvent.stocks || [],
tags: editingEvent.tags || [],
status: editingEvent.status || 'active',
importance: editingEvent.importance || 3,
});
// 如果是文本模式,将股票数组转为逗号分隔
if (stockInputMode === 'text' && editingEvent.stocks) {
setStockInput(editingEvent.stocks.join(','));
}
} else {
// 新建模式,重置表单
setFormData({
date: initialDate || dayjs().format('YYYY-MM-DD'),
title: '',
content: '',
type: eventType,
stocks: [],
tags: [],
status: 'active',
importance: 3,
});
setStockInput('');
setTagInput('');
}
}, [mode, editingEvent, eventType, initialDate, stockInputMode]);
// 保存数据
const handleSave = async (): Promise<void> => {
try {
setSaving(true);
const base = getApiBase();
// 构建请求数据
let requestData: Record<string, unknown> = { ...formData };
// 如果是文本模式,解析股票输入
if (stockInputMode === 'text' && stockInput) {
requestData.stocks = stockInput.split(',').map(s => s.trim()).filter(s => s);
}
// 根据 API 端点调整字段名
if (apiEndpoint === 'calendar/events') {
requestData = {
title: formData.title,
description: formData.content,
type: formData.type,
importance: formData.importance,
stocks: requestData.stocks,
event_date: formData.date,
};
}
const url = mode === 'edit' && editingEvent
? `${base}/api/account/${apiEndpoint}/${editingEvent.id}`
: `${base}/api/account/${apiEndpoint}`;
const method = mode === 'edit' ? 'PUT' : 'POST';
const response = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
body: JSON.stringify(requestData),
});
if (response.ok) {
logger.info('EventFormModal', `${mode === 'edit' ? '更新' : '创建'}${label}成功`, {
itemId: editingEvent?.id,
title: formData.title,
});
toast({
title: mode === 'edit' ? '更新成功' : '创建成功',
status: 'success',
duration: 2000,
});
onClose();
onSuccess();
} else {
throw new Error('保存失败');
}
} catch (error) {
logger.error('EventFormModal', 'handleSave', error, {
itemId: editingEvent?.id,
title: formData?.title
});
toast({
title: '保存失败',
description: '无法保存数据',
status: 'error',
duration: 3000,
});
} finally {
setSaving(false);
}
};
// 添加股票Tag 模式)
const handleAddStock = (): void => {
if (stockInput.trim() && !formData.stocks.includes(stockInput.trim())) {
setFormData({
...formData,
stocks: [...formData.stocks, stockInput.trim()],
});
setStockInput('');
}
};
// 移除股票Tag 模式)
const handleRemoveStock = (index: number): void => {
setFormData({
...formData,
stocks: formData.stocks.filter((_, i) => i !== index),
});
};
// 添加标签
const handleAddTag = (): void => {
if (tagInput.trim() && !formData.tags.includes(tagInput.trim())) {
setFormData({
...formData,
tags: [...formData.tags, tagInput.trim()],
});
setTagInput('');
}
};
// 移除标签
const handleRemoveTag = (index: number): void => {
setFormData({
...formData,
tags: formData.tags.filter((_, i) => i !== index),
});
};
// 获取标题 placeholder
const getTitlePlaceholder = (): string => {
switch (formData.type) {
case 'plan':
return '例如:布局新能源板块';
case 'review':
return '例如:本周操作复盘';
case 'reminder':
return '例如:关注半导体板块';
case 'analysis':
return '例如:行业分析任务';
default:
return '请输入标题';
}
};
// 获取内容 placeholder
const getContentPlaceholder = (): string => {
switch (formData.type) {
case 'plan':
return '详细描述您的投资计划...';
case 'review':
return '详细记录您的投资复盘...';
default:
return '详细描述...';
}
};
return (
<Modal isOpen={isOpen} onClose={onClose} size="xl" closeOnOverlayClick={false} closeOnEsc={true}>
<ModalOverlay />
<ModalContent>
<ModalHeader>
{mode === 'edit' ? '编辑' : '新建'}{label}
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<VStack spacing={4}>
{/* 日期选择器 */}
{showDatePicker && (
<FormControl isRequired>
<FormLabel></FormLabel>
<InputGroup>
<InputLeftElement pointerEvents="none">
<Icon as={FiCalendar} color={secondaryText} />
</InputLeftElement>
<Input
type="date"
value={formData.date}
onChange={(e) => setFormData({ ...formData, date: e.target.value })}
/>
</InputGroup>
</FormControl>
)}
{/* 标题 */}
<FormControl isRequired>
<FormLabel></FormLabel>
<Input
value={formData.title}
onChange={(e) => setFormData({ ...formData, title: e.target.value })}
placeholder={getTitlePlaceholder()}
/>
</FormControl>
{/* 内容/描述 */}
<FormControl>
<FormLabel></FormLabel>
<Textarea
value={formData.content}
onChange={(e) => setFormData({ ...formData, content: e.target.value })}
placeholder={getContentPlaceholder()}
rows={6}
/>
</FormControl>
{/* 类型选择 */}
{showTypeSelect && (
<FormControl>
<FormLabel></FormLabel>
<Select
value={formData.type}
onChange={(e) => setFormData({ ...formData, type: e.target.value as EventType })}
>
<option value="plan"></option>
<option value="review"></option>
<option value="reminder"></option>
<option value="analysis"></option>
</Select>
</FormControl>
)}
{/* 状态选择 */}
{showStatusSelect && (
<FormControl>
<FormLabel></FormLabel>
<Select
value={formData.status}
onChange={(e) => setFormData({ ...formData, status: e.target.value as EventStatus })}
>
<option value="active"></option>
<option value="completed"></option>
<option value="cancelled"></option>
</Select>
</FormControl>
)}
{/* 重要度选择 */}
{showImportance && (
<FormControl>
<FormLabel></FormLabel>
<Select
value={formData.importance}
onChange={(e) => setFormData({ ...formData, importance: parseInt(e.target.value) })}
>
<option value={5}> </option>
<option value={4}> </option>
<option value={3}> </option>
<option value={2}> </option>
<option value={1}> </option>
</Select>
</FormControl>
)}
{/* 股票输入 - Tag 模式 */}
{stockInputMode === 'tag' && (
<FormControl>
<FormLabel></FormLabel>
<HStack>
<Input
value={stockInput}
onChange={(e) => setStockInput(e.target.value)}
placeholder="输入股票代码"
onKeyPress={(e) => e.key === 'Enter' && handleAddStock()}
/>
<Button onClick={handleAddStock}></Button>
</HStack>
<HStack mt={2} spacing={2} flexWrap="wrap">
{formData.stocks.map((stock, idx) => (
<Tag key={idx} size="sm" colorScheme="blue">
<TagLeftIcon as={FiTrendingUp} />
<TagLabel>{stock}</TagLabel>
<TagCloseButton onClick={() => handleRemoveStock(idx)} />
</Tag>
))}
</HStack>
</FormControl>
)}
{/* 股票输入 - 文本模式 */}
{stockInputMode === 'text' && (
<FormControl>
<FormLabel></FormLabel>
<Input
value={stockInput}
onChange={(e) => setStockInput(e.target.value)}
placeholder="例如600519,000858,002415"
/>
</FormControl>
)}
{/* 标签输入 */}
{showTags && (
<FormControl>
<FormLabel></FormLabel>
<HStack>
<Input
value={tagInput}
onChange={(e) => setTagInput(e.target.value)}
placeholder="输入标签"
onKeyPress={(e) => e.key === 'Enter' && handleAddTag()}
/>
<Button onClick={handleAddTag}></Button>
</HStack>
<HStack mt={2} spacing={2} flexWrap="wrap">
{formData.tags.map((tag, idx) => (
<Tag key={idx} size="sm" colorScheme={colorScheme}>
<TagLeftIcon as={FiHash} />
<TagLabel>{tag}</TagLabel>
<TagCloseButton onClick={() => handleRemoveTag(idx)} />
</Tag>
))}
</HStack>
</FormControl>
)}
</VStack>
</ModalBody>
<ModalFooter>
<Button variant="ghost" mr={3} onClick={onClose}>
</Button>
<Button
colorScheme={colorScheme}
onClick={handleSave}
isDisabled={!formData.title || (showDatePicker && !formData.date)}
isLoading={saving}
leftIcon={<FiSave />}
>
</Button>
</ModalFooter>
</ModalContent>
</Modal>
);
};
export default EventFormModal;