fix: table settings get collectionField (#3837)

* fix: table settings get collectionField

* fix: remove duplicate useCollectionField

* fix: bug

* refactor: tagComponentFieldSettings

---------

Co-authored-by: katherinehhh <katherine_15995@163.com>
This commit is contained in:
jack zhang 2024-03-29 09:12:52 +08:00 committed by GitHub
parent 1f59d4bed2
commit a38e34cf9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 18 additions and 42 deletions

View File

@ -2,19 +2,7 @@ import { Field } from '@formily/core';
import { useField, useFieldSchema } from '@formily/react'; import { useField, useFieldSchema } from '@formily/react';
import { useIsFileField } from '../schema-component/antd/form-item/FormItem.Settings'; import { useIsFileField } from '../schema-component/antd/form-item/FormItem.Settings';
import { useColumnSchema } from '../schema-component/antd/table-v2/Table.Column.Decorator'; import { useColumnSchema } from '../schema-component/antd/table-v2/Table.Column.Decorator';
import { useCollectionManager_deprecated, useCollection_deprecated } from '../collection-manager'; import { useCollectionField } from '../data-source';
/**
*
* @returns
*/
function useCollectionField() {
const { getCollectionJoinField } = useCollectionManager_deprecated();
const { getField } = useCollection_deprecated();
const fieldSchema = useFieldSchema();
const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
return collectionField;
}
export function useFieldComponentName(): string { export function useFieldComponentName(): string {
const { fieldSchema: tableColumnSchema, collectionField: tableColumnField } = useColumnSchema(); const { fieldSchema: tableColumnSchema, collectionField: tableColumnField } = useColumnSchema();

View File

@ -22,7 +22,7 @@ export const CollectionFieldProvider: FC<CollectionFieldProviderProps> = (props)
const collectionManager = useCollectionManager(); const collectionManager = useCollectionManager();
const value = useMemo(() => { const value = useMemo(() => {
if (!collection || allowNull) return null; if (!collection) return null;
const field = fieldSchema?.['x-component-props']?.['field']; const field = fieldSchema?.['x-component-props']?.['field'];
return ( return (
collectionManager.getCollectionField(fieldSchema?.['x-collection-field']) || collectionManager.getCollectionField(fieldSchema?.['x-collection-field']) ||
@ -31,7 +31,7 @@ export const CollectionFieldProvider: FC<CollectionFieldProviderProps> = (props)
); );
}, [collection, fieldSchema, name, collectionManager]); }, [collection, fieldSchema, name, collectionManager]);
if (!collection || allowNull) { if (!value && allowNull) {
return <>{children}</>; return <>{children}</>;
} }

View File

@ -6,7 +6,7 @@ import { useFieldComponentName } from '../../../../common/useFieldComponentName'
import { useDesignable, useFieldModeOptions, useIsAddNewForm } from '../../../../schema-component'; import { useDesignable, useFieldModeOptions, useIsAddNewForm } from '../../../../schema-component';
import { isSubMode } from '../../../../schema-component/antd/association-field/util'; import { isSubMode } from '../../../../schema-component/antd/association-field/util';
import { useTitleFieldOptions } from '../../../../schema-component/antd/form-item/FormItem.Settings'; import { useTitleFieldOptions } from '../../../../schema-component/antd/form-item/FormItem.Settings';
import { useCollectionField } from '../utils'; import { useCollectionField } from '../../../../data-source';
const fieldComponent: any = { const fieldComponent: any = {
name: 'fieldComponent', name: 'fieldComponent',

View File

@ -9,7 +9,7 @@ import {
useIsFieldReadPretty, useIsFieldReadPretty,
useIsFormReadPretty, useIsFormReadPretty,
} from '../../../../schema-component/antd/form-item/FormItem.Settings'; } from '../../../../schema-component/antd/form-item/FormItem.Settings';
import { useCollectionField } from '../utils'; import { useCollectionField } from '../../../../data-source';
import { useFormBlockType } from '../../../../block-provider'; import { useFormBlockType } from '../../../../block-provider';
const allowMultiple: any = { const allowMultiple: any = {

View File

@ -10,7 +10,7 @@ import {
useTitleFieldOptions, useTitleFieldOptions,
} from '../../../../schema-component/antd/form-item/FormItem.Settings'; } from '../../../../schema-component/antd/form-item/FormItem.Settings';
import { useColumnSchema } from '../../../../schema-component/antd/table-v2/Table.Column.Decorator'; import { useColumnSchema } from '../../../../schema-component/antd/table-v2/Table.Column.Decorator';
import { useCollectionField } from '../utils'; import { useCollectionField } from '../../../../data-source';
export const titleField: any = { export const titleField: any = {
name: 'titleField', name: 'titleField',

View File

@ -6,7 +6,7 @@ import { useFieldComponentName } from '../../../../common/useFieldComponentName'
import { useDesignable, useFieldModeOptions, useIsAddNewForm } from '../../../../schema-component'; import { useDesignable, useFieldModeOptions, useIsAddNewForm } from '../../../../schema-component';
import { isSubMode } from '../../../../schema-component/antd/association-field/util'; import { isSubMode } from '../../../../schema-component/antd/association-field/util';
import { useIsFieldReadPretty } from '../../../../schema-component/antd/form-item/FormItem.Settings'; import { useIsFieldReadPretty } from '../../../../schema-component/antd/form-item/FormItem.Settings';
import { useCollectionField } from '../utils'; import { useCollectionField } from '../../../../data-source';
import { useColumnSchema } from '../../../../schema-component/antd/table-v2/Table.Column.Decorator'; import { useColumnSchema } from '../../../../schema-component/antd/table-v2/Table.Column.Decorator';
import { titleField } from '../Picker/recordPickerComponentFieldSettings'; import { titleField } from '../Picker/recordPickerComponentFieldSettings';

View File

@ -23,7 +23,7 @@ import { SchemaSettingsDataScope } from '../../../../schema-settings/SchemaSetti
import { SchemaSettingsSortingRule } from '../../../../schema-settings/SchemaSettingsSortingRule'; import { SchemaSettingsSortingRule } from '../../../../schema-settings/SchemaSettingsSortingRule';
import { useIsShowMultipleSwitch } from '../../../../schema-settings/hooks/useIsShowMultipleSwitch'; import { useIsShowMultipleSwitch } from '../../../../schema-settings/hooks/useIsShowMultipleSwitch';
import { useLocalVariables, useVariables } from '../../../../variables'; import { useLocalVariables, useVariables } from '../../../../variables';
import { useCollectionField } from '../utils'; import { useCollectionField } from '../../../../data-source';
const enableLink = { const enableLink = {
name: 'enableLink', name: 'enableLink',

View File

@ -8,7 +8,7 @@ import { isSubMode } from '../../../../schema-component/antd/association-field/u
import { useTitleFieldOptions } from '../../../../schema-component/antd/form-item/FormItem.Settings'; import { useTitleFieldOptions } from '../../../../schema-component/antd/form-item/FormItem.Settings';
import { useColorFields } from '../../../../schema-component/antd/table-v2/Table.Column.Designer'; import { useColorFields } from '../../../../schema-component/antd/table-v2/Table.Column.Designer';
import { useColumnSchema } from '../../../../schema-component/antd/table-v2/Table.Column.Decorator'; import { useColumnSchema } from '../../../../schema-component/antd/table-v2/Table.Column.Decorator';
import { useCollectionField } from '../utils'; import { useCollectionField } from '../../../../data-source';
const enableLink = { const enableLink = {
name: 'enableLink', name: 'enableLink',
@ -140,9 +140,12 @@ export const tagComponentFieldSettings = new SchemaSettings({
useComponentProps() { useComponentProps() {
const { t } = useTranslation(); const { t } = useTranslation();
const field = useField<Field>(); const field = useField<Field>();
const fieldSchema = useFieldSchema(); const schema = useFieldSchema();
const targetCollectionField = useCollectionField();
const { fieldSchema: tableColumnSchema, collectionField: tableColumnField } = useColumnSchema();
const fieldSchema = tableColumnSchema || schema;
const collectionField = tableColumnField || targetCollectionField;
const { dn } = useDesignable(); const { dn } = useDesignable();
const collectionField = useCollectionField();
const colorFieldOptions = useColorFields(collectionField?.target ?? collectionField?.targetCollection); const colorFieldOptions = useColorFields(collectionField?.target ?? collectionField?.targetCollection);
return { return {
title: t('Tag color field'), title: t('Tag color field'),

View File

@ -1,14 +0,0 @@
import { useFieldSchema } from '@formily/react';
import { useCollection_deprecated, useCollectionManager_deprecated } from '../../../collection-manager';
/**
*
* @returns
*/
export function useCollectionField() {
const { getCollectionJoinField } = useCollectionManager_deprecated();
const { getField } = useCollection_deprecated();
const fieldSchema = useFieldSchema();
const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
return collectionField;
}

View File

@ -67,16 +67,15 @@ export const FormItem: any = observer(
}, },
); );
}, [showTitle]); }, [showTitle]);
const fieldSchema = useFieldSchema();
return ( return (
<BlockItem className={'nb-form-item'}> <CollectionFieldProvider allowNull={true}>
<CollectionFieldProvider name={fieldSchema.name} allowNull={!fieldSchema['x-collection-field']}> <BlockItem className={'nb-form-item'}>
<ACLCollectionFieldProvider> <ACLCollectionFieldProvider>
<Item className={className} {...props} extra={extra} /> <Item className={className} {...props} extra={extra} />
</ACLCollectionFieldProvider> </ACLCollectionFieldProvider>
</CollectionFieldProvider> </BlockItem>
</BlockItem> </CollectionFieldProvider>
); );
}, },
{ displayName: 'FormItem' }, { displayName: 'FormItem' },