fix: avoid o2o, o2m can select the data already selected (#1462)

* fix: avoid o2o, o2m can select the data already selected

* fix: o2m
This commit is contained in:
Dunqing 2023-02-17 18:25:14 +08:00 committed by GitHub
parent 1beb25da70
commit 92185ec57c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 5 deletions

View File

@ -81,12 +81,12 @@ const InternalAssociationSelect = connect(
mapReadPretty(ReadPretty), mapReadPretty(ReadPretty),
); );
interface RemoteSelectInterface { interface AssociationSelectInterface {
(props: any): React.ReactElement; (props: any): React.ReactElement;
Designer: React.FC; Designer: React.FC;
} }
export const AssociationSelect = InternalAssociationSelect as unknown as RemoteSelectInterface; export const AssociationSelect = InternalAssociationSelect as unknown as AssociationSelectInterface;
AssociationSelect.Designer = () => { AssociationSelect.Designer = () => {
const { getCollectionFields, getInterface, getCollectionJoinField } = useCollectionManager(); const { getCollectionFields, getInterface, getCollectionJoinField } = useCollectionManager();

View File

@ -1,20 +1,51 @@
import { useFieldSchema } from '@formily/react'; import { useFieldSchema } from '@formily/react';
import { useMemo } from 'react'; import { useMemo } from 'react';
import { useCollection } from '../../../collection-manager'; import { useCollection, useCollectionManager } from '../../../collection-manager';
import { useRecord } from '../../../record-provider';
export default function useServiceOptions(props) { export default function useServiceOptions(props) {
const { action = 'list', service } = props; const { action = 'list', service, params, value } = props;
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const { getField } = useCollection(); const { getField } = useCollection();
const { getCollectionFields } = useCollectionManager();
const record = useRecord();
const collectionField = useMemo(() => { const collectionField = useMemo(() => {
return getField(fieldSchema.name); return getField(fieldSchema.name);
}, [fieldSchema.name]); }, [fieldSchema.name]);
const filter = useMemo(() => {
if (!collectionField) return params?.filter;
let extraFilter = {};
if (['oho', 'o2m'].includes(collectionField.interface)) {
const eqValue = record?.[collectionField.sourceKey];
extraFilter = {
$or: [
{
[collectionField.foreignKey]: {
$is: null,
},
},
eqValue !== undefined && eqValue !== null
? {
[collectionField.foreignKey]: {
$eq: eqValue,
},
}
: null,
].filter(Boolean),
};
}
return params?.filter ? { $and: [extraFilter, params?.filter] } : extraFilter;
}, [params?.filter, getCollectionFields, collectionField]);
return useMemo(() => { return useMemo(() => {
return { return {
resource: collectionField?.target, resource: collectionField?.target,
action, action,
...service, ...service,
params: { ...service?.params, filter },
}; };
}, [collectionField?.target, action, service]); }, [collectionField?.target, action, filter, service]);
} }