Skip to content

[React] Fix window types #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/React/Resources/assets/src/register_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@

'use strict';

import { ComponentClass, FunctionComponent } from 'react';

type Component = string | FunctionComponent<object> | ComponentClass<object, any>;

declare global {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This piece of code is inspired from a StackOverflow example and the Vue example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only part I'm not sure about is how you define the type Component. But, I think it's pretty safe to use this and if there is a better way, someone will tell us :). This is just to make TypeScript happier. So, I think the worst-case scenario is that someone tells us a nicer way to make TypeScript happier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. I just looked at how Typescript inferred the type where it is used.

If someone knows a better way, let's use it

function resolveReactComponent(name: string): Component;

interface Window {
resolveReactComponent(name: string): Component;
}
}

export function registerReactControllerComponents(context: __WebpackModuleApi.RequireContext) {
const reactControllers: { [key: string]: object } = {};
const reactControllers: { [key: string]: Component } = {};

const importAllReactComponents = (r: __WebpackModuleApi.RequireContext) => {
r.keys().forEach((key) => (reactControllers[key] = r(key).default));
Expand All @@ -19,7 +31,7 @@ export function registerReactControllerComponents(context: __WebpackModuleApi.Re
importAllReactComponents(context);

// Expose a global React loader to allow rendering from the Stimulus controller
(window as any).resolveReactComponent = (name: string): object => {
window.resolveReactComponent = (name: string): Component => {
const component = reactControllers[`./${name}.jsx`] || reactControllers[`./${name}.tsx`];
if (typeof component === 'undefined') {
throw new Error('React controller "' + name + '" does not exist');
Expand Down
6 changes: 5 additions & 1 deletion src/React/Resources/assets/src/render_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createRoot } from 'react-dom/client';
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
readonly componentValue: string;
readonly componentValue?: string;
readonly propsValue?: object;

static values = {
Expand All @@ -27,6 +27,10 @@ export default class extends Controller {

this._dispatchEvent('react:connect', { component: this.componentValue, props: props });

if (!this.componentValue) {
throw new Error('No component specified.');
}

Copy link
Contributor Author

@AlexandreGerault AlexandreGerault Nov 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to solve the unitialized error that Typescript raise when compiling assets, as it seems to be related to stimulus code -- we don't initialise it here but I guess it would in the runtime

const component = window.resolveReactComponent(this.componentValue);
this._renderReactElement(React.createElement(component, props, null));

Expand Down