File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed
src/React/Resources/assets/src Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change 9
9
10
10
'use strict' ;
11
11
12
+ import { ComponentClass , FunctionComponent } from 'react' ;
13
+
14
+ type Component = string | FunctionComponent < object > | ComponentClass < object , any > ;
15
+
16
+ declare global {
17
+ function resolveReactComponent ( name : string ) : Component ;
18
+
19
+ interface Window {
20
+ resolveReactComponent ( name : string ) : Component ;
21
+ }
22
+ }
23
+
12
24
export function registerReactControllerComponents ( context : __WebpackModuleApi . RequireContext ) {
13
- const reactControllers : { [ key : string ] : object } = { } ;
25
+ const reactControllers : { [ key : string ] : Component } = { } ;
14
26
15
27
const importAllReactComponents = ( r : __WebpackModuleApi . RequireContext ) => {
16
28
r . keys ( ) . forEach ( ( key ) => ( reactControllers [ key ] = r ( key ) . default ) ) ;
@@ -19,7 +31,7 @@ export function registerReactControllerComponents(context: __WebpackModuleApi.Re
19
31
importAllReactComponents ( context ) ;
20
32
21
33
// Expose a global React loader to allow rendering from the Stimulus controller
22
- ( window as any ) . resolveReactComponent = ( name : string ) : object => {
34
+ window . resolveReactComponent = ( name : string ) : Component => {
23
35
const component = reactControllers [ `./${ name } .jsx` ] || reactControllers [ `./${ name } .tsx` ] ;
24
36
if ( typeof component === 'undefined' ) {
25
37
throw new Error ( 'React controller "' + name + '" does not exist' ) ;
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import { createRoot } from 'react-dom/client';
14
14
import { Controller } from '@hotwired/stimulus' ;
15
15
16
16
export default class extends Controller {
17
- readonly componentValue : string ;
17
+ readonly componentValue ? : string ;
18
18
readonly propsValue ?: object ;
19
19
20
20
static values = {
@@ -27,6 +27,10 @@ export default class extends Controller {
27
27
28
28
this . _dispatchEvent ( 'react:connect' , { component : this . componentValue , props : props } ) ;
29
29
30
+ if ( ! this . componentValue ) {
31
+ throw new Error ( 'No component specified.' ) ;
32
+ }
33
+
30
34
const component = window . resolveReactComponent ( this . componentValue ) ;
31
35
this . _renderReactElement ( React . createElement ( component , props , null ) ) ;
32
36
You can’t perform that action at this time.
0 commit comments