Skip to content

Commit

Permalink
Merge pull request #258 from ODOICHON/feat/#257
Browse files Browse the repository at this point in the history
feat: 주소 찾기 모달 닫는 기능 추가
  • Loading branch information
JunJongHun authored Aug 18, 2024
2 parents a232eff + ec7915d commit dbb7deb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/components/Trade/AddressModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useRef } from 'react';
import DaumPostcodeEmbed, { Address } from 'react-daum-postcode';
import styles from './styles.module.scss';

Expand All @@ -8,6 +8,7 @@ type AddressModalProps = {
};

function AddressModal({ callback, setIsPostcodeOpen }: AddressModalProps) {
const modalRef = useRef<HTMLDivElement>(null);
const handleComplete = (data: Address) => {
let fullAddress = data.address;
let extraAddress = '';
Expand All @@ -25,9 +26,19 @@ function AddressModal({ callback, setIsPostcodeOpen }: AddressModalProps) {
callback(fullAddress, data.zonecode);
setIsPostcodeOpen((pre) => !pre);
};
useEffect(() => {
const handleCloseModal = (e: Event | React.MouseEvent) => {
if (!modalRef.current || !modalRef.current!.contains(e.target as Node))
setIsPostcodeOpen(false);
};
window.addEventListener('mousedown', handleCloseModal);
return () => {
window.removeEventListener('mousedown', handleCloseModal);
};
}, [modalRef]);
return (
<div className={styles.background}>
<section className={styles.container}>
<section ref={modalRef} className={styles.container}>
<DaumPostcodeEmbed onComplete={handleComplete} autoClose={false} />
</section>
</div>
Expand Down

0 comments on commit dbb7deb

Please sign in to comment.