2021-11-03 10:41:59 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import { IComponentOptions, IController } from 'angular';
|
2022-09-20 18:14:24 +00:00
|
|
|
import { StrictMode } from 'react';
|
2022-05-24 05:35:20 +00:00
|
|
|
import _ from 'lodash';
|
2021-11-03 10:41:59 +00:00
|
|
|
|
|
|
|
function toProps(
|
|
|
|
propNames: string[],
|
|
|
|
controller: IController,
|
|
|
|
$q: ng.IQService
|
|
|
|
) {
|
|
|
|
return Object.fromEntries(
|
|
|
|
propNames.map((key) => {
|
|
|
|
const prop = controller[key];
|
|
|
|
if (typeof prop !== 'function') {
|
|
|
|
return [key, prop];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
key,
|
|
|
|
(...args: unknown[]) =>
|
|
|
|
$q((resolve) => resolve(controller[key](...args))),
|
|
|
|
];
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-24 05:35:20 +00:00
|
|
|
type PropNames<T> = Exclude<keyof T, number | symbol>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* react2angular is used to bind a React component to an AngularJS component
|
|
|
|
* it used in an AngularJS module definition:
|
|
|
|
*
|
|
|
|
* `.component('componentName', react2angular(ComponentName, ['prop1', 'prop2']))`
|
|
|
|
*
|
|
|
|
* if the second parameter has any ts errors check that the component has the correct props
|
|
|
|
*/
|
|
|
|
export function react2angular<T, U extends PropNames<T>[]>(
|
2021-11-03 10:41:59 +00:00
|
|
|
Component: React.ComponentType<T>,
|
2022-05-24 05:35:20 +00:00
|
|
|
propNames: U & ([PropNames<T>] extends [U[number]] ? unknown : PropNames<T>)
|
|
|
|
): IComponentOptions & { name: string } {
|
2021-11-03 10:41:59 +00:00
|
|
|
const bindings = Object.fromEntries(propNames.map((key) => [key, '<']));
|
|
|
|
|
|
|
|
return {
|
|
|
|
bindings,
|
|
|
|
controller: Controller,
|
2022-05-24 05:35:20 +00:00
|
|
|
name: _.camelCase(Component.displayName || Component.name),
|
2021-11-03 10:41:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* @ngInject */
|
|
|
|
function Controller(
|
|
|
|
this: IController,
|
|
|
|
$element: HTMLElement[],
|
|
|
|
$q: ng.IQService
|
|
|
|
) {
|
2022-09-20 18:14:24 +00:00
|
|
|
let isDestroyed = false;
|
2021-11-03 10:41:59 +00:00
|
|
|
const el = $element[0];
|
2022-09-20 18:14:24 +00:00
|
|
|
|
2021-11-03 10:41:59 +00:00
|
|
|
this.$onChanges = () => {
|
2022-09-20 18:14:24 +00:00
|
|
|
if (!isDestroyed) {
|
|
|
|
const props = toProps(propNames, this, $q);
|
|
|
|
ReactDOM.render(
|
|
|
|
<StrictMode>
|
2022-01-10 13:22:21 +00:00
|
|
|
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
|
|
|
|
<Component {...(props as T)} />
|
2022-09-20 18:14:24 +00:00
|
|
|
</StrictMode>,
|
|
|
|
|
|
|
|
el
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.$onDestroy = () => {
|
|
|
|
if (!isDestroyed) {
|
|
|
|
isDestroyed = true;
|
|
|
|
ReactDOM.unmountComponentAtNode(el);
|
|
|
|
}
|
2021-11-03 10:41:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const r2a = react2angular;
|