index.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { createSchemaField } from '@formily/react';
  2. import { Editable, FormItem, FormLayout, Input, NumberPicker, Select } from '@formily/antd';
  3. import type { ISchema } from '@formily/json-schema';
  4. import './index.less';
  5. import { DataTypeList, DateTypeList, FileTypeList } from '@/pages/device/data';
  6. import { Store } from 'jetlinks-store';
  7. import JsonParam from '@/components/Metadata/JsonParam';
  8. import EnumParam from '@/components/Metadata/EnumParam';
  9. import BooleanEnum from '@/components/Metadata/BooleanParam';
  10. const ArrayParam = () => {
  11. const SchemaField = createSchemaField({
  12. components: {
  13. FormItem,
  14. Input,
  15. Select,
  16. Editable,
  17. FormLayout,
  18. NumberPicker,
  19. JsonParam,
  20. ArrayParam,
  21. EnumParam,
  22. BooleanEnum,
  23. },
  24. });
  25. const schema: ISchema = {
  26. type: 'object',
  27. properties: {
  28. config: {
  29. type: 'void',
  30. title: '配置元素',
  31. 'x-component': 'FormLayout',
  32. 'x-component-props': {
  33. layout: 'vertical',
  34. },
  35. 'x-decorator': 'Editable.Popover',
  36. 'x-decorator-props': {
  37. className: 'config-array',
  38. placement: 'left',
  39. },
  40. 'x-reactions':
  41. "{{(field) => field.title = field.query('.void.date2').get('value') || field.title}}",
  42. properties: {
  43. type: {
  44. title: '元素类型',
  45. 'x-decorator': 'FormItem',
  46. 'x-component': 'Select',
  47. enum: DataTypeList.filter((item) =>
  48. ['int', 'long', 'float', 'double', 'string', 'boolean', 'date'].includes(item.value),
  49. ),
  50. 'x-validator': [
  51. {
  52. required: true,
  53. message: '请选择元素类型',
  54. },
  55. ],
  56. },
  57. scale: {
  58. title: '精度',
  59. 'x-decorator': 'FormItem',
  60. 'x-component': 'NumberPicker',
  61. 'x-component-props': {
  62. min: 1,
  63. },
  64. default: 2,
  65. 'x-validator': [
  66. {
  67. format: 'integer',
  68. message: '请输入0-2147483647之间的正整数',
  69. },
  70. {
  71. max: 2147483647,
  72. message: '请输入0-2147483647之间的正整数',
  73. },
  74. {
  75. min: 0,
  76. message: '请输入0-2147483647之间的正整数',
  77. },
  78. ],
  79. 'x-reactions': {
  80. dependencies: ['.type'],
  81. fulfill: {
  82. state: {
  83. visible: "{{['float','double'].includes($deps[0])}}",
  84. },
  85. },
  86. },
  87. },
  88. unit: {
  89. title: '单位',
  90. 'x-decorator': 'FormItem',
  91. 'x-component': 'Select',
  92. 'x-visible': false,
  93. 'x-component-props': {
  94. showSearch: true,
  95. allowClear: true,
  96. showArrow: true,
  97. filterOption: (input: string, option: any) =>
  98. option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
  99. },
  100. enum: Store.get('units'),
  101. 'x-reactions': {
  102. dependencies: ['.type'],
  103. fulfill: {
  104. state: {
  105. visible: "{{['int','float','long','double'].includes($deps[0])}}",
  106. },
  107. },
  108. },
  109. },
  110. format: {
  111. title: '时间格式',
  112. 'x-decorator': 'FormItem',
  113. 'x-component': 'Select',
  114. enum: DateTypeList,
  115. default: 'string',
  116. 'x-validator': [
  117. {
  118. required: true,
  119. message: '请选择时间格式',
  120. },
  121. ],
  122. 'x-reactions': {
  123. dependencies: ['.type'],
  124. fulfill: {
  125. state: {
  126. visible: "{{['date'].includes($deps[0])}}",
  127. },
  128. },
  129. },
  130. },
  131. expands: {
  132. type: 'object',
  133. properties: {
  134. maxLength: {
  135. title: '最大长度',
  136. 'x-decorator': 'FormItem',
  137. 'x-component': 'NumberPicker',
  138. 'x-component-props': {
  139. min: 1,
  140. },
  141. 'x-decorator-props': {
  142. tooltip: '字节',
  143. },
  144. 'x-validator': [
  145. {
  146. format: 'integer',
  147. message: '请输入1-2147483647之间的正整数',
  148. },
  149. {
  150. max: 2147483647,
  151. message: '请输入1-2147483647之间的正整数',
  152. },
  153. {
  154. min: 1,
  155. message: '请输入1-2147483647之间的正整数',
  156. },
  157. ],
  158. 'x-reactions': {
  159. dependencies: ['..type'],
  160. fulfill: {
  161. state: {
  162. visible: "{{['string','password'].includes($deps[0])}}",
  163. },
  164. },
  165. },
  166. },
  167. },
  168. },
  169. booleanConfig: {
  170. title: '布尔值',
  171. 'x-decorator': 'FormItem',
  172. 'x-component': 'BooleanEnum',
  173. type: 'void',
  174. 'x-reactions': {
  175. dependencies: ['.type'],
  176. fulfill: {
  177. state: {
  178. visible: "{{['boolean'].includes($deps[0])}}",
  179. },
  180. },
  181. },
  182. },
  183. enumConfig: {
  184. title: '枚举项',
  185. type: 'void',
  186. 'x-decorator': 'FormItem',
  187. 'x-component': 'EnumParam',
  188. 'x-reactions': {
  189. dependencies: ['.type'],
  190. fulfill: {
  191. state: {
  192. visible: "{{['enum'].includes($deps[0])}}",
  193. },
  194. },
  195. },
  196. },
  197. fileType: {
  198. title: '文件类型',
  199. 'x-decorator': 'FormItem',
  200. 'x-component': 'Select',
  201. 'x-visible': false,
  202. enum: FileTypeList,
  203. 'x-reactions': {
  204. dependencies: ['.type'],
  205. fulfill: {
  206. state: {
  207. visible: "{{['file'].includes($deps[0])}}",
  208. },
  209. },
  210. },
  211. },
  212. jsonConfig: {
  213. title: 'JSON对象',
  214. type: 'void',
  215. 'x-decorator': 'FormItem',
  216. 'x-component': 'JsonParam',
  217. 'x-reactions': {
  218. dependencies: ['.type'],
  219. fulfill: {
  220. state: {
  221. visible: "{{['object'].includes($deps[0])}}",
  222. },
  223. },
  224. },
  225. },
  226. description: {
  227. title: '说明',
  228. 'x-decorator': 'FormItem',
  229. 'x-component': 'Input.TextArea',
  230. },
  231. },
  232. },
  233. },
  234. };
  235. return <SchemaField schema={schema} />;
  236. };
  237. export default ArrayParam;