diff --git a/README.md b/README.md index 30bbf74..eaf60c0 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,12 @@ A super simple way to render React components in AngularJS. This was written with the goal of facilitating conversion of an AngularJS app without doing a full rewrite. +## Versions + +Support for React 18 started in `react-in-angularjs@18` + +Support for versions 16 or less use `react-in-angularjs@17.7.1` + ## Install The function contained in this library is small enough that you might just want to copy paste it. Feel free to do so. Otherwise: diff --git a/index.js b/index.js index 27b7713..58c64dc 100644 --- a/index.js +++ b/index.js @@ -1,2 +1 @@ -"use strict";var React=require("react");var ReactDOMClient=require("react-dom/client");var isPlainObject=require("lodash/isPlainObject");var isEqual=require("lodash/isEqual");function angularize(Component,componentName,angularApp,bindings){bindings=bindings||{};if(typeof window==="undefined"||typeof angularApp==="undefined")return;angularApp.component(componentName,{bindings:bindings,controller:["$element",function($element){var _this=this;this.root=ReactDOMClient.createRoot($element[0]);if(window.angular){this.$scope=window.angular.element($element).scope();var previous={};this.$onInit=function(){for(var _i=0,_Object$keys=Object.keys(bindings);_i<_Object$keys.length;_i++){var bindingKey=_Object$keys[_i];if(/^data[A-Z]/.test(bindingKey)){console.warn("\"".concat(bindingKey,"\" binding for ").concat(componentName," component will be undefined because AngularJS ignores attributes starting with data-"))}if(bindings[bindingKey]==="="){previous[bindingKey]=window.angular.copy(_this[bindingKey])}}};this.$doCheck=function(){for(var _i2=0,_Object$keys2=Object.keys(previous);_i2<_Object$keys2.length;_i2++){var previousKey=_Object$keys2[_i2];if(!equals(_this[previousKey],previous[previousKey])){_this.$onChanges();previous[previousKey]=window.angular.copy(_this[previousKey]);return}}}}this.$onChanges=function(){_this.root.render(React.createElement(Component,_this))};this.$onDestroy=function(){_this.root.unmount()}}]})}function angularizeDirective(Component,directiveName,angularApp,bindings){bindings=bindings||{};if(typeof window==="undefined"||typeof angularApp==="undefined")return;angularApp.directive(directiveName,function(){return{scope:bindings,replace:true,link:function link(scope,element){scope.$scope=scope;var root=ReactDOMClient.createRoot($element[0]);root.render(React.createElement(Component,scope));var keys=[];for(var _i3=0,_Object$keys3=Object.keys(bindings);_i3<_Object$keys3.length;_i3++){var bindingKey=_Object$keys3[_i3];if(/^data[A-Z]/.test(bindingKey)){console.warn("\"".concat(bindingKey,"\" binding for ").concat(directiveName," directive will be undefined because AngularJS ignores attributes starting with data-"))}if(bindings[bindingKey]!=="&"){keys.push(bindingKey)}}scope.$watchGroup(keys,function(){root.render(React.createElement(Component,scope))});scope.$on("$destroy",function(){root.unmount()})}}})}function getService(serviceName){if(typeof window==="undefined"||typeof window.angular==="undefined")return{};return window.angular.element(document.body).injector().get(serviceName)}function equals(o1,o2){if(isPlainObject(o1)&&isPlainObject(o2)){return isEqual(o1,o2)}return window.angular.equals(o1,o2)}module.exports={getService:getService,angularize:angularize,angularizeDirective:angularizeDirective}; - +"use strict";var React=require("react");var ReactDOMClient=require("react-dom/client");var isPlainObject=require("lodash/isPlainObject");var isEqual=require("lodash/isEqual");function angularize(Component,componentName,angularApp,bindings){bindings=bindings||{};if(typeof window==="undefined"||typeof angularApp==="undefined")return;angularApp.component(componentName,{bindings:bindings,controller:["$element",function($element){var _this=this;this.root=ReactDOMClient.createRoot($element[0]);if(window.angular){this.$scope=window.angular.element($element).scope();var previous={};this.$onInit=function(){for(var _i=0,_Object$keys=Object.keys(bindings);_i<_Object$keys.length;_i++){var bindingKey=_Object$keys[_i];if(/^data[A-Z]/.test(bindingKey)){console.warn("'".concat(bindingKey,"' binding for ").concat(componentName," component will be undefined because AngularJS ignores attributes starting with data-"))}if(bindings[bindingKey]==="="){previous[bindingKey]=window.angular.copy(_this[bindingKey])}}};this.$doCheck=function(){for(var _i2=0,_Object$keys2=Object.keys(previous);_i2<_Object$keys2.length;_i2++){var previousKey=_Object$keys2[_i2];if(!equals(_this[previousKey],previous[previousKey])){_this.$onChanges();previous[previousKey]=window.angular.copy(_this[previousKey]);return}}}}this.$onChanges=function(){_this.root.render(React.createElement(Component,_this))};this.$onDestroy=function(){_this.root.unmount()}}]})}function angularizeDirective(Component,directiveName,angularApp,bindings){bindings=bindings||{};if(typeof window==="undefined"||typeof angularApp==="undefined")return;angularApp.directive(directiveName,function(){return{scope:bindings,replace:true,link:function link(scope,element){scope.$scope=scope;var root=ReactDOMClient.createRoot($element[0]);root.render(React.createElement(Component,scope));var keys=[];for(var _i3=0,_Object$keys3=Object.keys(bindings);_i3<_Object$keys3.length;_i3++){var bindingKey=_Object$keys3[_i3];if(/^data[A-Z]/.test(bindingKey)){console.warn("\"".concat(bindingKey,"\" binding for ").concat(directiveName," directive will be undefined because AngularJS ignores attributes starting with data-"))}if(bindings[bindingKey]!=="&"){keys.push(bindingKey)}}scope.$watchGroup(keys,function(root){root.render(React.createElement(Component,scope))});scope.$on("$destroy",function(){root.unmount()})}}})}function getService(serviceName){if(typeof window==="undefined"||typeof window.angular==="undefined")return{};return window.angular.element(document.body).injector().get(serviceName)}function equals(o1,o2){if(isPlainObject(o1)&&isPlainObject(o2)){return isEqual(o1,o2)}return window.angular.equals(o1,o2)}module.exports={getService:getService,angularize:angularize,angularizeDirective:angularizeDirective}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 0f8910d..985a02b 100644 --- a/src/index.js +++ b/src/index.js @@ -51,6 +51,7 @@ function angularize(Component, componentName, angularApp, bindings) { this.$onChanges = () => { this.root.render(React.createElement(Component, this)); + }; this.$onDestroy = () => { this.root.unmount();