helper.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import type { Slots } from 'vue'
  2. import { getSlot } from '@/utils/tsxHelper'
  3. import { PlaceholderModel } from './types'
  4. import { FormSchema } from '@/types/form'
  5. import { ColProps } from '@/types/components'
  6. /**
  7. *
  8. * @param schema 对应组件数据
  9. * @returns 返回提示信息对象
  10. * @description 用于自动设置placeholder
  11. */
  12. export const setTextPlaceholder = (schema: FormSchema): PlaceholderModel => {
  13. const { t } = useI18n()
  14. const textMap = ['Input', 'Autocomplete', 'InputNumber', 'InputPassword']
  15. const selectMap = ['Select', 'SelectV2', 'TimePicker', 'DatePicker', 'TimeSelect', 'TimeSelect']
  16. if (textMap.includes(schema?.component as string)) {
  17. return {
  18. placeholder: t('common.inputText') + schema.label
  19. }
  20. }
  21. if (selectMap.includes(schema?.component as string)) {
  22. // 一些范围选择器
  23. const twoTextMap = ['datetimerange', 'daterange', 'monthrange', 'datetimerange', 'daterange']
  24. if (
  25. twoTextMap.includes(
  26. (schema?.componentProps?.type || schema?.componentProps?.isRange) as string
  27. )
  28. ) {
  29. return {
  30. startPlaceholder: t('common.startTimeText'),
  31. endPlaceholder: t('common.endTimeText'),
  32. rangeSeparator: '-'
  33. }
  34. } else {
  35. return {
  36. placeholder: t('common.selectText') + schema.label
  37. }
  38. }
  39. }
  40. return {}
  41. }
  42. /**
  43. *
  44. * @param col 内置栅格
  45. * @returns 返回栅格属性
  46. * @description 合并传入进来的栅格属性
  47. */
  48. export const setGridProp = (col: ColProps = {}): ColProps => {
  49. const colProps: ColProps = {
  50. // 如果有span,代表用户优先级更高,所以不需要默认栅格
  51. ...(col.span
  52. ? {}
  53. : {
  54. xs: 24,
  55. sm: 12,
  56. md: 12,
  57. lg: 12,
  58. xl: 12
  59. }),
  60. ...col
  61. }
  62. return colProps
  63. }
  64. /**
  65. *
  66. * @param item 传入的组件属性
  67. * @returns 默认添加 clearable 属性
  68. */
  69. export const setComponentProps = (item: FormSchema): Recordable => {
  70. const notNeedClearable = ['ColorPicker']
  71. const componentProps: Recordable = notNeedClearable.includes(item.component as string)
  72. ? { ...item.componentProps }
  73. : {
  74. clearable: true,
  75. ...item.componentProps
  76. }
  77. // 需要删除额外的属性
  78. delete componentProps?.slots
  79. return componentProps
  80. }
  81. /**
  82. *
  83. * @param slots 插槽
  84. * @param slotsProps 插槽属性
  85. * @param field 字段名
  86. */
  87. export const setItemComponentSlots = (
  88. slots: Slots,
  89. slotsProps: Recordable = {},
  90. field: string
  91. ): Recordable => {
  92. const slotObj: Recordable = {}
  93. for (const key in slotsProps) {
  94. if (slotsProps[key]) {
  95. // 由于组件有可能重复,需要有一个唯一的前缀
  96. slotObj[key] = (data: Recordable) => {
  97. return getSlot(slots, `${field}-${key}`, data)
  98. }
  99. }
  100. }
  101. return slotObj
  102. }
  103. /**
  104. *
  105. * @param schema Form表单结构化数组
  106. * @param formModel FormModel
  107. * @returns FormModel
  108. * @description 生成对应的formModel
  109. */
  110. export const initModel = (schema: FormSchema[], formModel: Recordable) => {
  111. const model: Recordable = { ...formModel }
  112. schema.map((v) => {
  113. // 如果是hidden,就删除对应的值
  114. if (v.hidden) {
  115. delete model[v.field]
  116. } else if (v.component && v.component !== 'Divider') {
  117. const hasField = Reflect.has(model, v.field)
  118. // 如果先前已经有值存在,则不进行重新赋值,而是采用现有的值
  119. model[v.field] = hasField ? model[v.field] : v.value !== void 0 ? v.value : ''
  120. }
  121. })
  122. return model
  123. }
  124. /**
  125. * @param slots 插槽
  126. * @param field 字段名
  127. * @returns 返回FormIiem插槽
  128. */
  129. export const setFormItemSlots = (slots: Slots, field: string): Recordable => {
  130. const slotObj: Recordable = {}
  131. if (slots[`${field}-error`]) {
  132. slotObj['error'] = (data: Recordable) => {
  133. return getSlot(slots, `${field}-error`, data)
  134. }
  135. }
  136. if (slots[`${field}-label`]) {
  137. slotObj['label'] = (data: Recordable) => {
  138. return getSlot(slots, `${field}-label`, data)
  139. }
  140. }
  141. return slotObj
  142. }