form.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { Select, Input, DatePicker, InputNumber, Slider, Button, Switch } from 'ant-design-vue'
  2. import { isPlainObject } from 'lodash-es'
  3. import { YesOrNoOptions, addAllToOptions } from '../consts/consts'
  4. import SearchTableSelect from '../src/components/SearchTableSelect/index.vue'
  5. function merge(source: any, options: any = {}) {
  6. for (const key in options) {
  7. const value = options[key]
  8. if (isPlainObject(value)) {
  9. source[key] = source[key] || {}
  10. Object.assign(source[key], value)
  11. } else {
  12. source[key] = value
  13. }
  14. }
  15. options = undefined
  16. return source
  17. }
  18. export function genRulesRequired(type = {}, options: any = {}) {
  19. return [
  20. { required: true, message: `必填项`, trigger: ['blur', 'change'], ...type },
  21. JSON.stringify(options) === '{}' ? null : { ...options, trigger: ['blur', 'change'] }
  22. ].filter((el) => !!el)
  23. }
  24. /**
  25. * @name 输入框
  26. * @type {GenFormItemFunc<Input>}
  27. * @param {string} label label名称
  28. * @param {string} key 绑定属性名
  29. * @param {object} options 组件属性
  30. */
  31. export function genInput(label: string, key: string, options: any = {}) {
  32. return merge(
  33. {
  34. label,
  35. key,
  36. comp: Input,
  37. props: {
  38. placeholder: `请输入${label}`
  39. }
  40. },
  41. options
  42. )
  43. }
  44. export function genSwitch(label: string, key: string, options: any = {}) {
  45. return merge(
  46. {
  47. label,
  48. key,
  49. comp: Switch
  50. },
  51. options
  52. )
  53. }
  54. /**
  55. * @name 级联选择器
  56. * @type {GenFormItemFunc<Cascader>}
  57. * @param {string} label label名称
  58. * @param {string} key 绑定属性名
  59. * @param {object} options 组件属性
  60. */
  61. export function genCascader(label: string, key: string, options: any = {}) {
  62. return merge(
  63. {
  64. label,
  65. key,
  66. comp: 'Cascader',
  67. props: {
  68. placeholder: `请选择${label}`,
  69. expandTrigger: 'hover',
  70. allowClear: true,
  71. showSearch: {
  72. filter(inputValue: string, path: any[]) {
  73. return path.some(
  74. (option) => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
  75. )
  76. }
  77. }
  78. }
  79. },
  80. options
  81. )
  82. }
  83. /**
  84. * @name 多行文本
  85. * @type {GenFormItemFunc<Input.TextArea>}
  86. * @param {string} label label名称
  87. * @param {string} key 绑定属性名
  88. * @param {object} options 组件属性
  89. */
  90. export function genTextarea(label: string, key: string, options: any = {}) {
  91. return merge(
  92. {
  93. label,
  94. key,
  95. comp: Input.TextArea,
  96. props: {
  97. placeholder: `请输入${label}`
  98. }
  99. },
  100. options
  101. )
  102. }
  103. /**
  104. * @name 数字输入框
  105. * @type {GenFormItemFunc<InputNumber>}
  106. * @param {string} label label名称
  107. * @param {string} key 绑定属性名
  108. * @param {object} options 组件属性
  109. */
  110. export function genInputNumber(label: string, key: string, options: any = {}) {
  111. return merge(
  112. {
  113. label,
  114. key,
  115. comp: InputNumber,
  116. props: {
  117. placeholder: `请输入${label}`
  118. }
  119. },
  120. options
  121. )
  122. }
  123. /**
  124. * @name 日期选择器
  125. * @type {GenFormItemFunc<DatePicker>}
  126. * @param {string} label label名称
  127. * @param {string} key 绑定属性名
  128. * @param {object} options 组件属性
  129. */
  130. export function genDatePicker(label: string, key: string, options: any = {}) {
  131. return merge(
  132. {
  133. label,
  134. key,
  135. comp: DatePicker,
  136. props: {
  137. placeholder: `请选择${label}`
  138. }
  139. },
  140. options
  141. )
  142. }
  143. /**
  144. * @name 滑动输入条
  145. * @type {GenFormItemFunc<Slider>}
  146. * @param {string} label label名称
  147. * @param {string} key 绑定属性名
  148. * @param {object} options 组件属性
  149. */
  150. export function genSlider(label: string, key: string, options: any = {}) {
  151. return merge(
  152. {
  153. label,
  154. key,
  155. comp: Slider,
  156. props: {
  157. placeholder: `请选择${label}`
  158. }
  159. },
  160. options
  161. )
  162. }
  163. /**
  164. * @name 选择框
  165. * @type {GenFormItemFunc<Select>}
  166. * @param {string} label label名称
  167. * @param {string} key 绑定属性名
  168. * @param {object} options 组件属性
  169. */
  170. export function genSelect(label: string, key: string, options: any = {}) {
  171. return merge(
  172. {
  173. label,
  174. key,
  175. comp: Select,
  176. props: {
  177. placeholder: `请选择${label}`,
  178. showSearch: true,
  179. allowClear: true,
  180. options: addAllToOptions(YesOrNoOptions),
  181. filterOption: (input: string, option: any) =>
  182. option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0
  183. }
  184. },
  185. options
  186. )
  187. }
  188. /**
  189. * @name 表格选择框
  190. * @type {GenFormItemFunc}
  191. * @param {string} label label名称
  192. * @param {string} key 绑定属性名
  193. * @param {object} options 组件属性
  194. */
  195. export function genSearchTableSelect(label: string, key: string, options: any = {}) {
  196. return merge(
  197. {
  198. label,
  199. key,
  200. comp: SearchTableSelect,
  201. props: {
  202. placeholder: `请选择${label}`,
  203. allowClear: true
  204. }
  205. },
  206. options
  207. )
  208. }
  209. /**
  210. * @name 表格选择框
  211. * @type {GenFormItemFunc<{
  212. * placeholder: string;
  213. * tableModalProps: TableModelProps;
  214. * modalWidth: string;
  215. * defaultDisplay: string;
  216. * disabled: boolean;
  217. * allowClear: boolean;
  218. * }>}
  219. * @param {string} label label名称
  220. * @param {string} key 绑定属性名
  221. * @param {object} options 组件属性
  222. */
  223. /**
  224. * @name 日期范围选择
  225. * @type {GenFormItemFunc<DatePicker.RangePicker>}
  226. * @param {string} label label名称
  227. * @param {string} key 绑定属性名
  228. * @param {object} options 组件属性
  229. */
  230. export function genDateRangePicker(label: string, key: string, options: any = {}) {
  231. return merge(
  232. {
  233. label,
  234. key,
  235. comp: DatePicker.RangePicker,
  236. props: {
  237. placeholder: ['开始日期', '结束日期'],
  238. style: {
  239. width: '240px'
  240. }
  241. }
  242. },
  243. options
  244. )
  245. }
  246. /**
  247. * @name 按钮
  248. * @param {string} label label名称
  249. * @param {Function} clickFunc 点击事件
  250. * @param {{props:Omit<Button, keyof Vue>}} options 组件属性
  251. */
  252. export function genBtn(label: string, clickFunc: Function = () => {}, options: any = {}) {
  253. return merge(
  254. {
  255. comp: Button,
  256. props: {
  257. type: 'primary'
  258. },
  259. on: {
  260. click: clickFunc
  261. },
  262. content: label
  263. },
  264. options
  265. )
  266. }