index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {
  2. NumberPicker,
  3. FormLayout,
  4. Editable,
  5. ArrayItems,
  6. FormItem,
  7. Input,
  8. Select,
  9. } from '@formily/antd';
  10. import { createSchemaField } from '@formily/react';
  11. import type { ISchema } from '@formily/json-schema';
  12. import { DataTypeList } from '@/pages/device/data';
  13. import { Store } from 'jetlinks-store';
  14. // 不算是自定义组件。只是抽离了JSONSchema
  15. interface Props {
  16. keys?: string;
  17. }
  18. const JsonParam = (props: Props) => {
  19. const SchemaField = createSchemaField({
  20. components: {
  21. FormItem,
  22. Input,
  23. Select,
  24. JsonParam,
  25. ArrayItems,
  26. Editable,
  27. FormLayout,
  28. NumberPicker,
  29. },
  30. });
  31. const schema: ISchema = {
  32. type: 'object',
  33. properties: {
  34. [props?.keys || 'properties']: {
  35. type: 'array',
  36. 'x-component': 'ArrayItems',
  37. 'x-decorator': 'FormItem',
  38. items: {
  39. type: 'object',
  40. 'x-decorator': 'ArrayItems.Item',
  41. properties: {
  42. sort: {
  43. type: 'void',
  44. 'x-decorator': 'FormItem',
  45. 'x-component': 'ArrayItems.SortHandle',
  46. },
  47. config: {
  48. type: 'void',
  49. title: '配置参数',
  50. 'x-decorator': 'Editable.Popover',
  51. 'x-component': 'FormLayout',
  52. 'x-component-props': {
  53. layout: 'vertical',
  54. },
  55. 'x-decorator-props': {
  56. placement: 'left',
  57. },
  58. 'x-reactions':
  59. '{{(field)=>field.title = field.query(".config.name").get("value") || field.title}}',
  60. properties: {
  61. id: {
  62. title: '标识',
  63. required: true,
  64. 'x-decorator': 'FormItem',
  65. 'x-component': 'Input',
  66. },
  67. name: {
  68. title: '名称',
  69. required: true,
  70. 'x-decorator': 'FormItem',
  71. 'x-component': 'Input',
  72. },
  73. valueType: {
  74. type: 'object',
  75. properties: {
  76. type: {
  77. title: '数据类型',
  78. required: true,
  79. 'x-decorator': 'FormItem',
  80. 'x-component': 'Select',
  81. enum: DataTypeList,
  82. },
  83. unit: {
  84. title: '单位',
  85. 'x-decorator': 'FormItem',
  86. 'x-component': 'Select',
  87. 'x-visible': false,
  88. enum: Store.get('units'), // 理论上首层已经就缓存了单位数据,此处可直接获取
  89. 'x-reactions': {
  90. dependencies: ['..valueType.type'],
  91. fulfill: {
  92. state: {
  93. visible: "{{['int','float','long','double'].includes($deps[0])}}",
  94. },
  95. },
  96. },
  97. },
  98. expands: {
  99. type: 'object',
  100. properties: {
  101. maxLength: {
  102. title: '最大长度',
  103. 'x-decorator': 'FormItem',
  104. 'x-component': 'NumberPicker',
  105. 'x-reactions': {
  106. dependencies: ['..type'],
  107. fulfill: {
  108. state: {
  109. visible: "{{['string'].includes($deps[0])}}",
  110. },
  111. },
  112. },
  113. },
  114. },
  115. },
  116. },
  117. },
  118. 'valueType.scale': {
  119. title: '精度',
  120. 'x-decorator': 'FormItem',
  121. 'x-component': 'NumberPicker',
  122. 'x-visible': false,
  123. 'x-reactions': {
  124. dependencies: ['..valueType.type'],
  125. fulfill: {
  126. state: {
  127. visible: "{{['float','double'].includes($deps[0])}}",
  128. },
  129. },
  130. },
  131. },
  132. json: {
  133. type: 'string',
  134. title: 'JSON对象',
  135. 'x-visible': false,
  136. 'x-decorator': 'FormItem',
  137. 'x-component': 'JsonParam',
  138. 'x-reactions': {
  139. dependencies: ['.valueType.type'],
  140. fulfill: {
  141. state: {
  142. visible: "{{['object'].includes($deps[0])}}",
  143. },
  144. },
  145. },
  146. },
  147. },
  148. },
  149. remove: {
  150. type: 'void',
  151. 'x-decorator': 'FormItem',
  152. 'x-component': 'ArrayItems.Remove',
  153. },
  154. },
  155. },
  156. properties: {
  157. add: {
  158. type: 'void',
  159. title: '添加参数',
  160. 'x-component': 'ArrayItems.Addition',
  161. },
  162. },
  163. },
  164. },
  165. };
  166. return <SchemaField schema={schema} />;
  167. };
  168. export default JsonParam;