diff --git a/otoroshi/javascript/src/components/window.js b/otoroshi/javascript/src/components/window.js index 2c9bdf135..a4dc6db33 100644 --- a/otoroshi/javascript/src/components/window.js +++ b/otoroshi/javascript/src/components/window.js @@ -4,15 +4,12 @@ import isFunction from 'lodash/isFunction'; import isString from 'lodash/isString'; import { WizardFrame } from './wizardframe'; -const KEY_NAME_ESC = 'Escape'; -const KEY_EVENT_TYPE = 'keyup'; - function EscapeModalListener({ close, children }) { const ref = useRef() const handleEscKey = (event, onClose) => { - if (event.key === KEY_NAME_ESC) { + if (event.key === 'Escape') { onClose(); } }; @@ -24,11 +21,11 @@ function EscapeModalListener({ close, children }) { }; useEffect(() => { - document.addEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, close), false); + document.addEventListener('keyup', (event) => handleEscKey(event, close), false); document.addEventListener('mousedown', (event) => handleClickOutside(event, ref, close)); return () => { - document.removeEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, close), false); + document.removeEventListener('keyup', (event) => handleEscKey(event, close), false); document.removeEventListener('mousedown', (event) => handleClickOutside(event, ref, close)); } }, []) diff --git a/otoroshi/javascript/src/forms/wizards/AuthenticationWizard.js b/otoroshi/javascript/src/forms/wizards/AuthenticationWizard.js index ed138254e..3c279db35 100644 --- a/otoroshi/javascript/src/forms/wizards/AuthenticationWizard.js +++ b/otoroshi/javascript/src/forms/wizards/AuthenticationWizard.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState, useRef } from 'react'; import * as BackOfficeServices from '../../services/BackOfficeServices'; import { TextInput } from '../../components/inputs'; import { @@ -176,23 +176,37 @@ function GoBackSelection({ goBack }) { ); } -const handleEscKey = (event, onClose) => { - if (event.key === 'Escape') { - onClose(); - } -}; -const handleClickOutside = (event, ref, onClose) => { - if (ref.current && !ref.current.contains(event.target)) { - onClose(); - } -}; +function EscapeModalListener({ hide, children }) { + + const ref = useRef() + + const handleEscKey = (event, onHide) => { + if (event.key === 'Escape') { + onHide(); + } + }; + + const handleClickOutside = (event, ref, onHide) => { + if (ref.current && !ref.current.contains(event.target)) { + onHide(); + } + }; + + useEffect(() => { + document.addEventListener('keyup', (event) => handleEscKey(event, hide), false); + document.addEventListener('mousedown', (event) => handleClickOutside(event, ref, hide)); + + return () => { + document.removeEventListener('keyup', (event) => handleEscKey(event, hide), false); + document.removeEventListener('mousedown', (event) => handleClickOutside(event, ref, hide)); + } + }, []) + + return React.cloneElement(children, { ref }) +} export class AuthenticationWizard extends React.Component { - constructor(props) { - super(props); - this.modalRef = React.createRef(); - } state = { step: 1, @@ -240,46 +254,39 @@ export class AuthenticationWizard extends React.Component { } }; - componentDidMount() { - document.addEventListener('keyup', (event) => handleEscKey(event, this.props.hide), false); - document.addEventListener('mousedown', (event) => handleClickOutside(event, this.modalRef, this.props.hide)); - } - componentWillUnmount() { - document.removeEventListener('keyup', (event) => handleEscKey(event, this.props.hide), false); - document.removeEventListener('mousedown', (event) => handleClickOutside(event, this.modalRef, this.props.hide)); - } - render() { const { step, authenticationConfig, mode } = this.state; if (mode === 'update_in_wizard') { return (