fix(client): load data on demand
This commit is contained in:
		
							parent
							
								
									cbfd057c6d
								
							
						
					
					
						commit
						d5ae819b81
					
				@ -1,5 +1,6 @@
 | 
				
			|||||||
import { ArrayField } from '@formily/core';
 | 
					import { ArrayField } from '@formily/core';
 | 
				
			||||||
import { Schema, useField, useFieldSchema } from '@formily/react';
 | 
					import { Schema, useField, useFieldSchema } from '@formily/react';
 | 
				
			||||||
 | 
					import uniq from 'lodash/uniq';
 | 
				
			||||||
import React, { createContext, useContext, useEffect } from 'react';
 | 
					import React, { createContext, useContext, useEffect } from 'react';
 | 
				
			||||||
import { useCollectionManager } from '../collection-manager';
 | 
					import { useCollectionManager } from '../collection-manager';
 | 
				
			||||||
import { RecordProvider, useRecord } from '../record-provider';
 | 
					import { RecordProvider, useRecord } from '../record-provider';
 | 
				
			||||||
@ -33,7 +34,7 @@ const InternalTableSelectorProvider = (props) => {
 | 
				
			|||||||
  );
 | 
					  );
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const useAssociationNames = (collection) => {
 | 
					const useAssociationNames2 = (collection) => {
 | 
				
			||||||
  const { getCollectionFields } = useCollectionManager();
 | 
					  const { getCollectionFields } = useCollectionManager();
 | 
				
			||||||
  const names = getCollectionFields(collection)
 | 
					  const names = getCollectionFields(collection)
 | 
				
			||||||
    ?.filter((field) => field.target)
 | 
					    ?.filter((field) => field.target)
 | 
				
			||||||
@ -49,6 +50,53 @@ const recursiveParent = (schema: Schema, component) => {
 | 
				
			|||||||
    : null;
 | 
					    : 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) => {
 | 
					export const TableSelectorProvider = (props) => {
 | 
				
			||||||
  const fieldSchema = useFieldSchema();
 | 
					  const fieldSchema = useFieldSchema();
 | 
				
			||||||
  const ctx = useFormBlockContext();
 | 
					  const ctx = useFormBlockContext();
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user