fix: relational data should be loaded correctly on first render (#3016)

* fix: subform should lazy load data

* fix: relational data should be loaded correctly on first render
This commit is contained in:
被雨水过滤的空气-Rain 2023-11-10 15:27:53 +08:00 committed by GitHub
parent 3b5b732a1a
commit d9e7ba6e8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 9 deletions

View File

@ -301,13 +301,17 @@ export const BlockProvider = (props: {
}) => {
const { collection, association, name } = props;
const resource = useResource(props);
const params = useMemo(() => ({ ...props.params }), [props.params]);
const { appends, updateAssociationValues } = useAssociationNames();
const blockValue = useMemo(() => ({ name }), [name]);
if (!Object.keys(params).includes('appends')) {
params['appends'] = appends;
const params = useMemo(() => {
if (!props.params) {
return props.params;
}
if (!props.params['appends']) {
return { ...props.params, appends };
}
return { ...props.params };
}, [appends, props.params]);
const blockValue = useMemo(() => ({ name }), [name]);
return (
<BlockContext.Provider value={blockValue}>

View File

@ -1278,10 +1278,10 @@ export const useAssociationNames = () => {
const getAssociationAppends = (schema, str) => {
schema.reduceProperties((pre, s) => {
const prefix = pre || str;
const collectionfield = s['x-collection-field'] && getCollectionJoinField(s['x-collection-field']);
const collectionField = s['x-collection-field'] && getCollectionJoinField(s['x-collection-field']);
const isAssociationSubfield = s.name.includes('.');
const isAssociationField =
collectionfield && ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(collectionfield.type);
collectionField && ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(collectionField.type);
// 根据联动规则中条件的字段获取一些 appends
if (s['x-linkage-rules']) {
@ -1301,8 +1301,8 @@ export const useAssociationNames = () => {
});
}
const isTreeCollection = isAssociationField && getCollection(collectionfield.target)?.template === 'tree';
if (collectionfield && (isAssociationField || isAssociationSubfield) && s['x-component'] !== 'TableField') {
const isTreeCollection = isAssociationField && getCollection(collectionField.target)?.template === 'tree';
if (collectionField && (isAssociationField || isAssociationSubfield) && s['x-component'] !== 'TableField') {
const fieldPath = !isAssociationField && isAssociationSubfield ? getAssociationPath(s.name) : s.name;
const path = prefix === '' || !prefix ? fieldPath : prefix + '.' + fieldPath;
if (isTreeCollection) {

View File

@ -287,5 +287,11 @@ export default VariablesProvider;
* @returns
*/
function shouldToRequest(value) {
// fix https://nocobase.height.app/T-2502
// 兼容对多子表单子表格字段的情况
if (JSON.stringify(value) === '[{}]') {
return true;
}
return value === undefined;
}