|
| 1 | +import * as ko from 'knockout'; |
| 2 | +import { createElement } from 'react'; |
| 3 | +import { createRoot } from 'react-dom/client'; |
| 4 | + |
| 5 | +import { loadComponent } from '../../reactComponents/imports'; |
| 6 | + |
| 7 | +/** |
| 8 | + * REACT KNOCKOUT INTEGRATION |
| 9 | + * This is a oneway binding from knockout to react.js. Use the data-binding called reactWrapper |
| 10 | + * followed by the component name. Props are passed in as js object literal coded as a string using |
| 11 | + * the props param. Any new components used must also be added to the import file |
| 12 | + * desktop/core/src/desktop/js/reactComponents/imports.js. |
| 13 | + * |
| 14 | + * Example usage: |
| 15 | + * |
| 16 | + * <MyComponent data-bind="reactWrapper: 'MyComponent', |
| 17 | + * props: { title: 'Result title', activeExecutable: activeExecutable }"> |
| 18 | + * </MyComponent> |
| 19 | + * |
| 20 | + * |
| 21 | + * The name of the component element tag (eg <MyComponent>) can be anything, but for consistency |
| 22 | + * and to stay close to how normal react components look we use the actual component name. |
| 23 | + */ |
| 24 | + |
| 25 | +const getProps = allBindings => { |
| 26 | + const props = allBindings.get('props'); |
| 27 | + |
| 28 | + // Functions are not valid as a React child |
| 29 | + return { ...props, children: ko.toJS(props.children) }; |
| 30 | +}; |
| 31 | + |
| 32 | +ko.bindingHandlers.reactWrapper = (() => { |
| 33 | + return { |
| 34 | + init: function (el, valueAccessor, allBindings, viewModel, bindingContext) { |
| 35 | + const componentName = ko.unwrap(valueAccessor()); |
| 36 | + const props = getProps(allBindings); |
| 37 | + |
| 38 | + // The component's react root should only be created once per DOM |
| 39 | + // load so we pass it along via the bindingContext to be reused in the |
| 40 | + const reactRoot = createRoot(el); |
| 41 | + el.__KO_React_root = reactRoot; |
| 42 | + loadComponent(componentName).then(Component => { |
| 43 | + reactRoot.render(createElement(Component, props)); |
| 44 | + }); |
| 45 | + // Tell Knockout that it does not need to update the children |
| 46 | + // of this component, since that is now handled by React |
| 47 | + return { controlsDescendantBindings: true }; |
| 48 | + }, |
| 49 | + |
| 50 | + update: function (el, valueAccessor, allBindings, viewModel, bindingContext) { |
| 51 | + const componentName = ko.unwrap(valueAccessor()); |
| 52 | + const props = getProps(allBindings); |
| 53 | + |
| 54 | + loadComponent(componentName).then(Component => { |
| 55 | + el.__KO_React_root.render(createElement(Component, props)); |
| 56 | + }); |
| 57 | + |
| 58 | + // Handle KO observables |
| 59 | + Object.entries(props).forEach(([propName, propValue]) => { |
| 60 | + if (ko.isObservable(propValue)) { |
| 61 | + const koSubscription = propValue.subscribe(() => { |
| 62 | + loadComponent(componentName).then(Component => { |
| 63 | + el.__KO_React_root.render( |
| 64 | + createElement(Component, { ...props, [propName]: propValue() }) |
| 65 | + ); |
| 66 | + }); |
| 67 | + }); |
| 68 | + koSubscription.disposeWhenNodeIsRemoved(el); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + }; |
| 73 | +})(); |
0 commit comments