mirror of
				https://gitlab.nic.cz/turris/reforis/foris-js.git
				synced 2025-11-03 23:00:31 +01:00 
			
		
		
		
	Detect clicks outside element with a hook
This commit is contained in:
		
							
								
								
									
										2
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							@@ -1,6 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "foris",
 | 
			
		||||
  "version": "2.0.0",
 | 
			
		||||
  "version": "2.1.0",
 | 
			
		||||
  "lockfileVersion": 1,
 | 
			
		||||
  "requires": true,
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "foris",
 | 
			
		||||
  "version": "2.0.0",
 | 
			
		||||
  "version": "2.1.0",
 | 
			
		||||
  "description": "Set of components and utils for Foris and its plugins.",
 | 
			
		||||
  "author": "CZ.NIC, z.s.p.o.",
 | 
			
		||||
  "repository": {
 | 
			
		||||
 
 | 
			
		||||
@@ -5,10 +5,11 @@
 | 
			
		||||
 * See /LICENSE for more information.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import React, { useEffect, useRef } from "react";
 | 
			
		||||
import React, { useRef } from "react";
 | 
			
		||||
import PropTypes from "prop-types";
 | 
			
		||||
 | 
			
		||||
import { Portal } from "utils/Portal";
 | 
			
		||||
import { useClickOutside } from "utils/hooks";
 | 
			
		||||
 | 
			
		||||
Modal.propTypes = {
 | 
			
		||||
    /** Is modal shown value */
 | 
			
		||||
@@ -26,16 +27,7 @@ Modal.propTypes = {
 | 
			
		||||
export function Modal({ shown, setShown, children }) {
 | 
			
		||||
    const dialogRef = useRef();
 | 
			
		||||
 | 
			
		||||
    useEffect(() => {
 | 
			
		||||
        function handleClickOutsideDialog(e) {
 | 
			
		||||
            if (!dialogRef.current.contains(e.target)) setShown(false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        document.addEventListener("mousedown", handleClickOutsideDialog);
 | 
			
		||||
        return () => {
 | 
			
		||||
            document.removeEventListener("mousedown", handleClickOutsideDialog);
 | 
			
		||||
        };
 | 
			
		||||
    }, [setShown]);
 | 
			
		||||
    useClickOutside(dialogRef, () => setShown(false));
 | 
			
		||||
 | 
			
		||||
    return (
 | 
			
		||||
        <Portal containerId="modal-container">
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ import React from 'react';
 | 
			
		||||
import { act, fireEvent, render, waitForElement } from 'customTestRender';
 | 
			
		||||
import mockAxios from 'jest-mock-axios';
 | 
			
		||||
import { ForisForm } from "../components/ForisForm";
 | 
			
		||||
import { WebSockets } from "webSockets/WebSockets";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// It's possible to unittest each hooks via react-hooks-testing-library.
 | 
			
		||||
@@ -30,7 +31,6 @@ describe('useForm hook.', () => {
 | 
			
		||||
    let mockValidator;
 | 
			
		||||
    let mockPrepData;
 | 
			
		||||
    let mockPrepDataToSubmit;
 | 
			
		||||
    let mockWebSockets;
 | 
			
		||||
    let input;
 | 
			
		||||
    let form;
 | 
			
		||||
    const Child = jest.fn(props => <TestForm {...props}/>);
 | 
			
		||||
@@ -41,7 +41,7 @@ describe('useForm hook.', () => {
 | 
			
		||||
        mockValidator = jest.fn(data => data.field === 'invalidValue' ? {field: 'Error'} : {});
 | 
			
		||||
        const {getByTestId, container} = render(
 | 
			
		||||
            <ForisForm
 | 
			
		||||
                ws={mockWebSockets}
 | 
			
		||||
                ws={new WebSockets()}
 | 
			
		||||
                // Just some module which exists...
 | 
			
		||||
                forisConfig={{
 | 
			
		||||
                    endpoint: 'testEndpoint',
 | 
			
		||||
 
 | 
			
		||||
@@ -62,6 +62,7 @@ export {
 | 
			
		||||
    withEither, withSpinner, withSending, withSpinnerOnSending, withError, withErrorMessage,
 | 
			
		||||
} from "utils/conditionalHOCs";
 | 
			
		||||
export { ErrorMessage } from "utils/ErrorMessage";
 | 
			
		||||
export { useClickOutside } from "utils/hooks";
 | 
			
		||||
 | 
			
		||||
// Foris URL
 | 
			
		||||
export { ForisURLs, REFORIS_URL_PREFIX } from "forisUrls";
 | 
			
		||||
 
 | 
			
		||||
@@ -18,3 +18,19 @@ export function useConditionalTimeout({ callback, timeout = 125 }, ...callbackAr
 | 
			
		||||
    }, [condition, callback, timeout, callbackArgs]);
 | 
			
		||||
    return setCondition;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/** Execute callback when user clicks outside specified element. */
 | 
			
		||||
export function useClickOutside(element, callback) {
 | 
			
		||||
    function handleClickOutside(event) {
 | 
			
		||||
        if (element.current && !element.current.contains(event.target)) {
 | 
			
		||||
            callback(event);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    useEffect(() => {
 | 
			
		||||
        document.addEventListener("mousedown", handleClickOutside);
 | 
			
		||||
        return () => {
 | 
			
		||||
            document.removeEventListener("mousedown", handleClickOutside);
 | 
			
		||||
        };
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user