2022-04-20 11:14:48 +02:00
|
|
|
import { Controller } from '@hotwired/stimulus';
|
2022-06-29 18:47:38 +02:00
|
|
|
import React from 'react';
|
2022-04-20 11:14:48 +02:00
|
|
|
import { render, unmountComponentAtNode } from 'react-dom';
|
|
|
|
import invariant from 'tiny-invariant';
|
|
|
|
|
|
|
|
type Props = Record<string, unknown>;
|
|
|
|
|
2022-06-29 18:47:38 +02:00
|
|
|
const componentsRegistry = new Map();
|
|
|
|
|
|
|
|
import ComboAdresseSearch from '../components/ComboAdresseSearch';
|
|
|
|
import ComboAnnuaireEducationSearch from '../components/ComboAnnuaireEducationSearch';
|
|
|
|
import ComboCommunesSearch from '../components/ComboCommunesSearch';
|
|
|
|
import ComboDepartementsSearch from '../components/ComboDepartementsSearch';
|
|
|
|
import ComboMultiple from '../components/ComboMultiple';
|
|
|
|
import ComboMultipleDropdownList from '../components/ComboMultipleDropdownList';
|
|
|
|
import ComboPaysSearch from '../components/ComboPaysSearch';
|
|
|
|
import ComboRegionsSearch from '../components/ComboRegionsSearch';
|
|
|
|
import ComboSearch from '../components/ComboSearch';
|
|
|
|
import MapEditor from '../components/MapEditor';
|
|
|
|
import MapReader from '../components/MapReader';
|
|
|
|
|
|
|
|
componentsRegistry.set('ComboAdresseSearch', ComboAdresseSearch);
|
|
|
|
componentsRegistry.set(
|
|
|
|
'ComboAnnuaireEducationSearch',
|
|
|
|
ComboAnnuaireEducationSearch
|
|
|
|
);
|
|
|
|
componentsRegistry.set('ComboCommunesSearch', ComboCommunesSearch);
|
|
|
|
componentsRegistry.set('ComboDepartementsSearch', ComboDepartementsSearch);
|
|
|
|
componentsRegistry.set('ComboMultiple', ComboMultiple);
|
|
|
|
componentsRegistry.set('ComboMultipleDropdownList', ComboMultipleDropdownList);
|
|
|
|
componentsRegistry.set('ComboPaysSearch', ComboPaysSearch);
|
|
|
|
componentsRegistry.set('ComboRegionsSearch', ComboRegionsSearch);
|
|
|
|
componentsRegistry.set('ComboSearch', ComboSearch);
|
|
|
|
componentsRegistry.set('MapEditor', MapEditor);
|
|
|
|
componentsRegistry.set('MapReader', MapReader);
|
2022-04-20 11:14:48 +02:00
|
|
|
|
|
|
|
// Initialize React components when their markup appears into the DOM.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// <div data-controller="react" data-react-component-value="ComboMultiple" data-react-props-value="{}"></div>
|
|
|
|
//
|
2022-06-23 12:52:09 +02:00
|
|
|
export class ReactController extends Controller {
|
2022-04-20 11:14:48 +02:00
|
|
|
static values = {
|
|
|
|
component: String,
|
|
|
|
props: Object
|
|
|
|
};
|
|
|
|
|
|
|
|
declare readonly componentValue: string;
|
|
|
|
declare readonly propsValue: Props;
|
|
|
|
|
|
|
|
connect(): void {
|
|
|
|
this.mountComponent(this.element as HTMLElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect(): void {
|
|
|
|
unmountComponentAtNode(this.element as HTMLElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
private mountComponent(node: HTMLElement): void {
|
|
|
|
const componentName = this.componentValue;
|
|
|
|
const props = this.propsValue;
|
|
|
|
const Component = this.getComponent(componentName);
|
|
|
|
|
|
|
|
invariant(
|
|
|
|
Component,
|
|
|
|
`Cannot find a React component with class "${componentName}"`
|
|
|
|
);
|
2022-04-28 14:45:35 +02:00
|
|
|
render(<Component {...props} />, node);
|
2022-04-20 11:14:48 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 18:47:38 +02:00
|
|
|
private getComponent(componentName: string) {
|
2022-04-20 11:14:48 +02:00
|
|
|
return componentsRegistry.get(componentName) ?? null;
|
|
|
|
}
|
|
|
|
}
|