Postcode.js
2.22 KB
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
import * as React from 'react';
import WebView from 'react-native-webview';
const html = `
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<style> html, body { width: 100%; height: 100%; margin:0px; padding: 0px; background-color: #ececec; } </style>
</head>
<body>
<div id="layer" style="width:100%; min-height: 100%;"></div>
<script type="text/javascript">
function callback() {
var element_layer = document.getElementById('layer');
daum.postcode.load(function(){
new daum.Postcode({
...window.options,
oncomplete: function(data) {
window.ReactNativeWebView.postMessage(JSON.stringify(data));
},
onresize: function(size) {
document.getElementById('layer').style.height = size.height + 'px';
},
onclose: function(state) {
callback();
},
width : '100%',
height: '100%',
}).embed(element_layer);
});
}
function initOnReady(options) {
window.options = options;
var s = document.createElement('script');
s.type = 'text/javascript'; s.src = 'https://ssl.daumcdn.net/dmaps/map_js_init/postcode.v2.js?autoload=false';
s.onreadystatechange = callback; s.onload = callback;
var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x);
}
</script>
</body>
</html>
`;
const Postcode = (props) => {
const {jsOptions, onSelected, onError, ...otherProps} = props;
const injectedJavaScript = React.useMemo(() => `initOnReady(${JSON.stringify(jsOptions)});void(0);`, [jsOptions]);
const onMessage = React.useCallback(({nativeEvent}) => {
try {
nativeEvent.data && onSelected && onSelected(JSON.parse(nativeEvent.data));
} catch (e) {
onError(e);
}
}, [onSelected]);
return (
<WebView
{...otherProps}
source={{html, baseUrl: 'https://naver.com'}}
onMessage={onMessage}
injectedJavaScript={injectedJavaScript}
mixedContentMode={"compatibility"}
useWebKit={true}
onShouldStartLoadWithRequest={() => true}
/>
);
};
export default Postcode;