fix: hide collections & fields if not exist

This commit is contained in:
chenos 2022-04-21 18:08:18 +08:00
parent f1d7d14145
commit f2a3cef3cf
2 changed files with 12 additions and 5 deletions

View File

@ -1,15 +1,18 @@
import { SchemaKey } from '@formily/react';
import React from 'react'; import React from 'react';
import { merge } from '@formily/shared'; import { CollectionFieldContext } from './context';
import { useCollection } from './hooks'; import { useCollection } from './hooks';
import { CollectionFieldOptions } from './types'; import { CollectionFieldOptions } from './types';
import { CollectionFieldContext } from './context';
import { SchemaKey } from '@formily/react';
export const CollectionFieldProvider: React.FC<{ name?: SchemaKey; field?: CollectionFieldOptions }> = (props) => { export const CollectionFieldProvider: React.FC<{ name?: SchemaKey; field?: CollectionFieldOptions }> = (props) => {
const { name, field, children } = props; const { name, field, children } = props;
const { getField } = useCollection(); const { getField } = useCollection();
const value = field || getField(field?.name || name);
if (!value) {
return null;
}
return ( return (
<CollectionFieldContext.Provider value={field || getField(field?.name || name)}> <CollectionFieldContext.Provider value={value}>
{children} {children}
</CollectionFieldContext.Provider> </CollectionFieldContext.Provider>
); );

View File

@ -6,5 +6,9 @@ import { CollectionOptions } from './types';
export const CollectionProvider: React.FC<{ name?: string; collection?: CollectionOptions }> = (props) => { export const CollectionProvider: React.FC<{ name?: string; collection?: CollectionOptions }> = (props) => {
const { name, collection, children } = props; const { name, collection, children } = props;
const { getCollection } = useCollectionManager(); const { getCollection } = useCollectionManager();
return <CollectionContext.Provider value={getCollection(collection || name)}>{children}</CollectionContext.Provider>; const value = getCollection(collection || name);
if (!value) {
return null;
}
return <CollectionContext.Provider value={value}>{children}</CollectionContext.Provider>;
}; };