feat: 优化首页轮播图组件,采用 Pro Components 规范和组件化架构
主要优化内容: - 采用标准 Pro Components 替代自定义 UI 组件 - 重构表格列配置,支持图片预览和链接点击 - 分离创建和编辑表单为独立组件 (CreateForm, HomeCarouselForm) - 创建 CreateForm 组件支持图片上传和实时预览 - 改进搜索功能,使用标准 QueryFilter 组件 - 新增规范的样式文件 (index.less),美化页面展示 - 提取常量定义 (constants.ts),便于维护和国际化 - 完善表单验证规则和错误提示 - 优化状态管理和变量命名规范 - 使用 ActionRef 管理表格刷新 优化对标 react-web 中的最佳实践,提升代码质量和可维护性。 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,68 +1,171 @@
|
||||
import React, { useState } from 'react';
|
||||
import { PageContainer, ProTable } from '@ant-design/pro-components';
|
||||
import { Popconfirm, Space, Tag, message } from 'antd';
|
||||
import { ActionBar, GlassCard, GradientButton, SearchBarPlus } from '@/components/ui';
|
||||
import { PlusOutlined } from '@ant-design/icons';
|
||||
import React, { useRef, useState } from 'react';
|
||||
import { PageContainer, ProTable, QueryFilter, ProFormText, ProFormSelect } from '@ant-design/pro-components';
|
||||
import { Popconfirm, Space, Button, message, Typography, Image as AntImage } from 'antd';
|
||||
import { PlusOutlined, DeleteOutlined, EditOutlined, UploadOutlined } from '@ant-design/icons';
|
||||
import type { ActionType, ProColumns } from '@ant-design/pro-components';
|
||||
import { ordersApi } from '@/services/orders';
|
||||
import HomeCarouselForm, { HomeCarouselItem } from './components/HomeCarouselForm';
|
||||
import HomeCarouselForm from './components/HomeCarouselForm';
|
||||
import CreateForm from './components/CreateForm';
|
||||
import type { HomeCarouselItem } from './components/HomeCarouselForm';
|
||||
import styles from './index.less';
|
||||
|
||||
const { Link } = Typography;
|
||||
|
||||
// 常量定义
|
||||
const STATUS_OPTIONS = [
|
||||
{ label: '启用', value: 1 },
|
||||
{ label: '禁用', value: 0 },
|
||||
];
|
||||
|
||||
const HomeCarouselPage: React.FC = () => {
|
||||
const actionRef = useRef<ActionType>();
|
||||
const [createOpen, setCreateOpen] = useState(false);
|
||||
const [editOpen, setEditOpen] = useState(false);
|
||||
const [current, setCurrent] = useState<HomeCarouselItem | undefined>(undefined);
|
||||
const [params, setParams] = useState<Record<string, any>>({});
|
||||
const [searchParams, setSearchParams] = useState<Record<string, any>>({});
|
||||
|
||||
const columns: any[] = [
|
||||
{ dataIndex: 'index', valueType: 'indexBorder', width: 48 },
|
||||
{ title: '图片', dataIndex: 'imageUrl', render: (v: string) => <img src={v} alt="img" style={{ width: 80, height: 40, objectFit: 'cover' }} /> },
|
||||
{ title: '跳转链接', dataIndex: 'linkUrl', ellipsis: true },
|
||||
{ title: '排序', dataIndex: 'sort' },
|
||||
{ title: '状态', dataIndex: 'status', render: (v: number) => <Tag color={v === 1 ? 'green' : 'default'}>{v === 1 ? '启用' : '禁用'}</Tag> },
|
||||
// 处理删除
|
||||
const handleDelete = async (record: HomeCarouselItem) => {
|
||||
const hide = message.loading('删除中...');
|
||||
try {
|
||||
await ordersApi.deleteHomeCarousel(record.id!);
|
||||
message.success('删除成功');
|
||||
actionRef.current?.reload();
|
||||
return true;
|
||||
} catch (e) {
|
||||
message.error('删除失败');
|
||||
return false;
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
};
|
||||
|
||||
const columns: ProColumns<HomeCarouselItem>[] = [
|
||||
{
|
||||
title: '操作', key: 'action',
|
||||
render: (_: any, record: HomeCarouselItem) => (
|
||||
dataIndex: 'index',
|
||||
valueType: 'indexBorder',
|
||||
width: 48,
|
||||
},
|
||||
{
|
||||
title: '图片',
|
||||
dataIndex: 'imageUrl',
|
||||
width: 120,
|
||||
render: (imageUrl: string) => (
|
||||
<AntImage
|
||||
src={imageUrl}
|
||||
width={100}
|
||||
placeholder={
|
||||
<div style={{ background: '#f5f5f5', height: 100, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
||||
加载中...
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: '跳转链接',
|
||||
dataIndex: 'linkUrl',
|
||||
ellipsis: true,
|
||||
render: (url: string) => (
|
||||
<Link href={url} target="_blank">{url}</Link>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: '排序',
|
||||
dataIndex: 'sort',
|
||||
width: 80,
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'status',
|
||||
width: 80,
|
||||
valueEnum: {
|
||||
1: { text: '启用', status: 'Success' },
|
||||
0: { text: '禁用', status: 'Error' },
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
valueType: 'option',
|
||||
width: 180,
|
||||
render: (_, record) => (
|
||||
<Space>
|
||||
<a onClick={() => { setCurrent(record); setEditOpen(true); }}>编辑</a>
|
||||
<Popconfirm title={`确认删除?`} onConfirm={async () => {
|
||||
const hide = message.loading('删除中...');
|
||||
try {
|
||||
await ordersApi.deleteHomeCarousel(record.id!);
|
||||
message.success('删除成功');
|
||||
} catch (e) {
|
||||
message.error('删除失败');
|
||||
} finally { hide(); }
|
||||
}}>
|
||||
<a style={{ color: '#1677ff' }}>删除</a>
|
||||
<Link
|
||||
onClick={() => {
|
||||
setCurrent(record);
|
||||
setEditOpen(true);
|
||||
}}
|
||||
>
|
||||
<EditOutlined /> 编辑
|
||||
</Link>
|
||||
<Popconfirm
|
||||
title="确认删除?"
|
||||
description="删除后将无法恢复,请谨慎操作"
|
||||
onConfirm={() => handleDelete(record)}
|
||||
>
|
||||
<Link><DeleteOutlined /> 删除</Link>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
)
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<PageContainer header={{ title: '首页轮播' }}>
|
||||
<SearchBarPlus
|
||||
defaultCollapsed
|
||||
<PageContainer>
|
||||
<QueryFilter
|
||||
defaultCollapsed={false}
|
||||
split
|
||||
span={8}
|
||||
onTenantChanged={() => { /* 仅触发表格刷新 */ }}
|
||||
onFinish={async (values) => setParams(values)}
|
||||
className={styles['query-filter']}
|
||||
onFinish={async (values) => {
|
||||
setSearchParams(values);
|
||||
actionRef.current?.reload();
|
||||
}}
|
||||
style={{ marginBottom: 16 }}
|
||||
>
|
||||
<ProFormText
|
||||
name="title"
|
||||
label="标题"
|
||||
placeholder="请输入标题关键字"
|
||||
fieldProps={{
|
||||
allowClear: true,
|
||||
}}
|
||||
/>
|
||||
<ProFormSelect
|
||||
name="status"
|
||||
label="状态"
|
||||
placeholder="请选择状态"
|
||||
options={STATUS_OPTIONS}
|
||||
fieldProps={{
|
||||
allowClear: true,
|
||||
}}
|
||||
/>
|
||||
</QueryFilter>
|
||||
|
||||
<ProTable<HomeCarouselItem>
|
||||
rowKey="id"
|
||||
headerTitle="轮播图列表"
|
||||
actionRef={actionRef}
|
||||
columns={columns}
|
||||
request={(p) => ordersApi.homeCarousel({ ...(p as any), ...searchParams })}
|
||||
search={false}
|
||||
pagination={{
|
||||
defaultPageSize: 10,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
}}
|
||||
toolBarRender={() => [
|
||||
<Button
|
||||
key="add"
|
||||
type="primary"
|
||||
onClick={() => setCreateOpen(true)}
|
||||
>
|
||||
<PlusOutlined /> 新增轮播图
|
||||
</Button>,
|
||||
]}
|
||||
/>
|
||||
|
||||
<ActionBar left={<GradientButton size="middle" type="primary" icon={<PlusOutlined />} onClick={() => setCreateOpen(true)}>新增</GradientButton>} />
|
||||
|
||||
<GlassCard>
|
||||
<ProTable<HomeCarouselItem>
|
||||
rowKey="id"
|
||||
request={(p) => ordersApi.homeCarouselList({ ...(p as any), ...params })}
|
||||
columns={columns}
|
||||
search={false}
|
||||
pagination={{ showSizeChanger: true, showQuickJumper: true }}
|
||||
/>
|
||||
</GlassCard>
|
||||
|
||||
<HomeCarouselForm
|
||||
title="新增轮播"
|
||||
<CreateForm
|
||||
open={createOpen}
|
||||
onOpenChange={setCreateOpen}
|
||||
onFinish={async (values) => {
|
||||
@@ -70,16 +173,18 @@ const HomeCarouselPage: React.FC = () => {
|
||||
try {
|
||||
await ordersApi.saveOrUpdateHomeCarousel(values);
|
||||
message.success('新增成功');
|
||||
actionRef.current?.reload();
|
||||
return true;
|
||||
} catch (e) {
|
||||
message.error('保存失败');
|
||||
return false;
|
||||
} finally { hide(); }
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
<HomeCarouselForm
|
||||
title="编辑轮播"
|
||||
open={editOpen}
|
||||
initialValues={current}
|
||||
onOpenChange={setEditOpen}
|
||||
@@ -88,11 +193,14 @@ const HomeCarouselPage: React.FC = () => {
|
||||
try {
|
||||
await ordersApi.saveOrUpdateHomeCarousel({ ...(current as any), ...values });
|
||||
message.success('修改成功');
|
||||
actionRef.current?.reload();
|
||||
return true;
|
||||
} catch (e) {
|
||||
message.error('修改失败');
|
||||
message.error('保存失败');
|
||||
return false;
|
||||
} finally { hide(); }
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</PageContainer>
|
||||
|
||||
Reference in New Issue
Block a user