DaumPostcode.jsx 4.72 KB
Newer Older
박상호's avatar
from jw    
박상호 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React from 'react';
import PropTypes from 'prop-types';

const defaultErrorMessage = (<p>현재 Daum 우편번호 서비스를 이용할 수 없습니다. 잠시 후 다시 시도해주세요.</p>);

class DaumPostcode extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      display: 'block',
      width: this.props.width,
      height: this.props.height,
      error: false,
    };
  }

  componentDidMount() {
    const scriptId = 'daum_postcode_script';
    const isExist = !!document.getElementById(scriptId);

    if (!isExist) {
      const script = document.createElement('script');
      script.src = this.props.scriptUrl;
      script.onload = () => this.initiate(this);
      script.onerror = error => this.handleError(error);
      script.id = scriptId;
      document.body.appendChild(script);
    } else this.initiate(this);
  }

  initiate = (comp) => {
    window.daum.postcode.load(() => {
      const Postcode = new window.daum.Postcode({
        oncomplete: function oncomplete(data) {
          comp.props.onComplete(data);
          if (comp.props.autoClose) comp.setState({ display: 'none' });
        },
        onsearch: comp.props.onSearch,
        onresize: function onresize(size) {
          if (comp.props.autoResize) comp.setState({ height: size.height });
        },
        alwaysShowEngAddr: comp.props.alwaysShowEngAddr,
        animation: comp.props.animation,
        autoMapping: comp.props.autoMapping,
        autoResize: comp.props.autoResize,
        height: comp.props.height,
        hideEngBtn: comp.props.hideEngBtn,
        hideMapBtn: comp.props.hideMapBtn,
        maxSuggestItems: comp.props.maxSuggestItems,
        pleaseReadGuide: comp.props.pleaseReadGuide,
        pleaseReadGuideTimer: comp.props.pleaseReadGuideTimer,
        shorthand: comp.props.shorthand,
        showMoreHName: comp.props.showMoreHName,
        submitMode: comp.props.submitMode,
        theme: comp.props.theme,
        useSuggest: comp.props.useSuggest,
        width: comp.props.width,
        focusInput: comp.props.focusInput,
        focusContent: comp.props.focusContent,
      });

      Postcode.embed(this.wrap, { q: this.props.defaultQuery, autoClose: this.props.autoClose });
    });
  };

  handleError = (error) => {
    error.target.remove();
    this.setState({ error: true });
  };

  render() {
    const {
      style,
      onComplete,
      onSearch,
      alwaysShowEngAddr,
      animation,
      autoClose,
      autoMapping,
      autoResize,
      defaultQuery,
      errorMessage,
      height,
      hideEngBtn,
      hideMapBtn,
      maxSuggestItems,
      pleaseReadGuide,
      pleaseReadGuideTimer,
      scriptUrl,
      shorthand,
      showMoreHName,
      submitMode,
      theme,
      useSuggest,
      width,
      zonecodeOnly,
      focusInput,
      focusContent,
      ...rest
    } = this.props;

    return (
      <div
        ref={(div) => { this.wrap = div; }}
        style={{
          width: this.state.width,
          height: this.state.height,
          display: this.state.display,
          ...style,
        }}
        {...rest}
      >
        {this.state.error && this.props.errorMessage}
      </div>
    );
  }
}

DaumPostcode.propTypes = {
  onComplete: PropTypes.func.isRequired,
  onSearch: PropTypes.func,
  alwaysShowEngAddr: PropTypes.bool,
  animation: PropTypes.bool,
  autoClose: PropTypes.bool,
  autoMapping: PropTypes.bool,
  autoResize: PropTypes.bool,
  defaultQuery: PropTypes.string,
  errorMessage: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
  height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  hideEngBtn: PropTypes.bool,
  hideMapBtn: PropTypes.bool,
  maxSuggestItems: PropTypes.number,
  pleaseReadGuide: PropTypes.number,
  pleaseReadGuideTimer: PropTypes.number,
  scriptUrl: PropTypes.string,
  shorthand: PropTypes.bool,
  showMoreHName: PropTypes.bool,
  style: PropTypes.object,
  submitMode: PropTypes.bool,
  theme: PropTypes.object,
  useSuggest: PropTypes.bool,
  width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  focusInput: PropTypes.bool,
  focusContent: PropTypes.bool,
};

DaumPostcode.defaultProps = {
  onSearch: undefined,
  alwaysShowEngAddr: false,
  animation: false,
  autoClose: false,
  autoMapping: true,
  autoResize: false,
  defaultQuery: null,
  errorMessage: defaultErrorMessage,
  height: 400,
  hideEngBtn: false,
  hideMapBtn: false,
  maxSuggestItems: 10,
  pleaseReadGuide: 0,
  pleaseReadGuideTimer: 1.5,
  scriptUrl: 'https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js',
  shorthand: true,
  showMoreHName: false,
  style: null,
  submitMode: true,
  theme: null,
  useSuggest: true,
  width: '100%',
  focusInput: true,
  focusContent: true,
};

export default DaumPostcode;