Skip to content

Commit b74fb37

Browse files
committed
added observer for select
1 parent 3a6bd18 commit b74fb37

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

build/metro.all.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/metro.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29673,7 +29673,8 @@ onmessage = function (event) {\
2967329673
init: function( options, elem ) {
2967429674
this._super(elem, options, SelectDefaultConfig, {
2967529675
list: null,
29676-
placeholder: null
29676+
placeholder: null,
29677+
observer: null,
2967729678
});
2967829679

2967929680
return this;
@@ -29946,6 +29947,21 @@ onmessage = function (event) {\
2994629947
this.enable();
2994729948
}
2994829949

29950+
this.observer = new MutationObserver(this._updateSelect.bind(this));
29951+
this.observer.observe(element[0], {
29952+
childList: true,
29953+
subtree: true
29954+
});
29955+
},
29956+
29957+
_updateSelect: function(mutation){
29958+
for (let record of mutation) {
29959+
if (record.type === 'childList') {
29960+
if (record.addedNodes.length || record.removedNodes.length) {
29961+
this._createOptions();
29962+
}
29963+
}
29964+
}
2994929965
},
2995029966

2995129967
_createEvents: function(){

build/metro.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/components/select/select.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
init: function( options, elem ) {
6363
this._super(elem, options, SelectDefaultConfig, {
6464
list: null,
65-
placeholder: null
65+
placeholder: null,
66+
observer: null,
6667
});
6768

6869
return this;
@@ -335,6 +336,21 @@
335336
this.enable();
336337
}
337338

339+
this.observer = new MutationObserver(this._updateSelect.bind(this))
340+
this.observer.observe(element[0], {
341+
childList: true,
342+
subtree: true
343+
});
344+
},
345+
346+
_updateSelect: function(mutation){
347+
for (let record of mutation) {
348+
if (record.type === 'childList') {
349+
if (record.addedNodes.length || record.removedNodes.length) {
350+
this._createOptions()
351+
}
352+
}
353+
}
338354
},
339355

340356
_createEvents: function(){

tests/select.html

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,20 @@ <h1>Select test page</h1>
106106
<br>
107107
<br>
108108
<div class="example">
109-
<div class="d-flex flex-row no-wrap flex-align-start">
110-
<select id="select3" data-role="select" data-prepend="Select item:"></select>
111-
<button class="button" onclick="fillSelect()">Fill select</button>
109+
<div class="row">
110+
<div class="cell-md-6">
111+
<div class="d-flex flex-row no-wrap flex-align-start">
112+
<select id="select3" data-role="select" data-prepend="Select item:"></select>
113+
<button class="button" onclick="fillSelect()">Fill select</button>
114+
</div>
115+
116+
</div>
117+
<div class="cell-md-6">
118+
<div class="d-flex flex-row no-wrap flex-align-start">
119+
<select id="select3_1" data-role="select" data-prepend="Select item:"></select>
120+
<button class="button" onclick="fillSelect3_1()">Fill select</button>
121+
</div>
122+
</div>
112123
</div>
113124
</div>
114125

@@ -175,6 +186,12 @@ <h1>Select test page</h1>
175186
select.val("");
176187
}
177188

189+
function fillSelect3_1(){
190+
const select = $("#select3_1")
191+
select.append($("<option>").attr("value", "value_1").text(`value_1`))
192+
select.append($("<option>").attr("value", "value_2").attr("selected", "selected").text(`value_2`))
193+
}
194+
178195
// sel.on("change", function () {
179196
// console.log("changed");
180197
// });

0 commit comments

Comments
 (0)