1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import type { Application, Definition } from '@hotwired/stimulus';
- import React from 'react';
- import { initStimulus } from '../src/includes/initStimulus';
- export class StimulusWrapper extends React.Component<{
- debug?: boolean;
- definitions?: Definition[];
- }> {
- ref: React.RefObject<HTMLDivElement>;
- application?: Application;
- constructor(props) {
- super(props);
- this.ref = React.createRef();
- }
- componentDidMount() {
- const { debug = false, definitions = [] } = this.props;
- const root = this.ref.current || document.documentElement;
- this.application = initStimulus({ debug, definitions, root });
- }
- componentDidUpdate({ debug: prevDebug }) {
- const { debug } = this.props;
- if (debug !== prevDebug) {
- Object.assign(this.application as Application, { debug });
- }
- }
- componentWillUnmount() {
- if (!this.application) return;
- this.application.stop();
- delete this.application;
- }
- render() {
- const { children } = this.props;
- return <div ref={this.ref}>{children}</div>;
- }
- }
|