Skip to content

Commit

Permalink
feat(di): add hooks for registries and registryComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Mar 20, 2019
1 parent 60a8984 commit c512dc2
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 164 deletions.
25 changes: 21 additions & 4 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import React, { StatelessComponent, ComponentType, createContext } from 'react';
import React, {
ReactNode,
FunctionComponent,
StatelessComponent,
ComponentType,
createContext,
useContext,
} from 'react';

export type GetNonDefaultProps<T> = keyof T extends never ? never : T;
export type RegistryContext = Record<string, Registry>;
Expand Down Expand Up @@ -40,12 +47,12 @@ export function withRegistry(...registries: Registry[]) {
};
}

export interface IComponentRegistryConsumer {
export interface IComponentRegistryConsumerProps {
id: string;
children: (registry: any) => React.ReactNode;
children: (registry: any) => ReactNode;
}

export const ComponentRegistryConsumer: React.SFC<IComponentRegistryConsumer> = props => (
export const ComponentRegistryConsumer: FunctionComponent<IComponentRegistryConsumerProps> = props => (
<RegistryConsumer>
{registries => {
if (__DEV__) {
Expand All @@ -59,6 +66,16 @@ export const ComponentRegistryConsumer: React.SFC<IComponentRegistryConsumer> =
</RegistryConsumer>
);

export const useRegistries = () => {
return useContext(registryContext);
};

export const useComponentRegistry = <T extends {}>(id: string) => {
const registries = useRegistries();

return registries[id].snapshot<T>();
};

export interface IRegistryOptions {
id: string;
inverted?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/di/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"test": "../../node_modules/.bin/nyc npm run unit test/*.test.tsx"
},
"peerDependencies": {
"react": "^16.0.0"
"react": "^16.8.0"
},
"nyc": {
"include": [
Expand Down
Loading

0 comments on commit c512dc2

Please sign in to comment.