Skip to content

Commit d933955

Browse files
committed
feat: [useComponents] add hasComponent method
feat: [useComponents] add `hasComponent` method
1 parent c37afe1 commit d933955

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

src/components/ComponentsProvider/ComponentsContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { ComponentsContextInterface } from './interface';
33

44
export const ComponentsContext = React.createContext<ComponentsContextInterface>({
55
addComponent: () => {},
6+
hasComponent: () => false,
67
getComponent: () => null,
78
});

src/components/ComponentsProvider/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export const ComponentsProvider: React.FC<ComponentsProviderProps> = (props) =>
2020
componentsRef.current[code] = val;
2121
}
2222

23+
function hasComponent(code: string) {
24+
return componentsRef.current.hasOwnProperty(code);
25+
}
26+
2327
function getComponent(code: string, callback: GetComponentCallback = () => {}) {
2428
const comp = componentsRef.current[code];
2529

@@ -71,7 +75,7 @@ export const ComponentsProvider: React.FC<ComponentsProviderProps> = (props) =>
7175
}
7276

7377
return (
74-
<ComponentsContext.Provider value={{ addComponent, getComponent }}>
78+
<ComponentsContext.Provider value={{ addComponent, hasComponent, getComponent }}>
7579
{children}
7680
</ComponentsContext.Provider>
7781
);

src/components/ComponentsProvider/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface GetComponentCallback {
3838

3939
export interface ComponentsContextInterface {
4040
addComponent: (code: string, value: ComponentInterface) => void;
41+
hasComponent: (code: string) => boolean;
4142
getComponent: (
4243
code: string,
4344
callback?: GetComponentCallback,

0 commit comments

Comments
 (0)