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.
 

113 lines
3.1 KiB

import { RFormTypes } from '@/types/r-form/model'
import { ProColumns } from '@ant-design/pro-components'
import Switch from '@/components/switch'
import { Checkbox, DatePicker, Input, Radio, Select, TreeSelect } from 'antd'
import request from '@/request'
import { convertToBool } from '@/utils'
import { has, get } from 'lodash'
import { mapTree } from '@/utils/tree.ts'
import { getI18nTitle } from '@/i18n.ts'
const getValueType = (column: ProColumns) => {
return column.valueType
}
//根据type返回对应的组件
const getComponent = (column: ProColumns) => {
const type = getValueType(column) as any
switch (type) {
case 'input':
return Input
case 'select':
return Select
case 'date':
return DatePicker
case 'switch':
return Switch
case 'radio':
return Radio
case 'checkbox':
return Checkbox
case 'textarea':
return Input.TextArea
case 'tree':
case 'treeSelect':
return TreeSelect
default:
return Input
}
}
export const transformAntdTableProColumns = (columns: ProColumns[], overwriteColumns?: ProColumns[], i18n?: string) => {
const overwriteKeys = [] as string[]
return (columns || []).map(item => {
const type = getValueType(item)
const overwrite = overwriteColumns?.find(i => i.dataIndex === item.dataIndex)
if (overwrite) {
overwriteKeys.push(item.dataIndex)
}
return {
...item,
title: getI18nTitle(i18n!, item),
request: item.request ? async (params) => {
const {
transform = true,
url: _url,
method,
params: p,
fieldNames,
resultPath
} = item.request as unknown as RFormTypes.IRequest
const { value, label, disabled: disabledKey, children = 'children' } = fieldNames || {}
const url = (_url.startsWith('/') || _url.startsWith('http')) ? _url : `/${_url}`
return request[method?.toLowerCase() || 'get'](url, {
...params,
...p,
}).then(res => {
try {
const data = resultPath && has(res.data, resultPath) ? get(res.data, resultPath) : res.data
if (!transform) {
return data
}
return mapTree(data || [], (i: any) => {
const disabled = disabledKey && has(i, disabledKey) ? get(i, disabledKey) : ('status' in i ? !convertToBool(i.status) : false)
const title = i.i18n ? getI18nTitle(i.i18n, i.label || i[label || 'name']) : i.label || i[label || 'name']
return {
title,
label: title,
value: i.value ?? i[value || 'id'],
disabled,
data: i
}
}, { children })
} catch (e) {
console.log(`${url}请求异常:`, e)
return []
}
})
} : undefined,
// renderFormItem: (_scheam, config, dom) => {
//
// },
render: (text: any, _record: any) => {
return text
},
...overwrite
} as ProColumns
}).concat(overwriteColumns?.filter(i => !overwriteKeys.includes(i.dataIndex)) || [])
}