index.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import {
  2. ArrayItems,
  3. Editable,
  4. FormItem,
  5. FormLayout,
  6. Input,
  7. NumberPicker,
  8. Select,
  9. } from '@formily/antd';
  10. import { createSchemaField } from '@formily/react';
  11. import type { ISchema } from '@formily/json-schema';
  12. import { DataTypeList, DateTypeList } 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. 'x-validator': [
  67. {
  68. max: 64,
  69. message: '最多可输入64个字符',
  70. },
  71. {
  72. required: true,
  73. message: '请输入标识',
  74. },
  75. {
  76. validateId: true,
  77. message: 'ID只能由数字、26个英文字母或者下划线组成',
  78. },
  79. ],
  80. },
  81. name: {
  82. title: '名称',
  83. required: true,
  84. 'x-decorator': 'FormItem',
  85. 'x-component': 'Input',
  86. },
  87. valueType: {
  88. type: 'object',
  89. properties: {
  90. type: {
  91. title: '数据类型',
  92. required: true,
  93. 'x-decorator': 'FormItem',
  94. 'x-component': 'Select',
  95. enum: DataTypeList,
  96. },
  97. unit: {
  98. title: '单位',
  99. 'x-decorator': 'FormItem',
  100. 'x-component': 'Select',
  101. 'x-visible': false,
  102. enum: Store.get('units'), // 理论上首层已经就缓存了单位数据,此处可直接获取
  103. 'x-reactions': {
  104. dependencies: ['..valueType.type'],
  105. fulfill: {
  106. state: {
  107. visible: "{{['int','float','long','double'].includes($deps[0])}}",
  108. },
  109. },
  110. },
  111. },
  112. format: {
  113. title: '时间格式',
  114. 'x-decorator': 'FormItem',
  115. 'x-component': 'Select',
  116. enum: DateTypeList,
  117. 'x-visible': false,
  118. 'x-reactions': {
  119. dependencies: ['..valueType.type'],
  120. fulfill: {
  121. state: {
  122. visible: "{{['date'].includes($deps[0])}}",
  123. },
  124. },
  125. },
  126. },
  127. expands: {
  128. type: 'object',
  129. properties: {
  130. maxLength: {
  131. title: '最大长度',
  132. 'x-decorator': 'FormItem',
  133. 'x-component': 'NumberPicker',
  134. 'x-reactions': {
  135. dependencies: ['..type'],
  136. fulfill: {
  137. state: {
  138. visible: "{{['string'].includes($deps[0])}}",
  139. },
  140. },
  141. },
  142. },
  143. },
  144. },
  145. },
  146. },
  147. 'valueType.scale': {
  148. title: '精度',
  149. 'x-decorator': 'FormItem',
  150. 'x-component': 'NumberPicker',
  151. 'x-visible': false,
  152. 'x-reactions': {
  153. dependencies: ['..valueType.type'],
  154. fulfill: {
  155. state: {
  156. visible: "{{['float','double'].includes($deps[0])}}",
  157. },
  158. },
  159. },
  160. },
  161. json: {
  162. type: 'string',
  163. title: 'JSON对象',
  164. 'x-visible': false,
  165. 'x-decorator': 'FormItem',
  166. 'x-component': 'JsonParam',
  167. 'x-reactions': {
  168. dependencies: ['.valueType.type'],
  169. fulfill: {
  170. state: {
  171. visible: "{{['object'].includes($deps[0])}}",
  172. },
  173. },
  174. },
  175. },
  176. },
  177. },
  178. remove: {
  179. type: 'void',
  180. 'x-decorator': 'FormItem',
  181. 'x-component': 'ArrayItems.Remove',
  182. },
  183. },
  184. },
  185. properties: {
  186. add: {
  187. type: 'void',
  188. title: '添加参数',
  189. 'x-component': 'ArrayItems.Addition',
  190. },
  191. },
  192. },
  193. },
  194. };
  195. return <SchemaField schema={schema} />;
  196. };
  197. export default JsonParam;