EditTable.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { FormItem, Input, ArrayTable, Editable, Form, NumberPicker, Radio } from '@formily/antd';
  2. import { createForm } from '@formily/core';
  3. import { createSchemaField } from '@formily/react';
  4. import { Button } from 'antd';
  5. import { useEffect } from 'react';
  6. import RemoveData from './RemoveData';
  7. interface Props {
  8. onChange: (data: any) => void;
  9. data: Partial<DataSourceItem>[];
  10. table: {
  11. id: string;
  12. table: string;
  13. };
  14. }
  15. const EditTable = (props: Props) => {
  16. const SchemaField = createSchemaField({
  17. components: {
  18. FormItem,
  19. Editable,
  20. Input,
  21. ArrayTable,
  22. NumberPicker,
  23. Radio,
  24. RemoveData,
  25. },
  26. });
  27. const form = createForm({
  28. initialValues: {
  29. array: props.data,
  30. },
  31. });
  32. const schema = {
  33. type: 'object',
  34. properties: {
  35. array: {
  36. type: 'array',
  37. 'x-decorator': 'FormItem',
  38. 'x-component': 'ArrayTable',
  39. 'x-component-props': {
  40. pagination: { pageSize: 10 },
  41. scroll: { x: '100%' },
  42. },
  43. items: {
  44. type: 'object',
  45. properties: {
  46. column1: {
  47. type: 'void',
  48. 'x-component': 'ArrayTable.Column',
  49. 'x-component-props': { title: '列名' },
  50. properties: {
  51. name: {
  52. type: 'string',
  53. 'x-decorator': 'FormItem',
  54. 'x-component': 'Input',
  55. 'x-component-props': {
  56. placeholder: '请输入名称',
  57. },
  58. name: 'name',
  59. 'x-validator': [
  60. {
  61. max: 64,
  62. message: '最多可输入64个字符',
  63. },
  64. {
  65. required: true,
  66. message: '请输入名称',
  67. },
  68. ],
  69. required: true,
  70. },
  71. },
  72. },
  73. column2: {
  74. type: 'void',
  75. 'x-component': 'ArrayTable.Column',
  76. 'x-component-props': { title: '类型' },
  77. properties: {
  78. type: {
  79. type: 'string',
  80. 'x-decorator': 'FormItem',
  81. 'x-component': 'Input',
  82. 'x-component-props': {
  83. placeholder: '请输入类型',
  84. },
  85. name: 'typeId',
  86. 'x-validator': [
  87. {
  88. required: true,
  89. message: '请输入类型',
  90. },
  91. {
  92. max: 64,
  93. message: '最多可输入64个字符',
  94. },
  95. ],
  96. required: true,
  97. },
  98. },
  99. },
  100. column3: {
  101. type: 'void',
  102. 'x-component': 'ArrayTable.Column',
  103. 'x-component-props': { title: '长度' },
  104. properties: {
  105. length: {
  106. type: 'string',
  107. 'x-decorator': 'FormItem',
  108. 'x-component': 'NumberPicker',
  109. 'x-component-props': {
  110. placeholder: '请输入长度',
  111. },
  112. 'x-validator': [
  113. {
  114. required: true,
  115. message: '请输入长度',
  116. },
  117. {
  118. maximum: 99999,
  119. minimum: 1,
  120. },
  121. ],
  122. required: true,
  123. },
  124. },
  125. },
  126. column4: {
  127. type: 'void',
  128. 'x-component': 'ArrayTable.Column',
  129. 'x-component-props': { title: '精度' },
  130. properties: {
  131. scale: {
  132. type: 'string',
  133. 'x-decorator': 'FormItem',
  134. 'x-component': 'NumberPicker',
  135. 'x-component-props': {
  136. placeholder: '请输入精度',
  137. },
  138. 'x-validator': [
  139. {
  140. required: true,
  141. message: '请输入精度',
  142. },
  143. {
  144. maximum: 99999,
  145. minimum: 0,
  146. },
  147. ],
  148. required: true,
  149. },
  150. },
  151. },
  152. column5: {
  153. type: 'void',
  154. 'x-component': 'ArrayTable.Column',
  155. 'x-component-props': { width: 120, title: '不能为空' },
  156. properties: {
  157. notnull: {
  158. type: 'boolean',
  159. default: false,
  160. 'x-decorator': 'FormItem',
  161. 'x-component': 'Radio.Group',
  162. 'x-component-props': {
  163. placeholder: '请选择是否不能为空',
  164. optionType: 'button',
  165. buttonStyle: 'solid',
  166. },
  167. 'x-validator': [
  168. {
  169. required: true,
  170. message: '请选择是否不能为空',
  171. },
  172. ],
  173. required: true,
  174. enum: [
  175. {
  176. label: '是',
  177. value: true,
  178. },
  179. {
  180. label: '否',
  181. value: false,
  182. },
  183. ],
  184. },
  185. },
  186. },
  187. column6: {
  188. type: 'void',
  189. 'x-component': 'ArrayTable.Column',
  190. 'x-component-props': { title: '说明' },
  191. properties: {
  192. description: {
  193. 'x-component': 'Input',
  194. 'x-decorator': 'FormItem',
  195. 'x-component-props': {
  196. placeholder: '请输入说明',
  197. },
  198. },
  199. },
  200. },
  201. column7: {
  202. type: 'void',
  203. 'x-component': 'ArrayTable.Column',
  204. 'x-component-props': {
  205. title: '操作',
  206. dataIndex: 'operations',
  207. width: 50,
  208. },
  209. properties: {
  210. item: {
  211. type: 'void',
  212. 'x-component': 'FormItem',
  213. properties: {
  214. remove: {
  215. type: 'number',
  216. 'x-component': 'RemoveData',
  217. 'x-component-props': {
  218. type: props.table,
  219. },
  220. },
  221. },
  222. },
  223. },
  224. },
  225. },
  226. },
  227. properties: {
  228. add: {
  229. type: 'void',
  230. 'x-component': 'ArrayTable.Addition',
  231. title: '新增行',
  232. },
  233. },
  234. },
  235. },
  236. };
  237. useEffect(() => {
  238. form.setValues({ array: props?.data || [] });
  239. }, [props.data]);
  240. return (
  241. <div>
  242. <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
  243. <Button
  244. type="primary"
  245. style={{ marginBottom: 20 }}
  246. onClick={async () => {
  247. const data: any = await form.submit();
  248. props.onChange(data);
  249. }}
  250. >
  251. 保存
  252. </Button>
  253. </div>
  254. <Form form={form}>
  255. <SchemaField schema={schema} />
  256. </Form>
  257. </div>
  258. );
  259. };
  260. export default EditTable;