util.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 = ['dateRange', 'timeRange', 'numberRange', 'resourceFormTable', 'dataDictionary', 'staff', 'roleDictionary', 'office', 'formCountersignPerson']
  6. export const threeValueComponentList = ['roleDictionary']
  7. export const generateFieldKey = (type = 'error') => {
  8. return `${type}_${uuid().split('-')[0]}` // ${Date.now()}${cacheKey}
  9. }
  10. // 甚于类型的复制方式
  11. export const getCopyItem = (item) => {
  12. const result = cloneDeep(item)
  13. const type = item.config.type
  14. const sign = generateFieldKey(type)
  15. result.id = sign
  16. result.key = sign
  17. if (twoValueComponentList.includes(type)) {
  18. result.config.otherKey = `other${toUpperFirstCase(sign)}`
  19. }
  20. if (threeValueComponentList.includes(type)) {
  21. result.config.extraKey = `extra${toUpperFirstCase(sign)}`
  22. }
  23. return result
  24. }
  25. // layout 类型的复制方式
  26. export const getCopyLayout = (layout) => {
  27. const newLayout = getCopyItem(layout) // 修改自身标记
  28. newLayout.config.options.forEach(option => { // 修改子row标记
  29. const children = option.children || []
  30. if (children.length > 0) {
  31. option.children = children.map(getCopyRow)
  32. }
  33. })
  34. return newLayout
  35. }
  36. // table 的复制方式
  37. export const getCopyTable = (table) => {
  38. const newTable = getCopyItem(table) // 修改自身标记
  39. const options = newTable.config?.options || []
  40. if (options?.length > 0) {
  41. newTable.config.options = options.map(getCopyRow)
  42. }
  43. return newTable
  44. }
  45. // 复制一列
  46. export const getCopyRow = (row) => {
  47. const type = row.config?.type
  48. if (isLayoutType(type)) {
  49. return getCopyLayout(row)
  50. } else if (type === 'table') {
  51. return getCopyTable(row)
  52. } else {
  53. return getCopyItem(row)
  54. }
  55. }
  56. export const getTableItem = (item) => {
  57. const result = cloneDeep(item)
  58. const type = item.config.type
  59. result.id = item.config.key
  60. result.key = item.config.key
  61. if (twoValueComponentList.includes(type)) {
  62. result.config.otherKey = `other${toUpperFirstCase(item.config.key)}`
  63. }
  64. return result
  65. }