util.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { v4 as uuid } from 'uuid'
  2. import { cloneDeep, toUpperFirstCase } from '@cip/utils/util'
  3. import { isLayoutType } from '@cip/components/cip-form-layout/util'
  4. // 允许有otherValue的字段
  5. export const twoValueComponentList = [
  6. 'dateRange',
  7. 'timeRange',
  8. 'numberRange',
  9. 'resourceFormTable',
  10. 'dataDictionary',
  11. 'staff',
  12. 'roleDictionary',
  13. 'office',
  14. 'formCountersignPerson',
  15. 'role'
  16. ]
  17. export const threeValueComponentList = ['roleDictionary']
  18. export const generateFieldKey = (type = 'error') => {
  19. return `${type}_${uuid().split('-')[0]}` // ${Date.now()}${cacheKey}
  20. }
  21. // 甚于类型的复制方式
  22. export const getCopyItem = (item) => {
  23. const result = cloneDeep(item)
  24. const type = item.config.type
  25. const sign = generateFieldKey(type)
  26. result.id = sign
  27. result.key = sign
  28. if (twoValueComponentList.includes(type)) {
  29. result.config.otherKey = `other${toUpperFirstCase(sign)}`
  30. }
  31. if (threeValueComponentList.includes(type)) {
  32. result.config.otherKey = [`other${toUpperFirstCase(sign)}`, `extra${toUpperFirstCase(sign)}`]
  33. }
  34. return result
  35. }
  36. // layout 类型的复制方式
  37. export const getCopyLayout = (layout) => {
  38. const newLayout = getCopyItem(layout) // 修改自身标记
  39. newLayout.config.options?.forEach?.((option) => {
  40. // 修改子row标记
  41. const children = option.children || []
  42. if (children.length > 0) {
  43. option.children = children.map(getCopyRow)
  44. }
  45. })
  46. return newLayout
  47. }
  48. // table 的复制方式
  49. export const getCopyTable = (table) => {
  50. const newTable = getCopyItem(table) // 修改自身标记
  51. const options = newTable.config?.options || []
  52. if (options?.length > 0) {
  53. newTable.config.options = options.map(getCopyRow)
  54. }
  55. return newTable
  56. }
  57. // 复制一列
  58. export const getCopyRow = (row) => {
  59. const type = row.config?.type
  60. if (isLayoutType(type)) {
  61. return getCopyLayout(row)
  62. } else if (type === 'table') {
  63. return getCopyTable(row)
  64. } else {
  65. return getCopyItem(row)
  66. }
  67. }
  68. export const getTableItem = (item) => {
  69. const result = cloneDeep(item)
  70. const type = item.config.type
  71. result.id = item.config.key
  72. result.key = item.config.key
  73. if (twoValueComponentList.includes(type)) {
  74. result.config.otherKey = `other${toUpperFirstCase(item.config.key)}`
  75. }
  76. return result
  77. }