import React from 'react'; import PropTypes from 'prop-types'; const defaultErrorMessage = (
현재 Daum 우편번호 서비스를 이용할 수 없습니다. 잠시 후 다시 시도해주세요.
); 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 (