Skip to content

Commit

Permalink
feat(fr): 数组子项默认值设置 close alibaba#679
Browse files Browse the repository at this point in the history
  • Loading branch information
F-loat committed Feb 24, 2022
1 parent e803b19 commit 3a79e76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/form-render/advanced/listDisplay.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const schema = {
type: 'string',
enum: ['a', 'b', 'c'],
enumNames: ['', '', ''],
default: 'a'
},
obj: {
title: '对象',
Expand All @@ -46,6 +47,7 @@ const schema = {
title: '简单输入框',
type: 'string',
required: true,
default: '卡片列表'
},
select1: {
title: '单选',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React from 'react';
import { get } from 'lodash-es';
import { useStore, useTools } from '../../../hooks';
import { getDataPath } from '../../../utils';
import { getDataPath, getSchemaFromFlatten, generateDataSkeleton } from '../../../utils';
import './list.less';
import SimpleList from './SimpleList';
import CardList from './CardList';
Expand Down Expand Up @@ -43,7 +43,9 @@ const RenderList = ({
};

const addItem = () => {
const newList = [...displayList, {}];
const _schema = getSchemaFromFlatten(flatten, parentId);
const [newItem] = generateDataSkeleton(_schema) || [{}];
const newList = [...displayList, newItem];
const newIndex = newList.length - 1;
onItemChange(dataPath, newList);
return newIndex;
Expand Down
21 changes: 12 additions & 9 deletions packages/form-render/src/form-render-core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,19 +736,22 @@ export const generateDataSkeleton = (schema, formData) => {
const childSchema = schema.properties[key];
const childData = _formData[key];
const childResult = generateDataSkeleton(childSchema, childData);
result[key] = childResult;
if (childResult !== undefined) {
result[key] = childResult;
}
});
} else if (_formData !== undefined) {
// result = _formData;
} else if (schema.default !== undefined) {
result = clone(schema.default);
} else if (isListType(schema)) {
const childData = generateDataSkeleton(schema.items);
result = childData && [childData];
} else if (schema.type === 'boolean' && !schema.widget) {
// result = false;
result = undefined;
} else {
if (schema.default !== undefined) {
result = clone(schema.default);
} else if (schema.type === 'boolean' && !schema.widget) {
// result = false;
result = undefined;
} else {
result = undefined;
}
result = undefined;
}
return result;
};
Expand Down

0 comments on commit 3a79e76

Please sign in to comment.