index.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {
  2. ArrayItems,
  3. Editable,
  4. FormItem,
  5. FormLayout,
  6. Input,
  7. NumberPicker,
  8. Select,
  9. } from '@formily/antd';
  10. import { createSchemaField, observer } 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. import { useAsyncDataSource } from '@/utils/util';
  15. import { service } from '@/pages/device/components/Metadata';
  16. import MetadataModel from '@/pages/device/components/Metadata/Base/model';
  17. // 不算是自定义组件。只是抽离了JSONSchema
  18. interface Props {
  19. keys?: string;
  20. }
  21. const JsonParam = observer((props: Props) => {
  22. const SchemaField = createSchemaField({
  23. components: {
  24. FormItem,
  25. Input,
  26. Select,
  27. JsonParam,
  28. ArrayItems,
  29. Editable,
  30. FormLayout,
  31. NumberPicker,
  32. },
  33. });
  34. const getUnit = () =>
  35. service.getUnit().then((resp) => {
  36. const _data = resp.result.map((item: any) => ({
  37. label: item.description,
  38. value: item.id,
  39. }));
  40. // 缓存单位数据
  41. Store.set('units', _data);
  42. return _data;
  43. });
  44. const schema: ISchema = {
  45. type: 'object',
  46. properties: {
  47. [props?.keys || 'properties']: {
  48. type: 'array',
  49. 'x-component': 'ArrayItems',
  50. 'x-decorator': 'FormItem',
  51. items: {
  52. type: 'object',
  53. 'x-decorator': 'ArrayItems.Item',
  54. properties: {
  55. sort: {
  56. type: 'void',
  57. 'x-decorator': 'FormItem',
  58. 'x-component': 'ArrayItems.SortHandle',
  59. },
  60. config: {
  61. type: 'void',
  62. title: '配置参数',
  63. 'x-decorator': 'Editable.Popover',
  64. 'x-component': 'FormLayout',
  65. 'x-component-props': {
  66. layout: 'vertical',
  67. },
  68. 'x-decorator-props': {
  69. placement: 'left',
  70. },
  71. 'x-reactions':
  72. '{{(field)=>field.title = field.query(".config.name").get("value") || field.title}}',
  73. properties: {
  74. id: {
  75. title: '标识',
  76. required: true,
  77. 'x-decorator': 'FormItem',
  78. 'x-component': 'Input',
  79. 'x-validator': [
  80. {
  81. max: 64,
  82. message: '最多可输入64个字符',
  83. },
  84. {
  85. required: true,
  86. message: '请输入标识',
  87. },
  88. {
  89. validateId: true,
  90. message: 'ID只能由数字、26个英文字母或者下划线组成',
  91. },
  92. ],
  93. },
  94. name: {
  95. title: '名称',
  96. required: true,
  97. 'x-decorator': 'FormItem',
  98. 'x-component': 'Input',
  99. },
  100. valueType: {
  101. type: 'object',
  102. properties: {
  103. type: {
  104. title: '数据类型',
  105. required: true,
  106. 'x-decorator': 'FormItem',
  107. 'x-component': 'Select',
  108. enum:
  109. MetadataModel.type === 'functions'
  110. ? DataTypeList.filter((item) => item.value !== 'file')
  111. : DataTypeList,
  112. },
  113. unit: {
  114. title: '单位',
  115. 'x-decorator': 'FormItem',
  116. 'x-component': 'Select',
  117. 'x-visible': false,
  118. 'x-component-props': {
  119. showSearch: true,
  120. allowClear: true,
  121. showArrow: true,
  122. filterOption: (input: string, option: any) =>
  123. option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
  124. },
  125. enum: Store.get('units'),
  126. 'x-reactions': [
  127. {
  128. dependencies: ['..valueType.type'],
  129. fulfill: {
  130. state: {
  131. visible: "{{['int','float','long','double'].includes($deps[0])}}",
  132. },
  133. },
  134. },
  135. '{{useAsyncDataSource(getUnit)}}',
  136. ],
  137. },
  138. format: {
  139. title: '时间格式',
  140. 'x-decorator': 'FormItem',
  141. 'x-component': 'Select',
  142. enum: DateTypeList,
  143. 'x-visible': false,
  144. 'x-reactions': {
  145. dependencies: ['..valueType.type'],
  146. fulfill: {
  147. state: {
  148. visible: "{{['date'].includes($deps[0])}}",
  149. },
  150. },
  151. },
  152. },
  153. expands: {
  154. type: 'object',
  155. properties: {
  156. maxLength: {
  157. title: '最大长度',
  158. 'x-decorator': 'FormItem',
  159. 'x-component': 'NumberPicker',
  160. 'x-reactions': {
  161. dependencies: ['..type'],
  162. fulfill: {
  163. state: {
  164. visible: "{{['string'].includes($deps[0])}}",
  165. },
  166. },
  167. },
  168. },
  169. },
  170. },
  171. },
  172. },
  173. 'valueType.scale': {
  174. title: '精度',
  175. 'x-decorator': 'FormItem',
  176. 'x-component': 'NumberPicker',
  177. 'x-visible': false,
  178. 'x-reactions': {
  179. dependencies: ['..valueType.type'],
  180. fulfill: {
  181. state: {
  182. visible: "{{['float','double'].includes($deps[0])}}",
  183. },
  184. },
  185. },
  186. },
  187. json: {
  188. type: 'string',
  189. title: 'JSON对象',
  190. 'x-visible': false,
  191. 'x-decorator': 'FormItem',
  192. 'x-component': 'JsonParam',
  193. 'x-reactions': {
  194. dependencies: ['.valueType.type'],
  195. fulfill: {
  196. state: {
  197. visible: "{{['object'].includes($deps[0])}}",
  198. },
  199. },
  200. },
  201. },
  202. },
  203. },
  204. remove: {
  205. type: 'void',
  206. 'x-decorator': 'FormItem',
  207. 'x-component': 'ArrayItems.Remove',
  208. },
  209. },
  210. },
  211. properties: {
  212. add: {
  213. type: 'void',
  214. title: '添加参数',
  215. 'x-component': 'ArrayItems.Addition',
  216. },
  217. },
  218. },
  219. },
  220. };
  221. return <SchemaField schema={schema} scope={{ useAsyncDataSource, getUnit }} />;
  222. });
  223. export default JsonParam;