-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcountry-picker.html
130 lines (115 loc) · 4.01 KB
/
country-picker.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-list/iron-list.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-dialog/paper-dialog.html">
<link rel="import" href="../paper-dialog-scrollable/paper-dialog-scrollable.html">
<link rel="import" href="../paper-input/paper-input.html">
<!--
`country-picker`
An element to select country code/dial code.
@demo demo/index.html
-->
<dom-module id="country-picker">
<template>
<style>
:host {
display: flex;
flex-direction: column;
}
iron-list {
flex: 1 1 auto;
width: 70vw;
max-width: 400px;
margin-left: auto;
margin-right: auto;
}
iron-icon {
color: hsl(0, 0%, 50%);
margin-right: 12px;
}
.dial-code {
position: absolute;
right: 16px;
}
</style>
<paper-dialog id="dialog">
<paper-input type="search"
label="Search country"
on-input="_query"
value="{{_searchTerm}}">
<iron-icon icon="search" prefix></iron-icon>
</paper-input>
<paper-dialog-scrollable>
<iron-ajax url="countries.json" auto on-response="_handleResponse"></iron-ajax>
<iron-list items="[[_filteredData]]" as="item">
<template is="dom-bind">
<div>
<paper-item on-tap="_onItemSelected">
<paper-item-body>
<div>[[item.name]] [[item.code]]</div>
</paper-item-body>
<paper-item-body class="dial-code">
<span style="color: #2196f3">[[item.dialCode]]</span>
</paper-item-body>
</paper-item>
</div>
</template>
</iron-list>
</paper-dialog-scrollable>
</paper-dialog>
</template>
<script>
Polymer({
is: 'country-picker',
properties: {
_searchTerm: {
type: String,
value: ''
},
_allData: {
type: Array,
value: []
},
_filteredData: {
type: Array,
value: []
},
/**
* Selected country
* @type {{name: String, code: String, dialCode: String}}
*/
country: {
type: Object,
value: {},
notify: true
}
},
_onItemSelected: function (e) {
this.country = e.model.get('item');
this.$.dialog.close();
},
_handleResponse: function (data) {
this._allData = data.detail.response;
this._filteredData = data.detail.response;
},
_query: function (e) {
this._filteredData = this._filterData(this._allData, this._searchTerm);
},
_filterData: function (items, searchTerm) {
if (searchTerm == '') {
return items;
}
return items.filter(function (item) {
return item.name.toLowerCase().includes(searchTerm.toLowerCase());
});
},
/**
* Opens the country picker dialog
*/
open: function () {
this.$.dialog.open();
}
});
</script>
</dom-module>