import React, { useContext, useRef, useState } from 'react';
import type { ProColumns } from '@ant-design/pro-table';
import ProDescriptions from '@ant-design/pro-descriptions';
import ProProvider from '@ant-design/pro-provider';
import { Input, Space, Tag } from 'antd';
const valueEnum = {
0: 'close',
1: 'running',
2: 'online',
3: 'error',
};
export type TableListItem = {
key: number;
name: string;
status: {
label: string | number;
value: number;
}[];
};
const tableListDataSource: TableListItem = {
key: 1,
name: `TradeCode 1`,
status: [
{
value: Math.floor(Math.random() * 10),
label: valueEnum[Math.floor(Math.random() * 10) % 4],
},
{
value: Math.floor(Math.random() * 10),
label: valueEnum[Math.floor(Math.random() * 10) % 4],
},
],
};
const TagList: React.FC<{
value?: {
key: string;
label: string;
}[];
onChange?: (
value: {
key: string;
label: string;
}[],
) => void;
}> = ({ value, onChange }) => {
const ref = useRef(null);
const [newTags, setNewTags] = useState<
{
key: string;
label: string;
}[]
>([]);
const [inputValue, setInputValue] = useState('');
const handleInputChange = (e: React.ChangeEvent) => {
setInputValue(e.target.value);
};
const handleInputConfirm = () => {
let tempsTags = [...(value || [])];
if (inputValue && tempsTags.filter((tag) => tag.label === inputValue).length === 0) {
tempsTags = [...tempsTags, { key: `new-${tempsTags.length}`, label: inputValue }];
}
onChange?.(tempsTags);
setNewTags([]);
setInputValue('');
};
return (
{(value || []).concat(newTags).map((item) => (
{item.label}
))}
);
};
const columns: ProColumns[] = [
{
title: '链接',
dataIndex: 'name',
valueType: 'link',
},
{
title: '标签',
dataIndex: 'status',
key: 'status',
valueType: 'tags',
},
{
title: '操作',
key: 'option',
valueType: 'option',
render: (_, row, index, action) => [
{
action?.reload();
}}
>
刷新
,
],
},
];
export default () => {
const values = useContext(ProProvider);
return (
{text},
renderFormItem: (text, props) => (
),
},
tags: {
render: (text) => {
return (
<>
{[text].flat(1).map((item) => (
{item.label}
))}
>
);
},
renderFormItem: (text, props) => ,
},
},
}}
>
columns={columns}
editable={{}}
request={() => {
return Promise.resolve({
data: tableListDataSource,
success: true,
});
}}
title="自定义 valueType"
/>
);
};