refactor: view collection support add m2o association field (#2422)

* refactor: view collection support add m2o association field

* refactor: code improve
This commit is contained in:
katherinehhh 2023-09-22 11:09:53 +08:00 committed by GitHub
parent 9f147085a5
commit 49b75b6ab6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 55 deletions

View File

@ -274,7 +274,6 @@ export const RenderChildrenWithAssociationFilter: React.FC<any> = (props) => {
}; };
export const BlockProvider = (props) => { export const BlockProvider = (props) => {
console.log(props);
const { collection, association } = props; const { collection, association } = props;
const resource = useResource(props); const resource = useResource(props);
const params = { ...props.params }; const params = { ...props.params };

View File

@ -240,28 +240,47 @@ export const AddFieldAction = (props) => {
return optionArr; return optionArr;
}, [getTemplate, record]); }, [getTemplate, record]);
const items = useMemo<MenuProps['items']>(() => { const items = useMemo<MenuProps['items']>(() => {
return getFieldOptions().map((option) => { return getFieldOptions()
if (option.children.length === 0) { .map((option) => {
return null; if (option.children.length === 0) {
} return null;
}
return { if (record.template === 'view') {
type: 'group', return {
label: compile(option.label), type: 'group',
title: compile(option.label), label: compile(option.label),
key: option.label, title: compile(option.label),
children: option.children key: option.label,
.filter((child) => !['o2o', 'subTable', 'linkTo'].includes(child.name)) children: option.children
.map((child) => { .filter((child) => ['m2o'].includes(child.name))
return { .map((child) => {
label: compile(child.title), return {
title: compile(child.title), label: compile(child.title),
key: child.name, title: compile(child.title),
dataTargetScope: child.targetScope, key: child.name,
}; dataTargetScope: child.targetScope,
}), };
}; }),
}); };
}
return {
type: 'group',
label: compile(option.label),
title: compile(option.label),
key: option.label,
children: option.children
.filter((child) => !['o2o', 'subTable', 'linkTo'].includes(child.name))
.map((child) => {
return {
label: compile(child.title),
title: compile(child.title),
key: child.name,
dataTargetScope: child.targetScope,
};
}),
};
})
.filter((v) => v.children.length);
}, [getFieldOptions]); }, [getFieldOptions]);
const menu = useMemo<MenuProps>(() => { const menu = useMemo<MenuProps>(() => {
return { return {
@ -283,37 +302,35 @@ export const AddFieldAction = (props) => {
}; };
}, [getInterface, items, record]); }, [getInterface, items, record]);
return ( return (
record.template !== 'view' && ( <RecordProvider record={record}>
<RecordProvider record={record}> <ActionContextProvider value={{ visible, setVisible }}>
<ActionContextProvider value={{ visible, setVisible }}> <Dropdown getPopupContainer={getContainer} trigger={trigger} align={align} menu={menu}>
<Dropdown getPopupContainer={getContainer} trigger={trigger} align={align} menu={menu}> {children || (
{children || ( <Button icon={<PlusOutlined />} type={'primary'}>
<Button icon={<PlusOutlined />} type={'primary'}> {t('Add field')}
{t('Add field')} </Button>
</Button> )}
)} </Dropdown>
</Dropdown> <SchemaComponent
<SchemaComponent schema={schema}
schema={schema} components={{ ...components, ArrayTable }}
components={{ ...components, ArrayTable }} scope={{
scope={{ getContainer,
getContainer, useCancelAction,
useCancelAction, createOnly: true,
createOnly: true, isOverride: false,
isOverride: false, override: false,
override: false, useCreateCollectionField,
useCreateCollectionField, record,
record, showReverseFieldConfig: true,
showReverseFieldConfig: true, targetScope,
targetScope, collections: currentCollections,
collections: currentCollections, isDialect,
isDialect, disabledJSONB: false,
disabledJSONB: false, ...scope,
...scope, }}
}} />
/> </ActionContextProvider>
</ActionContextProvider> </RecordProvider>
</RecordProvider>
)
); );
}; };