๐Ÿค ์€์ง€log ๐Ÿค

[REACT] react-daum-postcode๋ฅผ ์ด์šฉํ•˜์—ฌ ์šฐํŽธ๋ฒˆํ˜ธ ๊ฒ€์ƒ‰ ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ’™ React

[REACT] react-daum-postcode๋ฅผ ์ด์šฉํ•˜์—ฌ ์šฐํŽธ๋ฒˆํ˜ธ ๊ฒ€์ƒ‰ ๊ตฌํ˜„ํ•˜๊ธฐ

Eun_zii 2023. 10. 30. 16:05

 

 

์นด์นด์˜ค์—์„œ ์ œ๊ณตํ•˜๋Š” ์šฐํŽธ๋ฒˆํ˜ธ ์„œ๋น„์Šค ๋ฅผ

๋ฆฌ์•กํŠธํ™˜๊ฒฝ์—์„œ ์กฐ๊ธˆ ๋” ๊ฐ„ํŽธํ•˜๊ฒŒ ์ด์šฉํ• ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์–ด์„œ ์‚ฌ์šฉํ•ด๋ดค๋‹ค ใ…Ž!

 

npm i react-daum-postcode

 

์ž„๋ฒ ๋“œ ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ณผ ํŒ์—… ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ณ , ๋‚˜๋Š” ํŒ์—… ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

 

โœ… ์ „์ฒด ์ฝ”๋“œ

import React, { useState } from "react";
import styled from "styled-components";
import { useDaumPostcodePopup } from "react-daum-postcode";
import { postcodeScriptUrl } from "react-daum-postcode/lib/loadPostcode";

const PostCode = () => {
  const open = useDaumPostcodePopup(postcodeScriptUrl);

  const [address, setAddress] = useState("");
  const [detailAddress, setDetailAddress] = useState("");
  const [extraAddress, setExtraAddress] = useState("");
  const [zonecode, setZonecode] = useState("");

  const handleComplete = (data: {
    address: any;
    addressType: string;
    bname: React.SetStateAction<string>;
    buildingName: string;
    zonecode: string;
  }) => {
    let fullAddress = data.address;

    if (data.addressType === "R") {
      if (data.bname !== "") {
        fullAddress += " " + data.bname;
      }
      if (data.buildingName !== "") {
        fullAddress += " " + data.buildingName;
      }
    }

    setAddress(fullAddress);
    setExtraAddress(data.bname);
    setDetailAddress("");
    setZonecode(data.zonecode);
  };

  const handleClick = () => {
    open({ onComplete: handleComplete });
  };

  return (
    <Input>
      <div className="row">
        <div className="col">
          <input placeholder="์šฐํŽธ๋ฒˆํ˜ธ" value={zonecode} readOnly />
          <input
            type="button"
            defaultValue="์šฐํŽธ๋ฒˆํ˜ธ ์ฐพ๊ธฐ"
            onClick={handleClick}
            style={{
              border: "2px solid black",
            }}
            readOnly
          />
        </div>
      </div>
      <input
        placeholder="์ฃผ์†Œ"
        defaultValue={address}
        style={{ width: "130%" }}
        readOnly
      />
      <input
        placeholder="์ƒ์„ธ์ฃผ์†Œ"
        defaultValue={detailAddress}
        onChange={(e) => setDetailAddress(e.target.value)}
        style={{ width: "100%" }}
      />
    </Input>
  );
};

const Input = styled.div`
  input {
    margin: 5px 5px 5px;
    padding: 5px 5px 5px;
    font-size: 28px;
    border-radius: 5px;
  }
`;

export default PostCode;

 

์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ์™„-์„ฑ ใ…Ž-๐Ÿ˜‹

728x90