form copy.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import { Select, Input, DatePicker, InputNumber, Slider, Button, Cascader } from 'ant-design-vue'
  2. import { isPlainObject } from 'lodash-es'
  3. import { YesOrNoOptions, addAllToOptions } from '../consts/consts'
  4. function merge2(source: any, target: any) {
  5. const { autoSetChange, context } = target
  6. const { prop, vModelKey } = source
  7. delete target.context
  8. delete target.autoSetChange
  9. for (const key in target) {
  10. const value = target[key]
  11. if (isPlainObject(value)) {
  12. source[key] = source[key] || {}
  13. Object.assign(source[key], value)
  14. } else {
  15. source[key] = value
  16. }
  17. }
  18. if (autoSetChange && context) {
  19. source.on = source.on || {}
  20. source.on.change = function (v: any) {
  21. context.searchForm[prop || vModelKey] = v
  22. context.handleSearch(1)
  23. }
  24. }
  25. target = undefined
  26. return source
  27. }
  28. /**
  29. * @name 输入框
  30. * @type {GenFormItemFunc<Input>}
  31. *
  32. */
  33. export function genInput(label: string, prop: string, options = {}) {
  34. return merge2(
  35. {
  36. comp: Input,
  37. label,
  38. prop,
  39. props: {
  40. placeholder: `请输入${label}`,
  41. allowClear: true
  42. }
  43. },
  44. options
  45. )
  46. }
  47. /**
  48. * @name 级联选择器
  49. * @type {GenFormItemFunc<Cascader>}
  50. */
  51. export function genCascader(label: string, prop: string, options = {}) {
  52. return merge2(
  53. {
  54. comp: Cascader,
  55. label,
  56. prop,
  57. props: {
  58. placeholder: '请选择',
  59. expandTrigger: 'hover',
  60. allowClear: true,
  61. showSearch: {
  62. filter(inputValue: string, path: any[]) {
  63. return path.some(
  64. (option) => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
  65. )
  66. }
  67. }
  68. }
  69. },
  70. options
  71. )
  72. }
  73. /**
  74. * @name 多行文本
  75. * @type {GenFormItemFunc<Input.TextArea>}
  76. */
  77. export function genTextarea(label: string, prop: string, options = {}) {
  78. return merge2(
  79. {
  80. comp: Input.TextArea,
  81. label,
  82. prop,
  83. props: {
  84. autoSize: { minRows: 3 },
  85. placeholder: '请输入',
  86. allowClear: true
  87. }
  88. },
  89. options
  90. )
  91. }
  92. /**
  93. * @name 数字输入框
  94. * @type {GenFormItemFunc<InputNumber>}
  95. */
  96. export function genInputNumber(label: string, prop: string, options = {}) {
  97. return merge2(
  98. {
  99. comp: InputNumber,
  100. label,
  101. prop,
  102. style: { width: '100%' },
  103. props: {
  104. min: 0,
  105. step: 1,
  106. defaultValue: 0,
  107. placeholder: '请输入',
  108. allowClear: true
  109. }
  110. },
  111. options
  112. )
  113. }
  114. /**
  115. * @name 日期选择器
  116. * @type {GenFormItemFunc<DatePicker>}
  117. */
  118. export function genDatePicker(label: string, prop: string, options = {}) {
  119. return merge2(
  120. {
  121. comp: DatePicker,
  122. label,
  123. prop,
  124. style: { width: '100%' },
  125. props: {
  126. placeholder: '请输入',
  127. allowClear: true
  128. }
  129. },
  130. options
  131. )
  132. }
  133. /**
  134. * @name 滑动输入条
  135. * @type {GenFormItemFunc<Slider>}
  136. */
  137. export function genSlider(label: string, prop: string, options = {}) {
  138. return merge2(
  139. {
  140. comp: Slider,
  141. label,
  142. prop,
  143. style: {
  144. margin: '10px 20px'
  145. },
  146. props: {
  147. placeholder: '请输入',
  148. range: true,
  149. min: 0,
  150. max: 42,
  151. step: 0.1,
  152. defaultValue: [],
  153. marks: {},
  154. allowClear: true
  155. }
  156. },
  157. options
  158. )
  159. }
  160. /**
  161. * @name 选择框
  162. * @type {GenFormItemFunc<Select>}
  163. */
  164. export function genSelect(label: string, prop: string, options = {}) {
  165. return merge2(
  166. {
  167. comp: Select,
  168. label,
  169. prop,
  170. props: {
  171. placeholder: '请选择',
  172. options: YesOrNoOptions,
  173. allowClear: true
  174. }
  175. },
  176. options
  177. )
  178. }
  179. /**
  180. * @name 表格选择框
  181. * @type {GenFormItemFunc<{
  182. * placeholder: string;
  183. * tableModalProps: TableModelProps;
  184. * modalWidth: string;
  185. * defaultDisplay: string;
  186. * disabled: boolean;
  187. * allowClear: boolean;
  188. * }>}
  189. */
  190. export function genTableSelect(label: string, prop: string, options = {}) {
  191. return merge2(
  192. {
  193. comp: SearchTableSelect,
  194. label,
  195. prop,
  196. style: {
  197. width: '100%'
  198. },
  199. props: {
  200. placeholder: `请选择${label}`,
  201. allowClear: true
  202. }
  203. },
  204. options
  205. )
  206. }
  207. /**
  208. * @name 级联选择检索
  209. * @type {GenFormItemFunc<Cascader>}
  210. */
  211. export function genSearchCascader(label: string, prop: string, options = {}) {
  212. return merge2(
  213. {
  214. label,
  215. comp: Cascader,
  216. prop,
  217. style: {
  218. width: '250px'
  219. },
  220. props: {
  221. placeholder: `请选择${label}`,
  222. expandTrigger: 'hover',
  223. allowClear: true,
  224. showSearch: {
  225. filter(inputValue: string, path: any[]) {
  226. return path.some(
  227. (option) => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
  228. )
  229. }
  230. }
  231. }
  232. },
  233. options
  234. )
  235. }
  236. /**
  237. * @name 表格选择框
  238. * @type {GenFormItemFunc<{
  239. * placeholder: string;
  240. * tableModalProps: TableModelProps;
  241. * modalWidth: string;
  242. * defaultDisplay: string;
  243. * disabled: boolean;
  244. * allowClear: boolean;
  245. * }>}
  246. */
  247. export function genSearchTableSelect(label: string, prop: string, options = {}) {
  248. return merge2(
  249. {
  250. label,
  251. comp: SearchTableSelect,
  252. prop,
  253. props: {
  254. placeholder: `请选择${label}`,
  255. allowClear: true
  256. }
  257. },
  258. options
  259. )
  260. }
  261. /**
  262. * @name 选择框可检索
  263. * @type {GenFormItemFunc<Select>}
  264. */
  265. export function genSearchSelect(label: string, prop: string, options = {}) {
  266. return merge2(
  267. {
  268. label,
  269. comp: Select,
  270. prop,
  271. props: {
  272. placeholder: `请选择${label}`,
  273. options: addAllToOptions(YesOrNoOptions),
  274. allowClear: true,
  275. showSearch: true,
  276. filterOption(
  277. input: string,
  278. option: { componentOptions: { children: { text: string }[] } }
  279. ) {
  280. return (
  281. option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  282. )
  283. }
  284. }
  285. },
  286. options
  287. )
  288. }
  289. /**
  290. * @name 日期范围选择
  291. * @type {GenFormItemFunc<DatePicker.RangePicker>}
  292. */
  293. export function genDateRangePicker(label: string, prop: string, options = {}) {
  294. return merge2(
  295. {
  296. label,
  297. comp: DatePicker.RangePicker,
  298. prop,
  299. style: {
  300. width: '300px'
  301. },
  302. props: {
  303. allowClear: true
  304. }
  305. },
  306. options
  307. )
  308. }
  309. /**
  310. * @name 按钮
  311. * @param {string} label
  312. * @param {Function} clickFunc
  313. * @param {{props:Omit<Button, keyof Vue>}} options
  314. * @returns {any}
  315. */
  316. export function genBtn(label: string, clickFunc = () => {}, options = {}) {
  317. return merge2(
  318. {
  319. comp: Button,
  320. props: {
  321. type: 'primary'
  322. },
  323. on: {
  324. click: clickFunc
  325. },
  326. content: label
  327. },
  328. options
  329. )
  330. }