index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { ArrayItems, FormItem, FormLayout, Input, NumberPicker, Select } from '@formily/antd';
  2. import { createSchemaField, observer } from '@formily/react';
  3. import type { ISchema } from '@formily/json-schema';
  4. import { DataTypeList, DateTypeList, FileTypeList } from '@/pages/device/data';
  5. import { Store } from 'jetlinks-store';
  6. import { useAsyncDataSource } from '@/utils/util';
  7. import { service } from '@/pages/device/components/Metadata';
  8. import MetadataModel from '@/pages/device/components/Metadata/Base/model';
  9. import BooleanEnum from '@/components/Metadata/BooleanParam';
  10. import EnumParam from '@/components/Metadata/EnumParam';
  11. import ArrayParam from '@/components/Metadata/ArrayParam';
  12. import { useIntl } from '@/.umi/plugin-locale/localeExports';
  13. import Editable from '../EditTable';
  14. // 不算是自定义组件。只是抽离了JSONSchema
  15. interface Props {
  16. keys?: string;
  17. }
  18. const JsonParam = observer((props: Props) => {
  19. const intl = useIntl();
  20. const SchemaField = createSchemaField({
  21. components: {
  22. FormItem,
  23. Input,
  24. Select,
  25. JsonParam,
  26. ArrayItems,
  27. Editable,
  28. FormLayout,
  29. NumberPicker,
  30. BooleanEnum,
  31. EnumParam,
  32. ArrayParam,
  33. },
  34. });
  35. const getUnit = () =>
  36. service.getUnit().then((resp) => {
  37. const _data = resp.result.map((item: any) => ({
  38. label: item.description,
  39. value: item.id,
  40. }));
  41. // 缓存单位数据
  42. Store.set('units', _data);
  43. return _data;
  44. });
  45. const schema: ISchema = {
  46. type: 'object',
  47. properties: {
  48. [props?.keys || 'properties']: {
  49. type: 'array',
  50. 'x-component': 'ArrayItems',
  51. 'x-decorator': 'FormItem',
  52. items: {
  53. type: 'object',
  54. 'x-decorator': 'ArrayItems.Item',
  55. properties: {
  56. sort: {
  57. type: 'void',
  58. 'x-decorator': 'FormItem',
  59. 'x-component': 'ArrayItems.SortHandle',
  60. },
  61. config: {
  62. type: 'void',
  63. title: '配置参数',
  64. 'x-decorator': 'Editable.Popover',
  65. 'x-component': 'FormLayout',
  66. 'x-component-props': {
  67. layout: 'vertical',
  68. },
  69. 'x-decorator-props': {
  70. placement: 'left',
  71. },
  72. 'x-reactions':
  73. '{{(field)=>field.title = field.query(".config.name").get("value") || field.title}}',
  74. properties: {
  75. id: {
  76. title: '标识',
  77. required: true,
  78. 'x-decorator': 'FormItem',
  79. 'x-component': 'Input',
  80. 'x-validator': [
  81. {
  82. max: 64,
  83. message: '最多可输入64个字符',
  84. },
  85. {
  86. required: true,
  87. message: '请输入标识',
  88. },
  89. {
  90. validateId: true,
  91. message: 'ID只能由数字、26个英文字母或者下划线组成',
  92. },
  93. ],
  94. },
  95. name: {
  96. title: '名称',
  97. required: true,
  98. 'x-decorator': 'FormItem',
  99. 'x-component': 'Input',
  100. },
  101. valueType: {
  102. type: 'object',
  103. properties: {
  104. type: {
  105. title: '数据类型',
  106. required: true,
  107. 'x-decorator': 'FormItem',
  108. 'x-component': 'Select',
  109. enum:
  110. MetadataModel.type === 'functions'
  111. ? DataTypeList.filter((item) => item.value !== 'file')
  112. : DataTypeList,
  113. },
  114. booleanConfig: {
  115. title: '布尔值',
  116. type: 'void',
  117. 'x-decorator': 'FormItem',
  118. 'x-component': 'BooleanEnum',
  119. 'x-reactions': {
  120. dependencies: ['..valueType.type'],
  121. fulfill: {
  122. state: {
  123. visible: "{{['boolean'].includes($deps[0])}}",
  124. },
  125. },
  126. },
  127. },
  128. enumConfig: {
  129. title: intl.formatMessage({
  130. id: 'pages.device.productDetail.metadata.enum',
  131. defaultMessage: '枚举项',
  132. }),
  133. type: 'void',
  134. 'x-decorator': 'FormItem',
  135. 'x-component': 'EnumParam',
  136. 'x-reactions': {
  137. dependencies: ['..valueType.type'],
  138. fulfill: {
  139. state: {
  140. visible: "{{['enum'].includes($deps[0])}}",
  141. },
  142. },
  143. },
  144. },
  145. elementType: {
  146. title: intl.formatMessage({
  147. id: 'pages.device.productDetail.metadata.elementConfiguration',
  148. defaultMessage: '元素配置',
  149. }),
  150. 'x-decorator': 'FormItem',
  151. 'x-component': 'ArrayParam',
  152. 'x-reactions': {
  153. dependencies: ['..valueType.type'],
  154. fulfill: {
  155. state: {
  156. visible: "{{['array'].includes($deps[0])}}",
  157. },
  158. },
  159. },
  160. },
  161. fileType: {
  162. title: intl.formatMessage({
  163. id: 'pages.device.productDetail.metadata.fileType',
  164. defaultMessage: '文件类型',
  165. }),
  166. 'x-decorator': 'FormItem',
  167. 'x-component': 'Select',
  168. 'x-visible': false,
  169. enum: FileTypeList,
  170. 'x-reactions': {
  171. dependencies: ['..valueType.type'],
  172. fulfill: {
  173. state: {
  174. visible: "{{['file'].includes($deps[0])}}",
  175. },
  176. },
  177. },
  178. },
  179. unit: {
  180. title: '单位',
  181. 'x-decorator': 'FormItem',
  182. 'x-component': 'Select',
  183. 'x-visible': false,
  184. 'x-component-props': {
  185. showSearch: true,
  186. allowClear: true,
  187. showArrow: true,
  188. filterOption: (input: string, option: any) =>
  189. option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
  190. },
  191. enum: Store.get('units'),
  192. 'x-reactions': [
  193. {
  194. dependencies: ['..valueType.type'],
  195. fulfill: {
  196. state: {
  197. visible: "{{['int','float','long','double'].includes($deps[0])}}",
  198. },
  199. },
  200. },
  201. '{{useAsyncDataSource(getUnit)}}',
  202. ],
  203. },
  204. format: {
  205. title: '时间格式',
  206. 'x-decorator': 'FormItem',
  207. 'x-component': 'Select',
  208. enum: DateTypeList,
  209. 'x-visible': false,
  210. 'x-reactions': {
  211. dependencies: ['..valueType.type'],
  212. fulfill: {
  213. state: {
  214. visible: "{{['date'].includes($deps[0])}}",
  215. },
  216. },
  217. },
  218. },
  219. expands: {
  220. type: 'object',
  221. properties: {
  222. maxLength: {
  223. title: '最大长度',
  224. 'x-decorator': 'FormItem',
  225. 'x-component': 'NumberPicker',
  226. 'x-decorator-props': {
  227. tooltip: intl.formatMessage({
  228. id: 'pages.device.productDetail.metadata.maxLength.byte',
  229. defaultMessage: '字节',
  230. }),
  231. },
  232. 'x-reactions': {
  233. dependencies: ['..type'],
  234. fulfill: {
  235. state: {
  236. visible: "{{['string','password'].includes($deps[0])}}",
  237. },
  238. },
  239. },
  240. },
  241. },
  242. },
  243. },
  244. },
  245. 'valueType.scale': {
  246. title: '精度',
  247. 'x-decorator': 'FormItem',
  248. 'x-component': 'NumberPicker',
  249. 'x-visible': false,
  250. 'x-reactions': {
  251. dependencies: ['..valueType.type'],
  252. fulfill: {
  253. state: {
  254. visible: "{{['float','double'].includes($deps[0])}}",
  255. },
  256. },
  257. },
  258. },
  259. json: {
  260. type: 'string',
  261. title: 'JSON对象',
  262. 'x-visible': false,
  263. 'x-decorator': 'FormItem',
  264. 'x-component': 'JsonParam',
  265. 'x-reactions': {
  266. dependencies: ['.valueType.type'],
  267. fulfill: {
  268. state: {
  269. visible: "{{['object'].includes($deps[0])}}",
  270. },
  271. },
  272. },
  273. },
  274. },
  275. },
  276. remove: {
  277. type: 'void',
  278. 'x-decorator': 'FormItem',
  279. 'x-component': 'ArrayItems.Remove',
  280. },
  281. },
  282. },
  283. properties: {
  284. add: {
  285. type: 'void',
  286. title: '添加参数',
  287. 'x-component': 'ArrayItems.Addition',
  288. },
  289. },
  290. },
  291. },
  292. };
  293. return <SchemaField schema={schema} scope={{ useAsyncDataSource, getUnit }} />;
  294. });
  295. export default JsonParam;