|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import type { ProColumns } from "@ant-design/pro-components"; |
| 4 | +import { |
| 5 | + EditableProTable, |
| 6 | + ProCard, |
| 7 | + ProFormField, |
| 8 | + ProFormRadio, |
| 9 | +} from "@ant-design/pro-components"; |
| 10 | +import React, { useState } from "react"; |
| 11 | + |
| 12 | +const waitTime = (time: number = 100) => { |
| 13 | + return new Promise((resolve) => { |
| 14 | + setTimeout(() => { |
| 15 | + resolve(true); |
| 16 | + }, time); |
| 17 | + }); |
| 18 | +}; |
| 19 | + |
| 20 | +type DataSourceType = { |
| 21 | + id: React.Key; |
| 22 | + title?: string; |
| 23 | + readonly?: string; |
| 24 | + decs?: string; |
| 25 | + state?: string; |
| 26 | + created_at?: number; |
| 27 | + update_at?: number; |
| 28 | + children?: DataSourceType[]; |
| 29 | +}; |
| 30 | + |
| 31 | +const defaultData: DataSourceType[] = [ |
| 32 | + { |
| 33 | + id: 624748504, |
| 34 | + title: "活动名称一", |
| 35 | + readonly: "活动名称一", |
| 36 | + decs: "这个活动真好玩", |
| 37 | + state: "open", |
| 38 | + created_at: 1590486176000, |
| 39 | + update_at: 1590486176000, |
| 40 | + }, |
| 41 | + { |
| 42 | + id: 624691229, |
| 43 | + title: "活动名称二", |
| 44 | + readonly: "活动名称二", |
| 45 | + decs: "这个活动真好玩", |
| 46 | + state: "closed", |
| 47 | + created_at: 1590481162000, |
| 48 | + update_at: 1590481162000, |
| 49 | + }, |
| 50 | +]; |
| 51 | + |
| 52 | +export default function DatasetList() { |
| 53 | + const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([]); |
| 54 | + const [dataSource, setDataSource] = useState<readonly DataSourceType[]>([]); |
| 55 | + const [position, setPosition] = useState<"top" | "bottom" | "hidden">( |
| 56 | + "bottom" |
| 57 | + ); |
| 58 | + |
| 59 | + const columns: ProColumns<DataSourceType>[] = [ |
| 60 | + { |
| 61 | + title: "活动名称", |
| 62 | + dataIndex: "title", |
| 63 | + tooltip: "只读,使用form.getFieldValue获取不到值", |
| 64 | + formItemProps: (form, { rowIndex }) => { |
| 65 | + return { |
| 66 | + rules: rowIndex > 1 ? [{ required: true, message: "此项为必填项" }] : [], |
| 67 | + }; |
| 68 | + }, |
| 69 | + // 第一行不允许编辑 |
| 70 | + editable: (text, record, index) => { |
| 71 | + return index !== 0; |
| 72 | + }, |
| 73 | + width: "15%", |
| 74 | + }, |
| 75 | + { |
| 76 | + title: "活动名称二", |
| 77 | + dataIndex: "readonly", |
| 78 | + tooltip: "只读,使用form.getFieldValue可以获取到值", |
| 79 | + readonly: true, |
| 80 | + width: "15%", |
| 81 | + }, |
| 82 | + { |
| 83 | + title: "状态", |
| 84 | + key: "state", |
| 85 | + dataIndex: "state", |
| 86 | + valueType: "select", |
| 87 | + valueEnum: { |
| 88 | + all: { text: "全部", status: "Default" }, |
| 89 | + open: { |
| 90 | + text: "未解决", |
| 91 | + status: "Error", |
| 92 | + }, |
| 93 | + closed: { |
| 94 | + text: "已解决", |
| 95 | + status: "Success", |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + title: "描述", |
| 101 | + dataIndex: "decs", |
| 102 | + fieldProps: (form, { rowKey, rowIndex }) => { |
| 103 | + if (form.getFieldValue([rowKey || "", "title"]) === "不好玩") { |
| 104 | + return { |
| 105 | + disabled: true, |
| 106 | + }; |
| 107 | + } |
| 108 | + if (rowIndex > 9) { |
| 109 | + return { |
| 110 | + disabled: true, |
| 111 | + }; |
| 112 | + } |
| 113 | + return {}; |
| 114 | + }, |
| 115 | + }, |
| 116 | + { |
| 117 | + title: "活动时间", |
| 118 | + dataIndex: "created_at", |
| 119 | + valueType: "date", |
| 120 | + }, |
| 121 | + { |
| 122 | + title: "操作", |
| 123 | + valueType: "option", |
| 124 | + width: 200, |
| 125 | + render: (text, record, _, action) => [ |
| 126 | + <a |
| 127 | + key="editable" |
| 128 | + onClick={() => { |
| 129 | + action?.startEditable?.(record.id); |
| 130 | + }} |
| 131 | + > |
| 132 | + 编辑 |
| 133 | + </a>, |
| 134 | + <a |
| 135 | + key="delete" |
| 136 | + onClick={() => { |
| 137 | + setDataSource(dataSource.filter((item) => item.id !== record.id)); |
| 138 | + }} |
| 139 | + > |
| 140 | + 删除 |
| 141 | + </a>, |
| 142 | + ], |
| 143 | + }, |
| 144 | + ]; |
| 145 | + |
| 146 | + return ( |
| 147 | + <> |
| 148 | + <EditableProTable<DataSourceType> |
| 149 | + rowKey="id" |
| 150 | + headerTitle="可编辑表格" |
| 151 | + maxLength={5} |
| 152 | + scroll={{ |
| 153 | + x: 960, |
| 154 | + }} |
| 155 | + recordCreatorProps={position !== "hidden" |
| 156 | + ? { |
| 157 | + position: position as "top", |
| 158 | + record: () => ({ id: (Math.random() * 1000000).toFixed(0) }), |
| 159 | + } |
| 160 | + : false} |
| 161 | + loading={false} |
| 162 | + toolBarRender={() => [ |
| 163 | + <ProFormRadio.Group |
| 164 | + key="render" |
| 165 | + fieldProps={{ |
| 166 | + value: position, |
| 167 | + onChange: (e) => setPosition(e.target.value), |
| 168 | + }} |
| 169 | + options={[ |
| 170 | + { |
| 171 | + label: "添加到顶部", |
| 172 | + value: "top", |
| 173 | + }, |
| 174 | + { |
| 175 | + label: "添加到底部", |
| 176 | + value: "bottom", |
| 177 | + }, |
| 178 | + { |
| 179 | + label: "隐藏", |
| 180 | + value: "hidden", |
| 181 | + }, |
| 182 | + ]} />, |
| 183 | + ]} |
| 184 | + columns={columns} |
| 185 | + request={async () => ({ |
| 186 | + data: defaultData, |
| 187 | + total: 3, |
| 188 | + success: true, |
| 189 | + })} |
| 190 | + value={dataSource} |
| 191 | + onChange={setDataSource} |
| 192 | + editable={{ |
| 193 | + type: "multiple", |
| 194 | + editableKeys, |
| 195 | + onSave: async (rowKey, data, row) => { |
| 196 | + console.log(rowKey, data, row); |
| 197 | + await waitTime(2000); |
| 198 | + }, |
| 199 | + onChange: setEditableRowKeys, |
| 200 | + }} /> |
| 201 | + <ProCard title="表格数据" headerBordered collapsible defaultCollapsed> |
| 202 | + <ProFormField |
| 203 | + ignoreFormItem |
| 204 | + fieldProps={{ |
| 205 | + style: { |
| 206 | + width: "100%", |
| 207 | + }, |
| 208 | + }} |
| 209 | + mode="read" |
| 210 | + valueType="jsonCode" |
| 211 | + text={JSON.stringify(dataSource)} /> |
| 212 | + </ProCard> |
| 213 | + </> |
| 214 | + ); |
| 215 | +} |
0 commit comments