Skip to content

Commit

Permalink
feat: [useComponents] add hasComponent method
Browse files Browse the repository at this point in the history
feat: [useComponents] add `hasComponent` method
  • Loading branch information
akai committed Mar 9, 2021
1 parent c37afe1 commit d933955
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/components/ComponentsProvider/ComponentsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { ComponentsContextInterface } from './interface';

export const ComponentsContext = React.createContext<ComponentsContextInterface>({
addComponent: () => {},
hasComponent: () => false,
getComponent: () => null,
});
6 changes: 5 additions & 1 deletion src/components/ComponentsProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const ComponentsProvider: React.FC<ComponentsProviderProps> = (props) =>
componentsRef.current[code] = val;
}

function hasComponent(code: string) {
return componentsRef.current.hasOwnProperty(code);
}

function getComponent(code: string, callback: GetComponentCallback = () => {}) {
const comp = componentsRef.current[code];

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

return (
<ComponentsContext.Provider value={{ addComponent, getComponent }}>
<ComponentsContext.Provider value={{ addComponent, hasComponent, getComponent }}>
{children}
</ComponentsContext.Provider>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/ComponentsProvider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface GetComponentCallback {

export interface ComponentsContextInterface {
addComponent: (code: string, value: ComponentInterface) => void;
hasComponent: (code: string) => boolean;
getComponent: (
code: string,
callback?: GetComponentCallback,
Expand Down

0 comments on commit d933955

Please sign in to comment.