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.

154 lines
4.5 KiB

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