diff --git a/packages/core/client/src/block-provider/TableSelectorProvider.tsx b/packages/core/client/src/block-provider/TableSelectorProvider.tsx index e3b6f07ef1..1dd28e5f74 100644 --- a/packages/core/client/src/block-provider/TableSelectorProvider.tsx +++ b/packages/core/client/src/block-provider/TableSelectorProvider.tsx @@ -1,5 +1,6 @@ import { ArrayField } from '@formily/core'; import { Schema, useField, useFieldSchema } from '@formily/react'; +import uniq from 'lodash/uniq'; import React, { createContext, useContext, useEffect } from 'react'; import { useCollectionManager } from '../collection-manager'; import { RecordProvider, useRecord } from '../record-provider'; @@ -33,7 +34,7 @@ const InternalTableSelectorProvider = (props) => { ); }; -const useAssociationNames = (collection) => { +const useAssociationNames2 = (collection) => { const { getCollectionFields } = useCollectionManager(); const names = getCollectionFields(collection) ?.filter((field) => field.target) @@ -49,6 +50,53 @@ const recursiveParent = (schema: Schema, component) => { : null; }; +const useAssociationNames = (collection) => { + const { getCollectionFields } = useCollectionManager(); + const collectionFields = getCollectionFields(collection); + const associationFields = new Set(); + for (const collectionField of collectionFields) { + if (collectionField.target) { + associationFields.add(collectionField.name); + const fields = getCollectionFields(collectionField.target); + for (const field of fields) { + if (field.target) { + associationFields.add(`${collectionField.name}.${field.name}`); + } + } + } + } + const fieldSchema = useFieldSchema(); + const tableSchema = fieldSchema.reduceProperties((buf, schema) => { + if (schema['x-component'] === 'TableV2.Selector') { + return schema; + } + return buf; + }, new Schema({})); + return uniq( + tableSchema.reduceProperties((buf, schema) => { + if (schema['x-component'] === 'TableV2.Column') { + const s = schema.reduceProperties((buf, s) => { + const [name] = (s.name as string).split('.'); + if (s['x-collection-field'] && associationFields.has(name)) { + return s; + } + return buf; + }, null); + if (s) { + // 关联字段和关联的关联字段 + const [firstName] = s.name.split('.'); + if (associationFields.has(s.name)) { + buf.push(s.name); + } else if (associationFields.has(firstName)) { + buf.push(firstName); + } + } + } + return buf; + }, []), + ); +}; + export const TableSelectorProvider = (props) => { const fieldSchema = useFieldSchema(); const ctx = useFormBlockContext();