forked from vehikl/polymer-select-with-options
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select-with-options.html
40 lines (37 loc) · 1.3 KB
/
select-with-options.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
<script>
function createOption(parent, label, value) {
var option = document.createElement('option');
option.textContent = label;
option.value = value;
option.autoAdded = true;
parent.appendChild(option);
}
new Polymer({
is: 'select-with-options',
extends: 'select',
properties: {
optionsList: { observer: '_renderOptions' },
optionValue: { type: String },
optionLabel: { type: String }
},
_renderOptions: function() {
var currentValue = this.value;
var baseOptions = [];
while (this.firstChild) {
if (!this.firstChild.autoAdded) {
baseOptions.push(this.firstChild);
}
this.removeChild(this.firstChild);
}
baseOptions.forEach(function(option) {
this.appendChild(option);
}, this);
this.optionsList.forEach(function(option) {
var label = this.optionLabel ? option[this.optionLabel] : option;
var value = this.optionValue ? option[this.optionValue] : option;
createOption(this, label, value);
}, this);
this.value = currentValue;
}
});
</script>