|
| 1 | +/** |
| 2 | + * title: 自定义创建逻辑 |
| 3 | + * description: 你可以设置 `creatorButtonProps={false}` 来关闭默认的创建效果,然后自定义创建逻辑 |
| 4 | + */ |
| 5 | +import { PlusCircleTwoTone } from '@ant-design/icons'; |
| 6 | +import type { ColumnItemList, SortableItem, SortableListRef } from '@ant-design/pro-editor'; |
| 7 | +import { ActionIcon, ColumnList } from '@ant-design/pro-editor'; |
| 8 | +import { Button, Empty } from 'antd'; |
| 9 | +import { useRef } from 'react'; |
| 10 | +import { Flexbox } from 'react-layout-kit'; |
| 11 | + |
| 12 | +import { tableColumnValueOptions } from './mock_data/options'; |
| 13 | + |
| 14 | +type SchemaItem = { |
| 15 | + title: string; |
| 16 | + valueType?: string; |
| 17 | + dataIndex: string; |
| 18 | +} & SortableItem; |
| 19 | + |
| 20 | +const INIT_VALUES = [ |
| 21 | + { id: 'orderId', dataIndex: 'orderId', valueType: 'text', title: '订单id', color: undefined }, |
| 22 | + { |
| 23 | + id: 'orderNumber', |
| 24 | + dataIndex: 'orderNumber', |
| 25 | + valueType: 'text', |
| 26 | + title: '订单号', |
| 27 | + color: undefined, |
| 28 | + }, |
| 29 | + { |
| 30 | + id: 'orderMoney', |
| 31 | + dataIndex: 'orderMoney', |
| 32 | + valueType: 'text', |
| 33 | + title: '订单金额', |
| 34 | + color: undefined, |
| 35 | + }, |
| 36 | +]; |
| 37 | + |
| 38 | +/** |
| 39 | + * 创建一个随机的索引标记符,请勿在生产环境使用 |
| 40 | + */ |
| 41 | +export const randomIndex = () => Math.random() * 10000; |
| 42 | + |
| 43 | +const columns: ColumnItemList<SchemaItem> = [ |
| 44 | + { |
| 45 | + title: '列标题', |
| 46 | + dataIndex: 'title', |
| 47 | + type: 'input', |
| 48 | + }, |
| 49 | + { |
| 50 | + title: '值类型', |
| 51 | + dataIndex: 'valueType', |
| 52 | + type: 'select', |
| 53 | + options: tableColumnValueOptions, |
| 54 | + }, |
| 55 | + { |
| 56 | + title: '字段', |
| 57 | + dataIndex: 'dataIndex', |
| 58 | + type: 'select', |
| 59 | + }, |
| 60 | +]; |
| 61 | + |
| 62 | +export default () => { |
| 63 | + const ref = useRef<SortableListRef<SchemaItem>>(null); |
| 64 | + |
| 65 | + const handleCreate = () => { |
| 66 | + const id = `id-${randomIndex()}}`; |
| 67 | + ref.current.addItem({ id, title: `new-${id}`, dataIndex: 'text' }); |
| 68 | + }; |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <Flexbox |
| 72 | + horizontal |
| 73 | + align={'center'} |
| 74 | + distribution={'space-between'} |
| 75 | + style={{ marginBottom: 16 }} |
| 76 | + > |
| 77 | + <div>列配置项</div> |
| 78 | + <ActionIcon |
| 79 | + icon={<PlusCircleTwoTone />} |
| 80 | + key={'edit'} |
| 81 | + tabIndex={-1} |
| 82 | + onClick={handleCreate} |
| 83 | + /> |
| 84 | + </Flexbox> |
| 85 | + <ColumnList<SchemaItem> |
| 86 | + ref={ref} |
| 87 | + columns={columns} |
| 88 | + renderEmpty={() => ( |
| 89 | + <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无数据"> |
| 90 | + <Button type="primary" onClick={handleCreate}> |
| 91 | + 创建自定义数据 |
| 92 | + </Button> |
| 93 | + </Empty> |
| 94 | + )} |
| 95 | + initialValues={INIT_VALUES} |
| 96 | + onChange={(values) => { |
| 97 | + console.log('onChange', values); |
| 98 | + }} |
| 99 | + creatorButtonProps={false} |
| 100 | + /> |
| 101 | + </> |
| 102 | + ); |
| 103 | +}; |
0 commit comments