Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

onFocus event for <input> not working inside WebComponent #28

Open
benrbray opened this issue Mar 27, 2020 · 3 comments
Open

onFocus event for <input> not working inside WebComponent #28

benrbray opened this issue Mar 27, 2020 · 3 comments

Comments

@benrbray
Copy link

I want to use React inside the Shadow DOM of a web component. I'm calling retargetEvents on the shadow root, but unfortunately the onFocus event still isn't working as expected. However, other events like onClick are working just fine. Minimal working example below. (Using TypeScript 3.8.3, React 16.13, Parcel 1.12.4, react-shadow-dom-retarget-events 1.0.11).

Expected Behavior: When receiving focus, both text inputs should print a message to the console.

Actual Behavior: Only the input NOT inside a web component correctly prints the message. The input inside the web component does not print a message on focus.

Minimal Working Example:

TSX:

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { init, GridGame } from '~/GridGame';

init();

import retargetEvents from "react-shadow-dom-retarget-events";

// WebComponent
export class InputFocusElement extends HTMLElement {
	constructor() {
		super();

		let shadowRoot = this.attachShadow({ mode: "open" });
		let template = document.createElement("div");
		shadowRoot.appendChild(template);

		ReactDOM.render(
			<InputFocusMWE />,
			template
		);

		// react-shadow-dom-retarget-events
		retargetEvents(shadowRoot);
	}
}

// React Component
interface IProps { }
class InputFocusMWE extends React.Component<IProps> {
	constructor(props: IProps) {
		super(props);
	}

	render() {
		return (
			<input
				type="text"
				onFocus={(evt) => { console.log("focus"); }}
				onClick={(evt) => { console.log("click"); }}
			/>
		);
	}
}

customElements.define("input-mwe", InputFocusElement);

ReactDOM.render(
	<InputFocusMWE />,
	document.getElementById("no-component")
);

HTML:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Hello React!</title>
	</head>
	<body>
		<div>with component:</div>
		<input-mwe></input-mwe>
		<div>without component</div>
		<div id="no-component"></div>
		<script src="./index.tsx"></script>
	</body>
</html>
@sTsaturian
Copy link

In case anyone is still facing this problem, this happens because the onFocusIn event is not retargeted. It needs to be handled in the same way as onFocusOut.

@minsohng
Copy link

minsohng commented Apr 1, 2021

In case anyone is still facing this problem, this happens because the onFocusIn event is not retargeted. It needs to be handled in the same way as onFocusOut.

what's the way that handled onFocusOut? I am trying to fix onFocus issue as well.

@sTsaturian
Copy link

sTsaturian commented Apr 1, 2021

what's the way that handled onFocusOut? I am trying to fix onFocus issue as well.

The easiest fix is to add onFocusIn here, and onFocusIn: 'onFocus', here.
Ot upgrade React to 17.x and eliminate the need for using the package.

jlap pushed a commit to jlap/react-shadow-dom-retarget-events that referenced this issue Nov 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants