fix(association-field): allow multiple (#1938)

This commit is contained in:
chenos 2023-05-26 11:38:03 +08:00 committed by GitHub
parent 849956b8ea
commit efbd7c7020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 6 deletions

View File

@ -312,7 +312,7 @@ export default {
"Display association fields": "显示关联表的字段", "Display association fields": "显示关联表的字段",
"Display field title": "显示字段标题", "Display field title": "显示字段标题",
"Field component": "字段组件", "Field component": "字段组件",
"Allow multiple": "允许多选", "Allow multiple": "允许添加/关联多条",
"Quick upload": "快速上传", "Quick upload": "快速上传",
"Select file": "选择文件", "Select file": "选择文件",
"Subtable": "子表格", "Subtable": "子表格",

View File

@ -8,6 +8,7 @@ export const AssociationFieldProvider = observer((props) => {
const field = useField<Field>(); const field = useField<Field>();
const { getCollectionJoinField, getCollection } = useCollectionManager(); const { getCollectionJoinField, getCollection } = useCollectionManager();
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const allowMultiple = fieldSchema['x-component-props']?.multiple !== false;
const collectionField = useMemo( const collectionField = useMemo(
() => getCollectionJoinField(fieldSchema['x-collection-field']), () => getCollectionJoinField(fieldSchema['x-collection-field']),
@ -23,7 +24,7 @@ export const AssociationFieldProvider = observer((props) => {
); );
return collectionField ? ( return collectionField ? (
<AssociationFieldContext.Provider value={{ options: collectionField, field, currentMode }}> <AssociationFieldContext.Provider value={{ options: collectionField, field, allowMultiple, currentMode }}>
{props.children} {props.children}
</AssociationFieldContext.Provider> </AssociationFieldContext.Provider>
) : null; ) : null;

View File

@ -24,7 +24,7 @@ const ToOneNester = (props) => {
const ToManyNester = observer((props) => { const ToManyNester = observer((props) => {
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const { field } = useAssociationFieldContext<ArrayField>(); const { field, allowMultiple } = useAssociationFieldContext<ArrayField>();
const { t } = useTranslation(); const { t } = useTranslation();
return ( return (
<Card bordered={true} style={{ position: 'relative' }}> <Card bordered={true} style={{ position: 'relative' }}>
@ -48,7 +48,7 @@ const ToManyNester = observer((props) => {
</> </>
); );
})} })}
{field.editable && ( {field.editable && allowMultiple && (
<Button <Button
type={'dashed'} type={'dashed'}
block block

View File

@ -4,7 +4,8 @@ import { createContext } from 'react';
export interface AssociationFieldContextProps { export interface AssociationFieldContextProps {
options?: any; options?: any;
field?: GeneralField; field?: GeneralField;
currentMode?:string; currentMode?: string;
allowMultiple?: boolean;
} }
export const AssociationFieldContext = createContext<AssociationFieldContextProps>({}); export const AssociationFieldContext = createContext<AssociationFieldContextProps>({});

View File

@ -30,7 +30,12 @@ export const useInsertSchema = (component) => {
}; };
export function useAssociationFieldContext<F extends GeneralField>() { export function useAssociationFieldContext<F extends GeneralField>() {
return useContext(AssociationFieldContext) as { options: any; field: F; currentMode: string }; return useContext(AssociationFieldContext) as {
options: any;
field: F;
currentMode: string;
allowMultiple?: boolean;
};
} }
export default function useServiceOptions(props) { export default function useServiceOptions(props) {