Skip to content

Commit b94651f

Browse files
authored
Merge pull request #40 from bernard-kms/3.1.0
Release 3.1.0
2 parents 061c44d + c45c4f7 commit b94651f

6 files changed

Lines changed: 165 additions & 60 deletions

File tree

README.md

Lines changed: 90 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,123 @@
11
# react-daum-postcode
22

3-
리액트 컴포넌트로 만든 Daum 우편번호 검색 서비스입니다. Daum 우편번호 검색 서비스를 React 환경에서 간편하게 이용할 수 있습니다.
3+
Daum 우편번호 검색 서비스를 React 환경에서 간편하게 이용할 수 있습니다.
44

5-
## 설치
5+
## Install
66

77
```shell
88
npm install --save react-daum-postcode
99
// or
1010
yarn add react-daum-postcode
1111
```
1212

13-
## 사용
13+
## Embed
14+
15+
`DaumPostcodeEmbed` 컴포넌트를 사용하여, 우편번호 검색 서비스를 임베드 방식으로 사용할 수 있습니다.
16+
17+
```javascript
18+
import React from 'react';
19+
import DaumPostcodeEmbed from 'react-daum-postcode';
20+
21+
const Postcode = () => {
22+
const handleComplete = (data) => {
23+
let fullAddress = data.address;
24+
let extraAddress = '';
25+
26+
if (data.addressType === 'R') {
27+
if (data.bname !== '') {
28+
extraAddress += data.bname;
29+
}
30+
if (data.buildingName !== '') {
31+
extraAddress += extraAddress !== '' ? `, ${data.buildingName}` : data.buildingName;
32+
}
33+
fullAddress += extraAddress !== '' ? ` (${extraAddress})` : '';
34+
}
35+
36+
console.log(fullAddress); // e.g. '서울 성동구 왕십리로2길 20 (성수동1가)'
37+
};
38+
39+
return <DaumPostcodeEmbed onComplete={handleComplete} {...props} />;
40+
};
41+
```
42+
43+
`DaumPostcodeEmbed` 컴포넌트에 다음 우편번호 서비스의 생성자 및 임베드 설정값 등을 `props`로 전달할 수 있습니다. 전달하지 않은 설정값은 다음 우편번호 서비스의 기본 설정을 따라갑니다.
44+
45+
| name | type | default | description
46+
|:----:|:----:|:-------:|:-----------|
47+
| scriptUrl | `string` | [CURRENT_URL](https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js) | Daum 우편번호 서비스의 스크립트 주소입니다.|
48+
| onComplete | `function` | `undefined` | 우편번호 검색이 끝났을 때 사용자가 선택한 정보를 받아올 콜백함수입니다. 주소 데이터의 구성은 [Daum 가이드](http://postcode.map.daum.net/guide)를 참고해주세요. |
49+
| onSearch | `function` | `undefined` | 주소를 검색할 경우 실행되는 콜백함수입니다. 검색 결과 정보의 구성은 [Daum 가이드](http://postcode.map.daum.net/guide)를 참고해주세요. |
50+
| onClose | `function` | `undefined` | 검색 결과를 선택하여, 서비스가 닫힐 때 실행되는 콜백함수입니다. |
51+
| onResize | `function` | `undefined` | 검색 결과로 인해, 우편번호 서비스의 화면 크기가 변경될 때 호출되는 콜백함수입니다. 변경된 화면 정보의 구성은 [Daum 가이드](http://postcode.map.daum.net/guide)를 참고해주세요. |
52+
| className | `string` | `undefined` | 우편번호 검색창을 감싸는 최상위 엘리먼트에 적용할 클래스 이름입니다. |
53+
| style | `object` | `{ width:"100%", height:400 }` | 우편번호 검색창을 감싸는 최상위 엘리먼트에 적용할 스타일입니다. |
54+
| defaultQuery | `string` | `undefined` | 우편번호 검색창에 기본으로 입력할 검색어입니다.
55+
| autoClose | `boolean` | `true` | 우편번호 검색 완료시 자동 닫힘 여부입니다. 주소를 선택하면, 최상위 엘리먼트를 돔에서 제거합니다. |
56+
| errorMessage | `ReactNode` | `<p>현재 Daum 우편번호 서비스를 이용할 수 없습니다. 잠시 후 다시 시도해주세요.</p>` | 우편번호 서비스 스크립트 로드에 실패했을 때 나타낼 에러 메세지 입니다. |
57+
58+
기타 Daum 우편번호 생성자 속성들을 동일한 이름으로 props를 전달할 수 있습니다. 속성값에 대해서는 [Daum 우편번호 서비스 가이드](http://postcode.map.daum.net/guide#attributes)를 참고해주세요.
59+
60+
## Popup
61+
62+
`useDaumPostcodePopup` hook 을 사용하여, 반환받은 함수를 통해 우편번호 검색 서비스를 팝업 방식으로 이용할 수 있습니다.
1463

1564
```javascript
1665
import React from 'react';
17-
import DaumPostcode from 'react-daum-postcode';
66+
import { useDaumPostcodePopup } from 'react-daum-postcode';
1867

1968
const Postcode = () => {
69+
const open = useDaumPostcodePopup(scriptUrl);
70+
2071
const handleComplete = (data) => {
2172
let fullAddress = data.address;
22-
let extraAddress = '';
23-
73+
let extraAddress = '';
74+
2475
if (data.addressType === 'R') {
2576
if (data.bname !== '') {
2677
extraAddress += data.bname;
2778
}
2879
if (data.buildingName !== '') {
29-
extraAddress += (extraAddress !== '' ? `, ${data.buildingName}` : data.buildingName);
80+
extraAddress += extraAddress !== '' ? `, ${data.buildingName}` : data.buildingName;
3081
}
31-
fullAddress += (extraAddress !== '' ? ` (${extraAddress})` : '');
82+
fullAddress += extraAddress !== '' ? ` (${extraAddress})` : '';
3283
}
3384

34-
console.log(fullAddress); // e.g. '서울 성동구 왕십리로2길 20 (성수동1가)'
35-
}
85+
console.log(fullAddress); // e.g. '서울 성동구 왕십리로2길 20 (성수동1가)'
86+
};
87+
88+
const handleClick = () => {
89+
open({ onComplete: handleComplete });
90+
};
3691

3792
return (
38-
<DaumPostcode
39-
onComplete={handleComplete}
40-
{ ...props }
41-
/>
93+
<button type='button' onClick={handleClick}>
94+
Open
95+
</button>
4296
);
43-
}
97+
};
4498
```
4599

46-
## props
47-
48-
- `onComplete` _[function]_ - 우편번호 검색이 끝났을 때 사용자가 선택한 정보를 받아올 콜백함수입니다.
49-
- `function(data: object) => void` : 주소 데이터의 구성은 [Daum 우편번호 서비스 가이드](http://postcode.map.daum.net/guide)를 참고해주세요.
50-
- `onSearch` _[function]_ - 주소를 검색할 경우에 실행되는 콜백함수입니다.
51-
- `function(data: object) => void` : 검색결과 정보의 구성은 [Daum 우편번호 서비스 가이드](http://postcode.map.daum.net/guide)를 참고해주세요.
52-
- `onClose` _[function]_ - 검색 결과를 선택하여, 우편번호 검색이 닫힐 때 실행되는 콜백함수입니다.
53-
- `onResize` _[function]_ - 검색 결과로 인해, 우편번호 서비스의 화면 크기가 변경될 때 실행되는 콜백함수입니다.
54-
- `className` _[string]_ - 우편번호 검색창을 감싸는 최상위 엘리먼트에 적용할 클래스명입니다.
55-
- `style` _[object]_ - 우편번호 검색창을 감싸는 최상위 엘리먼트에 적용할 스타일입니다. 기본값: `{ width: 100%, height: 400 }`
56-
- `scriptUrl` _[string]_ - 컴포넌트에서 사용할 Daum 우편번호 스크립트 주소입니다. 기본값: `'https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js'`
57-
- `defaultQuery` _[string]_ - 우편번호 검색창에 기본으로 입력할 검색어입니다. 기본값 : `undefined`
58-
- `autoClose` _[boolean]_ - 우편번호 검색 완료시, 자동 닫힘 여부입니다. 주소를 선택하면, 최상위 엘리먼트를 돔에서 제거합니다. 기본값: `true`
59-
- `errorMessage` _[React element]_ - Daum 우편번호 스크립트가 로드되지 않을 때 나타낼 에러 메시지입니다. 기본값: `<p>현재 Daum 우편번호 서비스를 이용할 수 없습니다. 잠시 후 다시 시도해주세요.</p>`
60-
- 기타 Daum 우편번호 생성자 속성들을 동일한 이름으로 props를 전달할 수 있습니다. 속성값에 대해서는 [Daum 우편번호 서비스 가이드](http://postcode.map.daum.net/guide#attributes)를 참고해주세요.
100+
`useDaumPostcodePopup` 실행 시 우편번호 서비스의 스크립트 주소를 전달할 수 있습니다. 반환한 함수를 실행할 때 다음 우편번호 서비스의 생성자 및 팝업 설정값을 전달할 수 있습니다. 전달하지 않은 설정값은 다음 우편번호 서비스의 기본 설정을 따라갑니다.
101+
102+
| name | type | default | description
103+
|:----:|:----:|:-------:|:-----------|
104+
| scriptUrl | `string` | [CURRENT_URL](https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js) | Daum 우편번호 서비스의 스크립트 주소입니다.|
105+
| onComplete | `function` | `undefined` | 우편번호 검색이 끝났을 때 사용자가 선택한 정보를 받아올 콜백함수입니다. 주소 데이터의 구성은 [Daum 가이드](http://postcode.map.daum.net/guide)를 참고해주세요. |
106+
| onSearch | `function` | `undefined` | 주소를 검색할 경우 실행되는 콜백함수입니다. 검색 결과 정보의 구성은 [Daum 가이드](http://postcode.map.daum.net/guide)를 참고해주세요. |
107+
| onClose | `function` | `undefined` | 검색 결과를 선택하여, 서비스가 닫힐 때 실행되는 콜백함수입니다. |
108+
| onResize | `function` | `undefined` | 검색 결과로 인해, 우편번호 서비스의 화면 크기가 변경될 때 호출되는 콜백함수입니다. 변경된 화면 정보의 구성은 [Daum 가이드](http://postcode.map.daum.net/guide)를 참고해주세요. |
109+
| width | `string\|number` | `undefined` | 우편번호 검색창의 가로 너비입니다. |
110+
| height | `string\|number` | `undefined` | 우편번호 검색창의 세로 높이입니다. |
111+
| defaultQuery | `string` | `undefined` | 우편번호 검색창에 기본으로 입력할 검색어입니다. |
112+
| top | `string\|number` | `undefined` | 팝업의 Y 위치를 나타내는 값입니다. |
113+
| left | `string\|number` | `undefined` | 팝업의 X 위치를 나타내는 값입니다. |
114+
| popupTitle | `string` | `undefined` | 팝업창의 상태표시줄에 나오는 Title 값을 지정할 수 있습니다. 전달하지 않을 경우, 다음 우편번호의 기본 설정 문구가 출력됩니다. |
115+
| popupKey | `string` | `undefined` | 팝업창의 key 입니다. 전달하지 않을 경우 매번 새창이 열리게 됩니다. |
116+
| autoClose | `boolean` | `true` | 우편번호 검색 완료시 자동 닫힘 여부입니다. 주소를 선택하면 팝업창이 닫힙니다.
117+
118+
기타 Daum 우편번호 생성자 속성들을 동일한 이름으로 props를 전달할 수 있습니다. 속성값에 대해서는 [Daum 우편번호 서비스 가이드](http://postcode.map.daum.net/guide#attributes)를 참고해주세요.
119+
61120

62121
## 안내
63122

64-
`react-daum-postcode`는 Daum 우편번호 서비스와 독립적으로 제작된 패키지입니다. React환경에서 발생하는 `react-daum-postcode`의 버그는 패키지 레포지터리의 [이슈트래커](https://github.com/kimminsik-bernard/react-daum-postcode/issues)에 말씀해주세요. 만약 Daum 우편번호 서비스 자체의 문제라고 생각하신다면, 다음 우편번호 서비스의 [FAQ](https://github.com/daumPostcode/QnA/blob/master/README.md)[이슈트래커](https://github.com/daumPostcode/QnA/issues)를 참조해주세요.
123+
`react-daum-postcode`는 Daum 우편번호 서비스와 독립적으로 제작된 패키지입니다. React환경에서 발생하는 `react-daum-postcode`의 버그는 패키지 레포지터리의 [이슈트래커](https://github.com/kimminsik-bernard/react-daum-postcode/issues)에 말씀해주세요. 만약 Daum 우편번호 서비스 자체의 문제라고 생각하신다면, 다음 우편번호 서비스의 [FAQ](https://github.com/daumPostcode/QnA/blob/master/README.md)[이슈트래커](https://github.com/daumPostcode/QnA/issues)를 참조해주세요.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-daum-postcode",
33
"version": "3.0.3",
4-
"description": "React daum-postcode component",
4+
"description": "Daum Postcode service for React",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",
77
"files": [
@@ -53,6 +53,7 @@
5353
},
5454
"keywords": [
5555
"react",
56+
"daum",
5657
"postcode",
5758
"zipcode"
5859
],
Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { Component, createRef, CSSProperties } from 'react';
2-
import loadPostcode, { PostcodeConstructor, PostcodeOptions } from './loadPostcode';
2+
import loadPostcode, { postcodeScriptUrl, PostcodeConstructor, PostcodeOptions } from './loadPostcode';
33

4-
export interface DaumPostcodeProps extends Omit<PostcodeOptions, 'oncomplete' | 'onresize' | 'onclose' | 'onsearch'> {
4+
export interface DaumPostcodeEmbedProps
5+
extends Omit<PostcodeOptions, 'oncomplete' | 'onresize' | 'onclose' | 'onsearch' | 'width' | 'height'> {
56
onComplete?: PostcodeOptions['oncomplete'];
67
onResize?: PostcodeOptions['onresize'];
78
onClose?: PostcodeOptions['onclose'];
@@ -13,6 +14,13 @@ export interface DaumPostcodeProps extends Omit<PostcodeOptions, 'oncomplete' |
1314
scriptUrl?: string;
1415
autoClose?: boolean;
1516
}
17+
/**
18+
* @deprecated
19+
* type 'DaumPostcodeProps' is renamed to 'DaumPostcodeEmbedProps'.
20+
* use 'DaumPostcodeEmbedProps' instead of 'DaumPostcodeProps'.
21+
* it will be removed future version.
22+
*/
23+
export type DaumPostcodeProps = DaumPostcodeEmbedProps;
1624

1725
interface State {
1826
hasError: boolean;
@@ -26,12 +34,12 @@ const defaultStyle = {
2634
};
2735

2836
const defaultProps = {
29-
scriptUrl: 'https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js',
37+
scriptUrl: postcodeScriptUrl,
3038
errorMessage: defaultErrorMessage,
3139
autoClose: true,
3240
};
3341

34-
class DaumPostcode extends Component<DaumPostcodeProps, State> {
42+
class DaumPostcodeEmbed extends Component<DaumPostcodeEmbedProps, State> {
3543
static defaultProps = defaultProps;
3644

3745
wrap = createRef<HTMLDivElement>();
@@ -50,21 +58,8 @@ class DaumPostcode extends Component<DaumPostcodeProps, State> {
5058

5159
initiate = (Postcode: PostcodeConstructor) => {
5260
if (!this.wrap.current) return;
53-
const {
54-
scriptUrl,
55-
className,
56-
style,
57-
defaultQuery,
58-
autoClose,
59-
errorMessage,
60-
width,
61-
height,
62-
onComplete,
63-
onClose,
64-
onResize,
65-
onSearch,
66-
...options
67-
} = this.props;
61+
const { scriptUrl, className, style, defaultQuery, autoClose, errorMessage, onComplete, onClose, onResize, onSearch, ...options } =
62+
this.props;
6863

6964
const postcode = new Postcode({
7065
...options,
@@ -99,4 +94,4 @@ class DaumPostcode extends Component<DaumPostcodeProps, State> {
9994
}
10095
}
10196

102-
export default DaumPostcode;
97+
export default DaumPostcodeEmbed;

src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import DaumPostcode, { DaumPostcodeProps } from './DaumPostcode';
2-
import { Address, Search, State } from './loadPostcode';
1+
import DaumPostcodeEmbed, { DaumPostcodeEmbedProps, DaumPostcodeProps } from './DaumPostcodeEmbed';
2+
import useDaumPostcodePopup, { DaumPostcodePopupParams } from './useDaumPostcodePopup';
3+
import loadPostcode, { Address, Search, State } from './loadPostcode';
34

4-
export type { DaumPostcodeProps, Address, Search, State };
5-
export { DaumPostcode };
6-
export default DaumPostcode;
5+
export type { DaumPostcodeEmbedProps, DaumPostcodeProps, DaumPostcodePopupParams, Address, Search, State };
6+
export { loadPostcode, DaumPostcodeEmbed, useDaumPostcodePopup };
7+
export default DaumPostcodeEmbed;

src/loadPostcode.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
declare global {
22
interface Window {
33
daum?: {
4-
postcode?: {
4+
postcode: {
55
load: (fn: () => void) => void;
66
version: string;
77
_validParam_: boolean;
88
};
9-
Postcode?: PostcodeConstructor;
9+
Postcode: PostcodeConstructor;
1010
};
1111
}
1212
}
@@ -84,6 +84,8 @@ export interface PostcodeOptions {
8484
focusInput?: boolean;
8585
focusContent?: boolean;
8686
autoMapping?: boolean;
87+
autoMappingRoad?: boolean;
88+
autoMappingJibun?: boolean;
8789
shorthand?: boolean;
8890
pleaseReadGuide?: number;
8991
pleaseReadGuideTimer?: number;
@@ -121,11 +123,13 @@ export interface Postcode {
121123
embed(element: HTMLElement, embedOptions?: EmbedOptions): void;
122124
}
123125

126+
export const postcodeScriptUrl = 'https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js';
127+
124128
const loadPostcode = (function () {
125129
const scriptId = 'daum_postcode_script';
126130
let promise: Promise<PostcodeConstructor> | null = null;
127131

128-
return function (url: string): Promise<PostcodeConstructor> {
132+
return function (url: string = postcodeScriptUrl): Promise<PostcodeConstructor> {
129133
if( promise ) return promise;
130134

131135
promise = new Promise((resolve, reject) => {

src/useDaumPostcodePopup.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { useCallback, useEffect } from 'react';
2+
3+
import loadPostcode, { PostcodeOptions, OpenOptions, postcodeScriptUrl } from './loadPostcode';
4+
5+
export type DaumPostcodePopupParams = Omit<PostcodeOptions, 'oncomplete' | 'onresize' | 'onclose' | 'onsearch'> &
6+
Omit<OpenOptions, 'q'> & {
7+
onComplete?: PostcodeOptions['oncomplete'];
8+
onResize?: PostcodeOptions['onresize'];
9+
onClose?: PostcodeOptions['onclose'];
10+
onSearch?: PostcodeOptions['onsearch'];
11+
onError?: (error: Error) => void;
12+
defaultQuery?: string;
13+
};
14+
15+
function useDaumPostcodePopup(scriptUrl = postcodeScriptUrl) {
16+
useEffect(() => {
17+
loadPostcode(scriptUrl);
18+
}, [scriptUrl]);
19+
20+
const open = useCallback(
21+
(options?: DaumPostcodePopupParams) => {
22+
const { defaultQuery, left, top, popupKey, popupTitle, autoClose, onComplete, onResize, onClose, onSearch, onError, ...others } = {
23+
...options,
24+
};
25+
26+
return loadPostcode(scriptUrl)
27+
.then((Postcode) => {
28+
const postcode = new Postcode({
29+
...others,
30+
oncomplete: onComplete,
31+
onsearch: onSearch,
32+
onresize: onResize,
33+
onclose: onClose,
34+
});
35+
postcode.open({ q: defaultQuery, left, top, popupTitle, popupKey, autoClose });
36+
})
37+
.catch(onError);
38+
},
39+
[scriptUrl]
40+
);
41+
42+
return open;
43+
}
44+
45+
export default useDaumPostcodePopup;

0 commit comments

Comments
 (0)