Browse Source

兼容request配置渲染,

main
dark 3 months ago
parent
commit
3debea4b48
  1. 37
      src/components/r-form/utils/index.tsx
  2. 14
      src/types/r-form/model.d.ts

37
src/components/r-form/utils/index.tsx

@ -8,13 +8,13 @@ import { has, get } from 'lodash'
import { mapTree } from '@/utils/tree.ts' import { mapTree } from '@/utils/tree.ts'
const getValueType = (column: RFormTypes.IColumn) => {
const getValueType = (column: ProColumns) => {
return column.valueType return column.valueType
} }
//根据type返回对应的组件 //根据type返回对应的组件
const getComponent = (column: RFormTypes.IColumn) => {
const getComponent = (column: ProColumns) => {
const type = getValueType(column) as any const type = getValueType(column) as any
switch (type) { switch (type) {
case 'input': case 'input':
@ -40,7 +40,7 @@ const getComponent = (column: RFormTypes.IColumn) => {
} }
export const transformAntdTableProColumns = (columns: RFormTypes.IColumn[], overwriteColumns?: ProColumns[]) => {
export const transformAntdTableProColumns = (columns: ProColumns[], overwriteColumns?: ProColumns[]) => {
const overwriteKeys = [] as string[] const overwriteKeys = [] as string[]
@ -51,29 +51,38 @@ export const transformAntdTableProColumns = (columns: RFormTypes.IColumn[], over
const overwrite = overwriteColumns?.find(i => i.dataIndex === item.dataIndex || item.name) const overwrite = overwriteColumns?.find(i => i.dataIndex === item.dataIndex || item.name)
if (overwrite) { if (overwrite) {
overwriteKeys.push(item.prop)
overwriteKeys.push(item.dataIndex)
} }
return { return {
...item, ...item,
request: item.dicUrl ? async (params, props) => {
const { fieldProps: { dataFiledNames } } = props
const { value, res: resKey, label, disabled: disabledKey, children } = dataFiledNames || {}
const url = `/${item.dicUrl.replace(/^:/, '/')}`
return request[item.dicMethod || 'get'](url, params).then(res => {
const data = has(res.data, resKey) ? get(res.data, resKey) : res.data
request: item.request ? async (params) => {
const { 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
return mapTree(data || [], (i: any) => { return mapTree(data || [], (i: any) => {
const disabled = has(i, disabledKey) ? get(i, disabledKey) : ('status' in i ? !convertToBool(i.status) : false)
const disabled = disabledKey && has(i, disabledKey) ? get(i, disabledKey) : ('status' in i ? !convertToBool(i.status) : false)
return { return {
title: i[label || 'label'],
label: i[label || 'label'],
value: i[value || 'id'],
title: i.label || i[label || 'name'],
label: i.label || i[label || 'name'],
value: i.value || i[value || 'id'],
disabled, disabled,
data: i data: i
} }
}, { children }) }, { children })
} catch (e) {
console.log(`${url}请求异常:`, e)
return []
}
}) })
} : undefined, } : undefined,
// renderFormItem: (_scheam, config, dom) => { // renderFormItem: (_scheam, config, dom) => {

14
src/types/r-form/model.d.ts

@ -21,5 +21,19 @@ export namespace RFormTypes {
} }
export interface IRequest {
url: string;
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
params?: any;
resultPath?: string;
fieldNames?: {
label: string;
value: string;
children: string;
disabled: string;
}
}
} }
Loading…
Cancel
Save