fix: dynamic appends not support template schema

This commit is contained in:
katherinehhh 2023-05-18 15:23:00 +08:00
parent aa69973a90
commit 2f1cc6c938
3 changed files with 35 additions and 13 deletions

View File

@ -40,8 +40,7 @@ const InternalDetailsBlockProvider = (props) => {
export const DetailsBlockProvider = (props) => {
const params = { ...props.params };
const { collection } = props;
const { appends } = useAssociationNames(collection);
const { appends } = useAssociationNames();
if (!Object.keys(params).includes('appends')) {
params['appends'] = appends;
}

View File

@ -72,7 +72,7 @@ export const FormBlockProvider = (props) => {
const currentCollection = useCollection();
const { designable } = useDesignable();
const isEmptyRecord = useIsEmptyRecord();
const { appends, updateAssociationValues } = useAssociationNames(collection);
const { appends, updateAssociationValues } = useAssociationNames();
if (!Object.keys(params).includes('appends')) {
params['appends'] = appends;
}

View File

@ -1073,10 +1073,20 @@ export const useAssociationFilterBlockProps = () => {
};
};
export const useAssociationNames = (collection) => {
const getTemplateSchema = ({ uid }) => {
const conf = {
url: `/uiSchemas:getJsonSchema/${uid}`,
};
const { data, loading } = useRequest(conf);
if (loading) {
// return;
}
return new Schema(data?.data || {});
};
export const useAssociationNames = () => {
const { getCollectionJoinField } = useCollectionManager();
const { getTemplateById } = useSchemaTemplateManager();
const fieldSchema = useFieldSchema();
const associationValues = [];
const formSchema = fieldSchema.reduceProperties((buf, schema) => {
@ -1086,6 +1096,13 @@ export const useAssociationNames = (collection) => {
return buf;
}, new Schema({}));
const templateSchema = formSchema.reduceProperties((buf, schema) => {
if (schema['x-component'] === 'BlockTemplate') {
return schema;
}
return buf;
}, null);
const getAssociationAppends = (schema, arr = []) => {
const data = schema.reduceProperties((buf, s) => {
const collectionfield = s['x-collection-field'] && getCollectionJoinField(s['x-collection-field']);
@ -1106,9 +1123,6 @@ export const useAssociationNames = (collection) => {
if (s['x-component'] === 'Grid.Row') {
const kk = buf?.concat?.();
return getNesterAppends(s, kk || []);
} else if (s['x-component'] === 'BlockTemplate') {
const templateSchema = getTemplateById(s['x-component-props']?.templateId);
return getAssociationAppends(templateSchema, buf);
} else {
return !s['x-component']?.includes('Action.') && s['x-component'] !== 'TableField'
? getAssociationAppends(s, buf)
@ -1138,7 +1152,7 @@ export const useAssociationNames = (collection) => {
for (let i = 0; i < nestedList.length; i++) {
flattenHelper(nestedList[i], nestedList[i][0]);
}
return uniq(flattenedList.filter((obj) => !obj.startsWith('.')));
return uniq(flattenedList.filter((obj) => !obj?.startsWith('.')));
}
const getNesterAppends = (gridSchema, data) => {
gridSchema.reduceProperties((buf, s) => {
@ -1147,8 +1161,17 @@ export const useAssociationNames = (collection) => {
}, data);
return data.filter((g) => g.length);
};
if (templateSchema) {
const template = getTemplateById(templateSchema['x-component-props']?.templateId);
const schema = getTemplateSchema(template);
if (schema) {
const associations = getAssociationAppends(schema);
const appends = flattenNestedList(associations);
return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) };
}
} else {
const associations = getAssociationAppends(formSchema);
const appends = flattenNestedList(associations);
return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) };
}
};