index.tsx 7.4 KB

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