From feb7b4cad39eecf826d7a944c23f4a66c07cbfaa Mon Sep 17 00:00:00 2001 From: Shaun Orssaud Date: Tue, 20 Feb 2024 12:46:13 +0900 Subject: [PATCH] UPD duration inputs now close automatically --- .../layout/durationInput/DurationInput.tsx | 20 +++++++++-- .../DurationInputWithFloatingBox.tsx | 35 ++++++++++++------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/frontend/src/components/layout/durationInput/DurationInput.tsx b/frontend/src/components/layout/durationInput/DurationInput.tsx index 995cd7e..6a882ef 100644 --- a/frontend/src/components/layout/durationInput/DurationInput.tsx +++ b/frontend/src/components/layout/durationInput/DurationInput.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import styled from 'styled-components'; const DurationInputWrapper = styled.div` @@ -21,13 +21,27 @@ const DurationInput: React.FC = ({ onDurationChange }) => { const [hours, setHours] = useState(0); const [minutes, setMinutes] = useState(0); const [seconds, setSeconds] = useState(0); + const durationInputRef = useRef(null); + + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if (durationInputRef.current && !durationInputRef.current.contains(event.target as Node)) { + onDurationChange({ days, hours, minutes, seconds }); + } + }; + + document.addEventListener('mousedown', handleClickOutside); + return () => { + document.removeEventListener('mousedown', handleClickOutside); + }; + }, [onDurationChange, days, hours, minutes, seconds]); const handleDurationChange = () => { onDurationChange({ days, hours, minutes, seconds }); }; return ( - + = ({ onDurationChange }) => { ); }; -export default DurationInput; \ No newline at end of file +export default DurationInput; diff --git a/frontend/src/components/layout/durationInput/DurationInputWithFloatingBox.tsx b/frontend/src/components/layout/durationInput/DurationInputWithFloatingBox.tsx index 5d5e66e..cc981ce 100644 --- a/frontend/src/components/layout/durationInput/DurationInputWithFloatingBox.tsx +++ b/frontend/src/components/layout/durationInput/DurationInputWithFloatingBox.tsx @@ -1,5 +1,4 @@ -// DurationInputWithFloatingBox.tsx -import React, { useState } from 'react'; +import React, { useEffect, useRef } from 'react'; import styled from 'styled-components'; import DurationInput from './DurationInput'; @@ -12,15 +11,6 @@ const FloatingBoxWrapper = styled.div` background: #555555; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); border-radius: 8px; - display: flex; - flex-direction: column; - align-items: stretch; -`; - -const CloseButton = styled.button` - margin-top: 10px; - cursor: pointer; - flex: 1; `; interface DurationInputWithFloatingBoxProps { @@ -30,10 +20,29 @@ interface DurationInputWithFloatingBoxProps { } const DurationInputWithFloatingBox: React.FC = ({ isOpen, onClose, onDurationChange }) => { + const ref = useRef(null); + + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if (ref.current && !ref.current.contains(event.target as Node)) { + onClose(); + } + }; + + if (isOpen) { + document.addEventListener('mousedown', handleClickOutside); + } else { + document.removeEventListener('mousedown', handleClickOutside); + } + + return () => { + document.removeEventListener('mousedown', handleClickOutside); + }; + }, [isOpen, onClose]); + return ( - + - Close ); };