mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 13:39:24 +08:00
fix: use association names
This commit is contained in:
parent
2f1cc6c938
commit
b7954a6f05
@ -1,5 +1,6 @@
|
|||||||
import { createForm } from '@formily/core';
|
import { createForm } from '@formily/core';
|
||||||
import { RecursionField, Schema, useField, useFieldSchema } from '@formily/react';
|
import { RecursionField, Schema, useField, useFieldSchema } from '@formily/react';
|
||||||
|
import { Spin } from 'antd';
|
||||||
import { isEmpty } from 'lodash';
|
import { isEmpty } from 'lodash';
|
||||||
import React, { createContext, useContext, useEffect, useMemo, useRef } from 'react';
|
import React, { createContext, useContext, useEffect, useMemo, useRef } from 'react';
|
||||||
import { useCollection } from '../collection-manager';
|
import { useCollection } from '../collection-manager';
|
||||||
@ -72,7 +73,10 @@ export const FormBlockProvider = (props) => {
|
|||||||
const currentCollection = useCollection();
|
const currentCollection = useCollection();
|
||||||
const { designable } = useDesignable();
|
const { designable } = useDesignable();
|
||||||
const isEmptyRecord = useIsEmptyRecord();
|
const isEmptyRecord = useIsEmptyRecord();
|
||||||
const { appends, updateAssociationValues } = useAssociationNames();
|
const { loading, appends, updateAssociationValues } = useAssociationNames();
|
||||||
|
if (loading) {
|
||||||
|
return <Spin />;
|
||||||
|
}
|
||||||
if (!Object.keys(params).includes('appends')) {
|
if (!Object.keys(params).includes('appends')) {
|
||||||
params['appends'] = appends;
|
params['appends'] = appends;
|
||||||
}
|
}
|
||||||
|
@ -16,12 +16,12 @@ import { transformToFilter } from '../../filter-provider/utils';
|
|||||||
import { useRecord } from '../../record-provider';
|
import { useRecord } from '../../record-provider';
|
||||||
import { removeNullCondition, useActionContext, useCompile } from '../../schema-component';
|
import { removeNullCondition, useActionContext, useCompile } from '../../schema-component';
|
||||||
import { BulkEditFormItemValueType } from '../../schema-initializer/components';
|
import { BulkEditFormItemValueType } from '../../schema-initializer/components';
|
||||||
|
import { useSchemaTemplateManager } from '../../schema-templates';
|
||||||
import { useCurrentUserContext } from '../../user';
|
import { useCurrentUserContext } from '../../user';
|
||||||
import { useBlockRequestContext, useFilterByTk } from '../BlockProvider';
|
import { useBlockRequestContext, useFilterByTk } from '../BlockProvider';
|
||||||
import { useDetailsBlockContext } from '../DetailsBlockProvider';
|
import { useDetailsBlockContext } from '../DetailsBlockProvider';
|
||||||
import { mergeFilter } from '../SharedFilterProvider';
|
import { mergeFilter } from '../SharedFilterProvider';
|
||||||
import { TableFieldResource } from '../TableFieldProvider';
|
import { TableFieldResource } from '../TableFieldProvider';
|
||||||
import { useSchemaTemplateManager } from '../../schema-templates';
|
|
||||||
|
|
||||||
export const usePickActionProps = () => {
|
export const usePickActionProps = () => {
|
||||||
const form = useForm();
|
const form = useForm();
|
||||||
@ -1073,105 +1073,108 @@ export const useAssociationFilterBlockProps = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const getTemplateSchema = ({ uid }) => {
|
const getTemplateSchema = async (api, { uid }) => {
|
||||||
const conf = {
|
const conf = {
|
||||||
url: `/uiSchemas:getJsonSchema/${uid}`,
|
url: `/uiSchemas:getJsonSchema/${uid}`,
|
||||||
};
|
};
|
||||||
const { data, loading } = useRequest(conf);
|
const { data } = await api.request(conf);
|
||||||
if (loading) {
|
console.log('data?.data', data?.data);
|
||||||
// return;
|
|
||||||
}
|
|
||||||
return new Schema(data?.data || {});
|
return new Schema(data?.data || {});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useAssociationNames = () => {
|
export const useAssociationNames = () => {
|
||||||
|
const api = useAPIClient();
|
||||||
const { getCollectionJoinField } = useCollectionManager();
|
const { getCollectionJoinField } = useCollectionManager();
|
||||||
const { getTemplateById } = useSchemaTemplateManager();
|
const { getTemplateById } = useSchemaTemplateManager();
|
||||||
const fieldSchema = useFieldSchema();
|
const fieldSchema = useFieldSchema();
|
||||||
const associationValues = [];
|
const { loading, data } = useRequest(async () => {
|
||||||
const formSchema = fieldSchema.reduceProperties((buf, schema) => {
|
const associationValues = [];
|
||||||
if (['FormV2', 'Details', 'List', 'GridCard'].includes(schema['x-component'])) {
|
const formSchema = fieldSchema.reduceProperties((buf, schema) => {
|
||||||
return schema;
|
if (['FormV2', 'Details', 'List', 'GridCard'].includes(schema['x-component'])) {
|
||||||
}
|
return schema;
|
||||||
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']);
|
|
||||||
if (
|
|
||||||
collectionfield &&
|
|
||||||
['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(collectionfield.type) &&
|
|
||||||
s['x-component'] !== 'TableField'
|
|
||||||
) {
|
|
||||||
buf.push(s.name);
|
|
||||||
if (['Nester', 'SubTable'].includes(s['x-component-props']?.mode)) {
|
|
||||||
associationValues.push(s.name);
|
|
||||||
}
|
|
||||||
if (s['x-component-props'].mode === 'Nester') {
|
|
||||||
return getAssociationAppends(s, buf);
|
|
||||||
}
|
|
||||||
return buf;
|
|
||||||
} else {
|
|
||||||
if (s['x-component'] === 'Grid.Row') {
|
|
||||||
const kk = buf?.concat?.();
|
|
||||||
return getNesterAppends(s, kk || []);
|
|
||||||
} else {
|
|
||||||
return !s['x-component']?.includes('Action.') && s['x-component'] !== 'TableField'
|
|
||||||
? getAssociationAppends(s, buf)
|
|
||||||
: buf;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, arr);
|
return buf;
|
||||||
return data || [];
|
}, new Schema({}));
|
||||||
};
|
|
||||||
|
|
||||||
function flattenNestedList(nestedList) {
|
const templateSchema = formSchema.reduceProperties((buf, schema) => {
|
||||||
const flattenedList = [];
|
if (schema['x-component'] === 'BlockTemplate') {
|
||||||
function flattenHelper(list, prefix) {
|
return schema;
|
||||||
for (let i = 0; i < list.length; i++) {
|
}
|
||||||
if (Array.isArray(list[i])) {
|
return buf;
|
||||||
`${prefix}` !== `${list[i][0]}` && flattenHelper(list[i], `${prefix}.${list[i][0]}`);
|
}, null);
|
||||||
|
|
||||||
|
const getAssociationAppends = (schema, arr = []) => {
|
||||||
|
const data = schema.reduceProperties((buf, s) => {
|
||||||
|
const collectionfield = s['x-collection-field'] && getCollectionJoinField(s['x-collection-field']);
|
||||||
|
if (
|
||||||
|
collectionfield &&
|
||||||
|
['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(collectionfield.type) &&
|
||||||
|
s['x-component'] !== 'TableField'
|
||||||
|
) {
|
||||||
|
buf.push(s.name);
|
||||||
|
if (['Nester', 'SubTable'].includes(s['x-component-props']?.mode)) {
|
||||||
|
associationValues.push(s.name);
|
||||||
|
}
|
||||||
|
if (s['x-component-props'].mode === 'Nester') {
|
||||||
|
return getAssociationAppends(s, buf);
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
} else {
|
} else {
|
||||||
const str = prefix.replaceAll(`.${list[i]}`, '').trim();
|
if (s['x-component'] === 'Grid.Row') {
|
||||||
if (!str) {
|
const kk = buf?.concat?.();
|
||||||
!list.includes(str) && flattenedList.push(`${list[i]}`);
|
return getNesterAppends(s, kk || []);
|
||||||
} else {
|
} else {
|
||||||
!list.includes(str) ? flattenedList.push(`${str}.${list[i]}`) : flattenedList.push(str);
|
return !s['x-component']?.includes('Action.') && s['x-component'] !== 'TableField'
|
||||||
|
? getAssociationAppends(s, buf)
|
||||||
|
: buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, arr);
|
||||||
|
return data || [];
|
||||||
|
};
|
||||||
|
|
||||||
|
function flattenNestedList(nestedList) {
|
||||||
|
const flattenedList = [];
|
||||||
|
function flattenHelper(list, prefix) {
|
||||||
|
for (let i = 0; i < list.length; i++) {
|
||||||
|
if (Array.isArray(list[i])) {
|
||||||
|
`${prefix}` !== `${list[i][0]}` && flattenHelper(list[i], `${prefix}.${list[i][0]}`);
|
||||||
|
} else {
|
||||||
|
const str = prefix.replaceAll(`.${list[i]}`, '').trim();
|
||||||
|
if (!str) {
|
||||||
|
!list.includes(str) && flattenedList.push(`${list[i]}`);
|
||||||
|
} else {
|
||||||
|
!list.includes(str) ? flattenedList.push(`${str}.${list[i]}`) : flattenedList.push(str);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (let i = 0; i < nestedList.length; i++) {
|
||||||
|
flattenHelper(nestedList[i], nestedList[i][0]);
|
||||||
|
}
|
||||||
|
return uniq(flattenedList.filter((obj) => !obj?.startsWith('.')));
|
||||||
}
|
}
|
||||||
for (let i = 0; i < nestedList.length; i++) {
|
const getNesterAppends = (gridSchema, data) => {
|
||||||
flattenHelper(nestedList[i], nestedList[i][0]);
|
gridSchema.reduceProperties((buf, s) => {
|
||||||
}
|
buf.push(getAssociationAppends(s));
|
||||||
return uniq(flattenedList.filter((obj) => !obj?.startsWith('.')));
|
return buf;
|
||||||
}
|
}, data);
|
||||||
const getNesterAppends = (gridSchema, data) => {
|
return data.filter((g) => g.length);
|
||||||
gridSchema.reduceProperties((buf, s) => {
|
};
|
||||||
buf.push(getAssociationAppends(s));
|
if (templateSchema) {
|
||||||
return buf;
|
const template = getTemplateById(templateSchema['x-component-props']?.templateId);
|
||||||
}, data);
|
const schema = await getTemplateSchema(api, template);
|
||||||
return data.filter((g) => g.length);
|
if (schema) {
|
||||||
};
|
const associations = getAssociationAppends(schema);
|
||||||
if (templateSchema) {
|
const appends = flattenNestedList(associations);
|
||||||
const template = getTemplateById(templateSchema['x-component-props']?.templateId);
|
return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) };
|
||||||
const schema = getTemplateSchema(template);
|
}
|
||||||
if (schema) {
|
} else {
|
||||||
const associations = getAssociationAppends(schema);
|
const associations = getAssociationAppends(formSchema);
|
||||||
const appends = flattenNestedList(associations);
|
const appends = flattenNestedList(associations);
|
||||||
return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) };
|
return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) };
|
||||||
}
|
}
|
||||||
} else {
|
});
|
||||||
const associations = getAssociationAppends(formSchema);
|
|
||||||
const appends = flattenNestedList(associations);
|
return { appends: [], updateAssociationValues: [], loading, ...data };
|
||||||
return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) };
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user