自定义 tag

  1. import { useContext, useRef, useState } from 'react';
  2. import { Input, Space, Tag } from 'antd';
  3. import { ProProvider, ProTable } from '@ant-design/pro-components';
  4. import type { InputRef } from 'antd';
  5. export default () => {
  6. const values = useContext(ProProvider);
  7. return (
  8. <ProProvider.Provider
  9. value={{
  10. ...values,
  11. valueTypeMap: {
  12. link: {
  13. render: (text) => <a>{text}</a>,
  14. renderFormItem: (text, props) => (
  15. <Input placeholder="请输入链接" {...props?.fieldProps} />
  16. ),
  17. },
  18. tags: {
  19. render: (text) => {
  20. return (
  21. <>
  22. {[text].flat(1).map((item) => (
  23. <Tag key={item.value}>{item.label}</Tag>
  24. ))}
  25. </>
  26. );
  27. },
  28. renderFormItem: (text, props) => (
  29. <TagList {...props} {...props?.fieldProps} />
  30. ),
  31. },
  32. },
  33. }}
  34. >
  35. <ProTable<TableListItem, Record<string, any>, 'link' | 'tags'>
  36. columns={columns}
  37. request={() => {
  38. return Promise.resolve({
  39. total: 200,
  40. data: tableListDataSource,
  41. success: true,
  42. });
  43. }}
  44. rowKey="key"
  45. headerTitle="自定义 valueType"
  46. />
  47. </ProProvider.Provider>
  48. );
  49. };

Tag

  1. import type { ProColumns } from '@ant-design/pro-components';
  2. import { ProProvider, ProTable } from '@ant-design/pro-components';
  3. import type { InputRef } from 'antd';
  4. import { Input, Space, Tag } from 'antd';
  5. import React, { useContext, useRef, useState } from 'react';
  6. interface IProps {
  7. value?: {
  8. key: string;
  9. label: string;
  10. }[];
  11. onChange?: (
  12. value: {
  13. key: string;
  14. label: string;
  15. }[],
  16. ) => void;
  17. }
  18. const TagList: React.FC<IProps> = ({ value, onChange }) => {
  19. const ref = useRef<InputRef | null>(null);
  20. const [newTags, setNewTags] = useState<
  21. { key: string; label: string }[]
  22. >([]);
  23. const [inputValue, setInputValue] = useState<string>('');
  24. const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  25. setInputValue(e.target.value);
  26. };
  27. const handleInputConfirm = () => {
  28. let tempsTags = [...(value || [])];
  29. if (
  30. inputValue &&
  31. tempsTags.filter((tag) => tag.label === inputValue).length === 0
  32. ) {
  33. tempsTags = [
  34. ...tempsTags,
  35. { key: `new-${tempsTags.length}`, label: inputValue },
  36. ];
  37. }
  38. onChange?.(tempsTags);
  39. setNewTags([]);
  40. setInputValue('');
  41. };
  42. return (
  43. <Space>
  44. {(value || []).concat(newTags).map((item) => (
  45. <Tag key={item.key}>{item.label}</Tag>
  46. ))}
  47. <Input
  48. ref={ref}
  49. type="text"
  50. size="small"
  51. style={{ width: 78 }}
  52. value={inputValue}
  53. onChange={handleInputChange}
  54. onBlur={handleInputConfirm}
  55. onPressEnter={handleInputConfirm}
  56. />
  57. </Space>
  58. );
  59. };

columns

  1. import type { ProColumns } from '@ant-design/pro-components';
  2. export type TableItem = {
  3. key: number;
  4. name: string;
  5. status: {
  6. label: string | number;
  7. value: number;
  8. }[];
  9. };
  10. const dataSource: TableItem[] = [
  11. {
  12. "key": 1,
  13. "name": "TradeCode 1",
  14. "status": [
  15. { "value": 3, "label": "online" },
  16. { "value": 2, "label": "online" }
  17. ]
  18. },
  19. {
  20. "key": 2,
  21. "name": "TradeCode 2",
  22. "status": [
  23. { "value": 3, "label": "close" },
  24. { "value": 6, "label": "running" }
  25. ]
  26. },
  27. {
  28. "key": 3,
  29. "name": "TradeCode 3",
  30. "status": [
  31. { "value": 0, "label": "running" },
  32. ]
  33. },
  34. ]
  35. const columns: ProColumns<TableListItem, 'link' | 'tag'>[] = [
  36. {
  37. title: '链接',
  38. dataIndex: 'name',
  39. valueType: 'link',
  40. },
  41. {
  42. title: '标签',
  43. dataIndex: 'status',
  44. valueType: 'tag',
  45. },
  46. {
  47. title: '操作',
  48. key: 'option',
  49. valueType: 'option',
  50. render: (_, row, index, action) => [
  51. <a
  52. key="a"
  53. onClick={() => {
  54. action?.startEditable(row.key);
  55. }}
  56. >
  57. 编辑
  58. </a>,
  59. ],
  60. },
  61. ];

自定义 valueType 参考

https://github.com/ant-design/pro-components/blob/master/packages/table/src/demos/customization-value-type.tsx