移除分类图片
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import React, {useEffect, useState} from 'react';
|
||||
import {Button, Image, Tag, Typography} from 'antd';
|
||||
import React, {useEffect, useMemo, useState} from 'react';
|
||||
import {Button, Form, Tag, TreeSelect, Typography} from 'antd';
|
||||
import type {FormInstance} from 'antd';
|
||||
import {NodeExpandOutlined} from '@ant-design/icons';
|
||||
import XinTable from '@/components/XinTable';
|
||||
import type { XinTableColumn, XinTableProps } from '@/components/XinTable/typings.ts';
|
||||
@@ -10,6 +11,45 @@ import {getCategoryTable, getCategoryTree} from '@/api/product/category.ts';
|
||||
|
||||
const { Title, Text } = Typography;
|
||||
|
||||
/**
|
||||
* 上级分类选择(分类最多二级):
|
||||
* - 二级分类禁选(不能作为上级,否则出现三级)
|
||||
* - 编辑时禁选自身
|
||||
* - 编辑的分类含子分类时禁选所有一级分类(只能保持顶级,否则其子分类会变成三级)
|
||||
*/
|
||||
const ParentCategorySelect: React.FC<{
|
||||
form: FormInstance;
|
||||
tree: IProductCategoryTree[];
|
||||
value?: number;
|
||||
onChange?: (value: number) => void;
|
||||
}> = ({ form, tree, value, onChange }) => {
|
||||
// id/children 由 XinTable 编辑时 setFieldsValue(record) 写入(非表单项,需传 form 监听)
|
||||
const editingId = Form.useWatch('id', form);
|
||||
const children = Form.useWatch('children', form);
|
||||
const hasChildren = Array.isArray(children) && children.length > 0;
|
||||
|
||||
const treeData = useMemo(() => {
|
||||
const walk = (nodes: IProductCategoryTree[], depth: number): IProductCategoryTree[] =>
|
||||
nodes.map((node) => ({
|
||||
...node,
|
||||
disabled: depth >= 2 || node.id === editingId || (hasChildren && depth >= 1),
|
||||
children: node.children?.length ? walk(node.children, depth + 1) : node.children,
|
||||
}));
|
||||
return [{ id: 0, name: '顶级分类', children: walk(tree, 1) }];
|
||||
}, [tree, editingId, hasChildren]);
|
||||
|
||||
return (
|
||||
<TreeSelect
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
treeData={treeData}
|
||||
fieldNames={{ label: 'name', value: 'id', children: 'children' }}
|
||||
placeholder="默认顶级分类"
|
||||
treeDefaultExpandAll
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 递归收集全部节点 id(用于展开整棵树)
|
||||
*/
|
||||
@@ -52,16 +92,10 @@ const ProductCategoryPage: React.FC = () => {
|
||||
{
|
||||
title: '上级分类',
|
||||
dataIndex: 'parent_id',
|
||||
valueType: 'treeSelect',
|
||||
hideInTable: true,
|
||||
hideInSearch: true,
|
||||
initialValue: 0,
|
||||
fieldProps: {
|
||||
treeData: [{ id: 0, name: '顶级分类', children: categoryTree }],
|
||||
fieldNames: { label: 'name', value: 'id', children: 'children' },
|
||||
placeholder: '默认顶级分类',
|
||||
treeDefaultExpandAll: true,
|
||||
},
|
||||
fieldRender: (form) => <ParentCategorySelect form={form} tree={categoryTree} />,
|
||||
},
|
||||
{
|
||||
title: '排序',
|
||||
@@ -90,31 +124,6 @@ const ProductCategoryPage: React.FC = () => {
|
||||
},
|
||||
align: 'center',
|
||||
},
|
||||
{
|
||||
title: '分类图标',
|
||||
dataIndex: 'icon_id',
|
||||
valueType: 'image',
|
||||
fieldProps: {
|
||||
action: '/product/category/upload',
|
||||
mode: 'single',
|
||||
maxCount: 1,
|
||||
changeType: "id"
|
||||
},
|
||||
render: (_, record: IProductCategory) => {
|
||||
const url = record.icon_url
|
||||
if (!url) return '-';
|
||||
return (
|
||||
<Image
|
||||
src={url}
|
||||
width={32}
|
||||
height={32}
|
||||
style={{ objectFit: 'cover', borderRadius: 4 }}
|
||||
/>
|
||||
);
|
||||
},
|
||||
align: 'center',
|
||||
hideInSearch: true,
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'updated_at',
|
||||
|
||||
Reference in New Issue
Block a user