feat: add ParentCollectionContext

This commit is contained in:
chenos 2024-02-08 10:43:21 +08:00
parent e9e6d4a817
commit c8ebcbcbb3

View File

@ -1,8 +1,9 @@
import React, { FC, ReactNode } from 'react'; import React, { FC, ReactNode, createContext, useContext } from 'react';
import { useCollectionManagerV2 } from './CollectionManagerProvider';
import { CollectionDeletedPlaceholder } from '../components/CollectionDeletedPlaceholder';
import { CollectionProviderV2 } from './CollectionProvider';
import { CollectionFieldProviderV2 } from '../collection-field'; import { CollectionFieldProviderV2 } from '../collection-field';
import { CollectionDeletedPlaceholder } from '../components/CollectionDeletedPlaceholder';
import { CollectionV2 } from './Collection';
import { useCollectionManagerV2 } from './CollectionManagerProvider';
import { CollectionProviderV2, useCollectionV2 } from './CollectionProvider';
export interface AssociationProviderPropsV2 { export interface AssociationProviderPropsV2 {
dataSource?: string; dataSource?: string;
@ -10,6 +11,17 @@ export interface AssociationProviderPropsV2 {
children?: ReactNode; children?: ReactNode;
} }
const ParentCollectionContext = createContext<CollectionV2>(null);
const ParentCollectionProvider = (props) => {
const collection = useCollectionV2();
return <ParentCollectionContext.Provider value={collection}>{props.children}</ParentCollectionContext.Provider>;
};
export const useParentCollection = () => {
return useContext(ParentCollectionContext);
};
export const AssociationProviderV2: FC<AssociationProviderPropsV2> = (props) => { export const AssociationProviderV2: FC<AssociationProviderPropsV2> = (props) => {
const { name, children } = props; const { name, children } = props;
@ -20,9 +32,11 @@ export const AssociationProviderV2: FC<AssociationProviderPropsV2> = (props) =>
return ( return (
<CollectionProviderV2 name={String(name).split('.')[0]}> <CollectionProviderV2 name={String(name).split('.')[0]}>
<CollectionFieldProviderV2 name={name}> <ParentCollectionProvider>
<CollectionProviderV2 name={collectionName}>{children}</CollectionProviderV2> <CollectionFieldProviderV2 name={name}>
</CollectionFieldProviderV2> <CollectionProviderV2 name={collectionName}>{children}</CollectionProviderV2>
</CollectionFieldProviderV2>
</ParentCollectionProvider>
</CollectionProviderV2> </CollectionProviderV2>
); );
}; };