|
|
@ -15,12 +15,12 @@ |
|
|
|
* limitations under the License. |
|
|
|
* limitations under the License. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
import { toRef } from 'vue' |
|
|
|
import { toRef, Ref } from 'vue' |
|
|
|
import { formatValidate } from './utils' |
|
|
|
import { formatValidate } from './utils' |
|
|
|
import getField from './fields/get-field' |
|
|
|
import getField from './fields/get-field' |
|
|
|
import { omit } from 'lodash' |
|
|
|
import { omit } from 'lodash' |
|
|
|
import type { FormRules } from 'naive-ui' |
|
|
|
import type { FormRules } from 'naive-ui' |
|
|
|
import type { IJsonItem } from './types' |
|
|
|
import type { IFormItem, IJsonItem } from './types' |
|
|
|
|
|
|
|
|
|
|
|
export default function getElementByJson( |
|
|
|
export default function getElementByJson( |
|
|
|
json: IJsonItem[], |
|
|
|
json: IJsonItem[], |
|
|
@ -28,23 +28,23 @@ export default function getElementByJson( |
|
|
|
) { |
|
|
|
) { |
|
|
|
const rules: FormRules = {} |
|
|
|
const rules: FormRules = {} |
|
|
|
const initialValues: { [field: string]: any } = {} |
|
|
|
const initialValues: { [field: string]: any } = {} |
|
|
|
const elements = [] |
|
|
|
const elements: IFormItem[] = [] |
|
|
|
for (let item of json) { |
|
|
|
for (let item of json) { |
|
|
|
const { name, value, field, span, children, validate, ...rest } = item |
|
|
|
const { name, value, field, span = 24, children, validate, ...rest } = item |
|
|
|
if (value || value === 0) { |
|
|
|
if (value || value === 0) { |
|
|
|
fields[field] = value |
|
|
|
fields[field] = value |
|
|
|
initialValues[field] = value |
|
|
|
initialValues[field] = value |
|
|
|
} |
|
|
|
} |
|
|
|
if (validate) rules[field] = formatValidate(validate) |
|
|
|
if (validate) rules[field] = formatValidate(validate) |
|
|
|
const spanRef = span === void 0 ? 24 : toRef(item, 'span') |
|
|
|
const element: IFormItem = { |
|
|
|
elements.push({ |
|
|
|
|
|
|
|
showLabel: !!name, |
|
|
|
showLabel: !!name, |
|
|
|
...omit(rest, ['type', 'props', 'options']), |
|
|
|
...omit(rest, ['type', 'props', 'options']), |
|
|
|
label: name, |
|
|
|
label: name, |
|
|
|
path: !children ? field : '', |
|
|
|
path: !children ? field : '', |
|
|
|
widget: () => getField(item, fields, rules), |
|
|
|
widget: () => getField(item, fields, rules), |
|
|
|
span: spanRef |
|
|
|
span: toRef(item, 'span') as Ref<number> |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
elements.push(element) |
|
|
|
} |
|
|
|
} |
|
|
|
return { rules, elements, initialValues } |
|
|
|
return { rules, elements, initialValues } |
|
|
|
} |
|
|
|
} |
|
|
|