DataRow.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { Form, FormGrid, FormItem, Input, Password, NumberPicker, Radio } from '@formily/antd';
  2. import { createForm } from '@formily/core';
  3. import type { ISchema } from '@formily/react';
  4. import { createSchemaField } from '@formily/react';
  5. import { Modal } from 'antd';
  6. interface Props {
  7. close: () => void;
  8. reload: (data: any) => void;
  9. data: Partial<DataSourceItem>;
  10. }
  11. const DataRow = (props: Props) => {
  12. const form = createForm({
  13. validateFirst: true,
  14. initialValues: props.data,
  15. });
  16. const SchemaField = createSchemaField({
  17. components: {
  18. FormItem,
  19. Input,
  20. Password,
  21. FormGrid,
  22. NumberPicker,
  23. Radio,
  24. },
  25. });
  26. const schema: ISchema = {
  27. type: 'object',
  28. properties: {
  29. layout: {
  30. type: 'void',
  31. 'x-decorator': 'FormGrid',
  32. 'x-decorator-props': {
  33. maxColumns: 2,
  34. minColumns: 2,
  35. columnGap: 24,
  36. },
  37. properties: {
  38. name: {
  39. title: '列名',
  40. type: 'string',
  41. 'x-decorator': 'FormItem',
  42. 'x-component': 'Input',
  43. 'x-decorator-props': {
  44. gridSpan: 1,
  45. },
  46. 'x-component-props': {
  47. placeholder: '请输入名称',
  48. },
  49. name: 'name',
  50. 'x-validator': [
  51. {
  52. max: 64,
  53. message: '最多可输入64个字符',
  54. },
  55. {
  56. required: true,
  57. message: '请输入名称',
  58. },
  59. ],
  60. required: true,
  61. },
  62. type: {
  63. title: '类型',
  64. type: 'string',
  65. 'x-decorator': 'FormItem',
  66. 'x-component': 'Input',
  67. 'x-decorator-props': {
  68. gridSpan: 1,
  69. },
  70. 'x-component-props': {
  71. placeholder: '请输入类型',
  72. },
  73. name: 'typeId',
  74. 'x-validator': [
  75. {
  76. required: true,
  77. message: '请输入类型',
  78. },
  79. {
  80. max: 64,
  81. message: '最多可输入64个字符',
  82. },
  83. ],
  84. required: true,
  85. },
  86. length: {
  87. title: '长度',
  88. type: 'string',
  89. 'x-decorator': 'FormItem',
  90. 'x-component': 'NumberPicker',
  91. 'x-decorator-props': {
  92. gridSpan: 1,
  93. },
  94. 'x-component-props': {
  95. placeholder: '请输入长度',
  96. },
  97. 'x-validator': [
  98. {
  99. required: true,
  100. message: '请输入长度',
  101. },
  102. {
  103. maximum: 99999,
  104. minimum: 1,
  105. },
  106. ],
  107. required: true,
  108. },
  109. scale: {
  110. title: '精度',
  111. type: 'string',
  112. 'x-decorator': 'FormItem',
  113. 'x-component': 'NumberPicker',
  114. 'x-decorator-props': {
  115. gridSpan: 1,
  116. },
  117. 'x-component-props': {
  118. placeholder: '请输入精度',
  119. },
  120. 'x-validator': [
  121. {
  122. required: true,
  123. message: '请输入精度',
  124. },
  125. {
  126. maximum: 99999,
  127. minimum: 1,
  128. },
  129. ],
  130. required: true,
  131. },
  132. notnull: {
  133. title: '不能为空',
  134. type: 'boolean',
  135. default: false,
  136. 'x-decorator': 'FormItem',
  137. 'x-component': 'Radio.Group',
  138. 'x-component-props': {
  139. placeholder: '请选择是否不能为空',
  140. optionType: 'button',
  141. buttonStyle: 'solid',
  142. },
  143. 'x-decorator-props': {
  144. gridSpan: 2,
  145. },
  146. 'x-validator': [
  147. {
  148. required: true,
  149. message: '请选择是否不能为空',
  150. },
  151. ],
  152. required: true,
  153. enum: [
  154. {
  155. label: '是',
  156. value: true,
  157. },
  158. {
  159. label: '否',
  160. value: false,
  161. },
  162. ],
  163. },
  164. description: {
  165. title: '说明',
  166. 'x-component': 'Input.TextArea',
  167. 'x-decorator': 'FormItem',
  168. 'x-decorator-props': {
  169. gridSpan: 2,
  170. },
  171. 'x-component-props': {
  172. rows: 3,
  173. showCount: true,
  174. maxLength: 200,
  175. placeholder: '请输入说明',
  176. },
  177. },
  178. },
  179. },
  180. },
  181. };
  182. const handleSave = async () => {
  183. const data: any = await form.submit();
  184. props.reload(data);
  185. };
  186. return (
  187. <Modal
  188. width={'55vw'}
  189. title={`${props.data?.id ? '编辑' : '新增'}列`}
  190. visible
  191. onCancel={() => {
  192. props.close();
  193. }}
  194. onOk={() => {
  195. handleSave();
  196. }}
  197. >
  198. <Form form={form} layout="vertical">
  199. <SchemaField schema={schema} />
  200. </Form>
  201. </Modal>
  202. );
  203. };
  204. export default DataRow;