新增虚拟模型和提供商的弹窗组件,优化模型和提供商的创建与编辑流程
This commit is contained in:
63
frontend/src/components/ui/Modal.jsx
Normal file
63
frontend/src/components/ui/Modal.jsx
Normal file
@@ -0,0 +1,63 @@
|
||||
import React, { useEffect } from 'react';
|
||||
|
||||
const Modal = ({ isOpen, onClose, title, children, size = 'md' }) => {
|
||||
useEffect(() => {
|
||||
const handleEscape = (e) => {
|
||||
if (e.key === 'Escape' && isOpen) {
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
|
||||
if (isOpen) {
|
||||
document.addEventListener('keydown', handleEscape);
|
||||
document.body.style.overflow = 'hidden';
|
||||
}
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('keydown', handleEscape);
|
||||
document.body.style.overflow = 'unset';
|
||||
};
|
||||
}, [isOpen, onClose]);
|
||||
|
||||
if (!isOpen) return null;
|
||||
|
||||
const sizeClasses = {
|
||||
sm: 'max-w-md',
|
||||
md: 'max-w-2xl',
|
||||
lg: 'max-w-4xl',
|
||||
xl: 'max-w-6xl'
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center">
|
||||
{/* 遮罩层 */}
|
||||
<div
|
||||
className="fixed inset-0 bg-black bg-opacity-50 transition-opacity"
|
||||
onClick={onClose}
|
||||
/>
|
||||
|
||||
{/* 弹窗内容 */}
|
||||
<div className={`relative bg-white rounded-lg shadow-xl ${sizeClasses[size]} w-full mx-4 max-h-[90vh] flex flex-col`}>
|
||||
{/* 头部 */}
|
||||
<div className="flex items-center justify-between p-6 border-b border-gray-200">
|
||||
<h2 className="text-xl font-semibold text-gray-900">{title}</h2>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="text-gray-400 hover:text-gray-600 transition-colors"
|
||||
>
|
||||
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 内容区域 */}
|
||||
<div className="p-6 overflow-y-auto flex-1">
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Modal;
|
||||
@@ -1,6 +1,7 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { getProviders, createProvider, updateProvider, deleteProvider } from '../api';
|
||||
import ProviderForm from './ProviderForm';
|
||||
import Modal from '../../../components/ui/Modal';
|
||||
|
||||
const ProviderList = () => {
|
||||
const [providers, setProviders] = useState([]);
|
||||
@@ -75,32 +76,35 @@ const ProviderList = () => {
|
||||
);
|
||||
}
|
||||
|
||||
if (showCreateForm) {
|
||||
return (
|
||||
<div className="bg-white p-6 rounded-lg shadow">
|
||||
<h3 className="text-lg font-medium mb-4">创建新提供商</h3>
|
||||
return (
|
||||
<>
|
||||
{/* 创建提供商弹窗 */}
|
||||
<Modal
|
||||
isOpen={showCreateForm}
|
||||
onClose={() => setShowCreateForm(false)}
|
||||
title="创建新提供商"
|
||||
size="md"
|
||||
>
|
||||
<ProviderForm
|
||||
onSave={handleCreate}
|
||||
onCancel={() => setShowCreateForm(false)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
</Modal>
|
||||
|
||||
if (editingProvider) {
|
||||
return (
|
||||
<div className="bg-white p-6 rounded-lg shadow">
|
||||
<h3 className="text-lg font-medium mb-4">编辑提供商</h3>
|
||||
{/* 编辑提供商弹窗 */}
|
||||
<Modal
|
||||
isOpen={!!editingProvider}
|
||||
onClose={() => setEditingProvider(null)}
|
||||
title="编辑提供商"
|
||||
size="md"
|
||||
>
|
||||
<ProviderForm
|
||||
provider={editingProvider}
|
||||
onSave={handleUpdate}
|
||||
onCancel={() => setEditingProvider(null)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
</Modal>
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex justify-end">
|
||||
<button
|
||||
@@ -167,7 +171,8 @@ const ProviderList = () => {
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export default ProviderList;
|
||||
@@ -2,6 +2,7 @@ import { useState } from 'react';
|
||||
import VirtualModelList from './components/VirtualModelList';
|
||||
import VirtualModelForm from './components/VirtualModelForm';
|
||||
import { createVirtualModel, updateVirtualModel } from './api';
|
||||
import Modal from '../../components/ui/Modal';
|
||||
|
||||
const VirtualModelsPage = () => {
|
||||
const [editingModel, setEditingModel] = useState(null);
|
||||
@@ -36,6 +37,34 @@ const VirtualModelsPage = () => {
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* 创建虚拟模型弹窗 */}
|
||||
<Modal
|
||||
isOpen={isCreating}
|
||||
onClose={handleCancel}
|
||||
title="新建虚拟模型"
|
||||
size="lg"
|
||||
>
|
||||
<VirtualModelForm
|
||||
model={null}
|
||||
onSave={handleSave}
|
||||
onCancel={handleCancel}
|
||||
/>
|
||||
</Modal>
|
||||
|
||||
{/* 编辑虚拟模型弹窗 */}
|
||||
<Modal
|
||||
isOpen={!!editingModel}
|
||||
onClose={handleCancel}
|
||||
title="编辑虚拟模型"
|
||||
size="lg"
|
||||
>
|
||||
<VirtualModelForm
|
||||
model={editingModel}
|
||||
onSave={handleSave}
|
||||
onCancel={handleCancel}
|
||||
/>
|
||||
</Modal>
|
||||
|
||||
<div className="flex justify-between items-center mb-4">
|
||||
<h2 className="text-xl font-semibold">虚拟模型管理</h2>
|
||||
<button
|
||||
@@ -45,13 +74,7 @@ const VirtualModelsPage = () => {
|
||||
新建虚拟模型
|
||||
</button>
|
||||
</div>
|
||||
{(isCreating || editingModel) && (
|
||||
<VirtualModelForm
|
||||
model={editingModel}
|
||||
onSave={handleSave}
|
||||
onCancel={handleCancel}
|
||||
/>
|
||||
)}
|
||||
|
||||
<VirtualModelList key={key} onEdit={handleEdit} />
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user