From 5c821a941a4e1bff7d0385087be188db5b1a45b0 Mon Sep 17 00:00:00 2001 From: Jan Fiala Date: Mon, 29 Jul 2024 15:22:35 +0200 Subject: [PATCH] fix(FormField): check focus event target to counter bubbling from portals https://github.com/facebook/react/issues/11387 --- src/js/components/FormField.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/js/components/FormField.js b/src/js/components/FormField.js index 4707e43c6bf..8d426e60472 100644 --- a/src/js/components/FormField.js +++ b/src/js/components/FormField.js @@ -49,9 +49,29 @@ export default class FormField extends Component { this.setState({focus: false}); } - _onClick () { - if (this._inputElement) { - this._inputElement.focus(); + _onClick (evt) { + if (this._inputElement && this.containerRef) { + let node = evt.target; + let containerFound = false; + while (node && node.parentNode) { + if (node === document.body) { + break; + } + if (node === this.containerRef) { + containerFound = true; + break; + } + let nodeParent = null; + try { + nodeParent = node.parentNode; + } catch (e) { + // Nothing + } + node = nodeParent; + } + if (containerFound) { + this._inputElement.focus(); + } } } @@ -89,7 +109,7 @@ export default class FormField extends Component { ) : undefined; return ( -
+
this.containerRef = ref}> {fieldError} {labelNode} {fieldHelp}