Merge "Security provider for UX-Client-Login"
[ccsdk/features.git] / sdnr / wt / odlux / framework / src / utilities / withComponents.ts
1 import * as React from 'react';
2 import applicationService from '../services/applicationManager';
3 export type WithComponents<T extends { [name: string]: string }> = {
4   components: { [prop in keyof T]: React.ComponentType }
5 };
6
7 export function withComponents<TProps,TMap extends { [name: string]: string }>(mapping: TMap) {
8   return (component: React.ComponentType<TProps & WithComponents<TMap>>): React.ComponentType<TProps> => {
9     const components = {} as any;
10     Object.keys(mapping).forEach(name => {
11       const [appKey, componentKey] = mapping[name].split('.');
12       const reg = applicationService.applications[appKey];
13       components[name] = reg && reg.exportedComponents && reg.exportedComponents[componentKey] || (() => null);
14     });
15     return (props: TProps) => (
16       React.createElement(component, Object.assign({ components }, props))
17     );
18   }
19 }
20 export default withComponents;