You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.6 KiB

9 months ago
  1. import { XFormTypes } from '@/types/x-form/model'
  2. import { ProColumns } from '@ant-design/pro-components'
  3. import Switch from '@/components/switch'
  4. import { Checkbox, DatePicker, Input, Radio, Select, TreeSelect } from 'antd'
  5. import request from '@/request'
  6. import { convertToBool, genProTableColumnWidthProps } from '@/utils'
  7. import { has, get } from 'lodash'
  8. import { mapTree } from '@/utils/tree.ts'
  9. const getValueType = (column: XFormTypes.IColumn) => {
  10. switch (column.type) {
  11. case 'input':
  12. return 'text'
  13. case 'select':
  14. return 'select'
  15. case 'date':
  16. return 'date'
  17. case 'switch':
  18. return 'switch'
  19. case 'radio':
  20. return 'radio'
  21. case 'checkbox':
  22. return 'checkbox'
  23. case 'textarea':
  24. return 'textarea'
  25. case 'tree':
  26. return 'treeSelect'
  27. default:
  28. return 'text'
  29. }
  30. }
  31. //根据type返回对应的组件
  32. const getComponent = (column: XFormTypes.IColumn) => {
  33. const type = getValueType(column) as any
  34. switch (type) {
  35. case 'input':
  36. return Input
  37. case 'select':
  38. return Select
  39. case 'date':
  40. return DatePicker
  41. case 'switch':
  42. return Switch
  43. case 'radio':
  44. return Radio
  45. case 'checkbox':
  46. return Checkbox
  47. case 'textarea':
  48. return Input.TextArea
  49. case 'tree':
  50. case 'treeSelect':
  51. return TreeSelect
  52. default:
  53. return Input
  54. }
  55. }
  56. export const transformAntdTableProColumns = (columns: XFormTypes.IColumn[], overwriteColumns?: ProColumns[]) => {
  57. const overwriteKeys = [] as string[]
  58. return (columns || []).map(item => {
  59. const { value, props, multiple, checkStrictly } = item
  60. const { width, fieldProps: _fieldProps } = genProTableColumnWidthProps(item.width)
  61. const fieldProps: ProColumns['fieldProps'] = {
  62. dataFiledNames: props,
  63. ...(multiple ? { multiple: true } : {}),
  64. ...(checkStrictly ? { treeCheckStrictly: true } : {}),
  65. ..._fieldProps,
  66. }
  67. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  68. const formItemProps: ProColumns['formItemProps'] = (_form, _config) => {
  69. return {
  70. rules: item.rules?.map(i => {
  71. return {
  72. required: i.required,
  73. message: i.message
  74. }
  75. }),
  76. ...(value ? { valuePropName: value } : {})
  77. }
  78. }
  79. const rowProps = item.gutter ? { gutter: item.gutter } : { gutter: [ 16, 0 ], }
  80. const colProps = item.span ? { span: item.span } : {}
  81. const type = getValueType(item)
  82. const overwrite = overwriteColumns?.find(i => i.dataIndex === item.prop)
  83. if (overwrite) {
  84. overwriteKeys.push(item.prop)
  85. }
  86. return {
  87. title: item.label,
  88. dataIndex: item.prop,
  89. key: item.prop,
  90. width,
  91. valueType: type,
  92. hideInSearch: !item.search,
  93. hideInTable: item.hide,
  94. fieldProps,
  95. formItemProps,
  96. colProps,
  97. rowProps,
  98. request: item.dicUrl ? async (params, props) => {
  99. const { fieldProps: { dataFiledNames } } = props
  100. const { value, res: resKey, label, disabled: disabledKey, children } = dataFiledNames || {}
  101. const url = `/${item.dicUrl.replace(/^:/, '/')}`
  102. return request[item.dicMethod || 'get'](url, params).then(res => {
  103. const data = has(res.data, resKey) ? get(res.data, resKey) : res.data
  104. return mapTree(data || [], (i: object) => {
  105. const disabled = has(i, disabledKey) ? get(i, disabledKey) : ('status' in i ? !convertToBool(i.status) : false)
  106. return {
  107. title: i[label || 'label'],
  108. label: i[label || 'label'],
  109. value: i[value || 'id'],
  110. disabled,
  111. data: i
  112. }
  113. }, { children })
  114. })
  115. } : undefined,
  116. renderFormItem: (_scheam, config) => {
  117. const Component = getComponent(item) as any
  118. const { options, ...props } = config as any
  119. if ([ 'tree', 'treeSelect' ].includes(_scheam.valueType as string)) {
  120. return <Component {...props} treeData={options}/>
  121. }
  122. if (_scheam.valueType as string === 'select') {
  123. return <Select {...props} options={options}/>
  124. }
  125. return <Component {...config} />
  126. },
  127. render: (text: any, record: any) => {
  128. if (type === 'switch' || type === 'checkbox' || type === 'radio') {
  129. return <Switch size={'small'} value={record[item.prop]}/>
  130. }
  131. if (item.colorFormat) {
  132. return <span style={{ color: item.colorFormat }}>{text}</span>
  133. }
  134. return text
  135. },
  136. ...overwrite
  137. } as ProColumns
  138. }).concat(overwriteColumns?.filter(i => !overwriteKeys.includes(i.dataIndex)) || [])
  139. }