Skip to content

Commit 61fadce

Browse files
committed
Fix select not updating value
1 parent 0f4f6af commit 61fadce

File tree

5 files changed

+108
-35
lines changed

5 files changed

+108
-35
lines changed

docs/src/pages/components/Select.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@
223223
country: '',
224224
font: '',
225225
food: '',
226-
users: []
226+
users: [
227+
'jim_halpert',
228+
'michael_scott'
229+
]
227230
}),
228231
methods: {
229232
setPulpFiction() {

src/components/mdInputContainer/mdInputContainer.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<style lang="scss" src="./mdInputContainer.scss"></style>
1414

1515
<script>
16+
import isArray from '../../core/utils/isArray';
17+
1618
export default {
1719
props: {
1820
mdInline: Boolean,
@@ -37,7 +39,7 @@
3739
},
3840
computed: {
3941
hasValue() {
40-
if (this.value && this.value.constructor === Array) {
42+
if (isArray(this.value)) {
4143
return this.value.length > 0;
4244
}
4345

src/components/mdSelect/mdOption.vue

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@
3030
}),
3131
computed: {
3232
isSelected() {
33-
return this.value && this.parentSelect.value && this.value.toString() === this.parentSelect.value.toString();
33+
if (this.value && this.parentSelect.value) {
34+
let thisValue = this.value.toString();
35+
36+
if (this.parentSelect.multiple) {
37+
return this.parentSelect.value.indexOf(thisValue) >= 0;
38+
}
39+
40+
return this.value && this.parentSelect.value && thisValue === this.parentSelect.value.toString();
41+
}
42+
43+
return false;
3444
},
3545
classes() {
3646
return {
@@ -40,20 +50,20 @@
4050
}
4151
},
4252
methods: {
43-
selectOption(changed) {
53+
selectOption() {
4454
if (!this.parentSelect.multiple) {
45-
this.parentSelect.selectOption(this.value, this.$refs.item.textContent, changed);
55+
this.parentSelect.selectOption(this.value, this.$refs.item.textContent);
4656
} else {
4757
this.check = !this.check;
4858
}
49-
},
50-
selectIfValueMatches() {
51-
if (this.isSelected) {
52-
this.selectOption(true);
53-
}
5459
}
5560
},
5661
watch: {
62+
isSelected(selected) {
63+
if (this.parentSelect.multiple) {
64+
this.check = selected;
65+
}
66+
},
5767
check(check) {
5868
if (check) {
5969
this.parentSelect.selectMultiple(this.index, this.value, this.$refs.item.textContent);
@@ -73,13 +83,12 @@
7383
this.parentSelect.optionsAmount++;
7484
this.index = this.parentSelect.optionsAmount;
7585
76-
this.parentSelect.options[this.index] = {};
77-
78-
this.$watch(() => {
79-
return this.parentSelect.value;
80-
}, this.selectIfValueMatches);
81-
82-
this.selectIfValueMatches();
86+
this.parentSelect.multipleOptions[this.index] = {};
87+
this.parentSelect.options[this.index] = this;
88+
},
89+
beforeDestroy() {
90+
delete this.parentSelect.options[this.index];
91+
delete this.parentSelect.multipleOptions[this.index];
8392
}
8493
};
8594
</script>

src/components/mdSelect/mdSelect.vue

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="md-select" :class="classes">
33
<md-menu :md-close-on-select="!multiple">
4-
<span class="md-select-value" md-menu-trigger ref="value">{{ selectedValue || multiplevalue || placeholder }}</span>
4+
<span class="md-select-value" md-menu-trigger ref="value">{{ selectedText || multipleText || placeholder }}</span>
55

66
<md-menu-content class="md-select-content" :class="contentClasses">
77
<slot></slot>
@@ -18,6 +18,7 @@
1818

1919
<script>
2020
import getClosestVueParent from '../../core/utils/getClosestVueParent';
21+
import isArray from '../../core/utils/isArray';
2122
2223
export default {
2324
props: {
@@ -33,7 +34,9 @@
3334
data() {
3435
return {
3536
selectedValue: null,
36-
multiplevalue: null,
37+
selectedText: null,
38+
multipleText: null,
39+
multipleOptions: {},
3740
options: {},
3841
optionsAmount: 0
3942
};
@@ -52,42 +55,93 @@
5255
return this.mdMenuClass;
5356
}
5457
},
58+
watch: {
59+
value(value) {
60+
this.setTextAndvalue(value);
61+
}
62+
},
5563
methods: {
56-
changeValue(value, parentValue, changed) {
57-
if (changed) {
58-
this.$emit('change', value);
64+
getSingleValue(value) {
65+
let output = {};
66+
67+
Object.keys(this.options).forEach((index) => {
68+
const options = this.options[index];
69+
70+
if (options.value === value) {
71+
output.value = value;
72+
output.text = options.$refs.item.textContent;
73+
}
74+
});
75+
76+
return output;
77+
},
78+
getMultipleValue(modelValue) {
79+
if (isArray(this.value)) {
80+
let outputText = [];
81+
82+
modelValue.forEach((value) => {
83+
Object.keys(this.options).forEach((index) => {
84+
const options = this.options[index];
85+
86+
if (options.value === value) {
87+
let text = options.$refs.item.textContent;
88+
89+
this.multipleOptions[index] = {
90+
value,
91+
text
92+
};
93+
outputText.push(text);
94+
}
95+
});
96+
});
97+
98+
return {
99+
value: modelValue,
100+
text: outputText.join(', ')
101+
};
59102
}
60103
104+
return {};
105+
},
106+
setTextAndvalue(modelValue) {
107+
const output = this.multiple ? this.getMultipleValue(modelValue) : this.getSingleValue(modelValue);
108+
109+
this.selectedValue = output.value;
110+
this.selectedText = output.text;
111+
61112
if (this.parentContainer) {
62-
this.$parent.setValue(parentValue || value);
113+
this.$parent.setValue(output.text);
63114
}
64115
},
116+
changeValue(value) {
117+
this.$emit('input', value);
118+
this.$emit('change', value);
119+
},
65120
selectMultiple(index, value, text) {
66-
let output = [];
67121
let values = [];
68122
69-
this.options[index] = {
123+
this.multipleOptions[index] = {
70124
value,
71125
text
72126
};
73127
74-
for (var key in this.options) {
75-
if (this.options.hasOwnProperty(key) && this.options[key].text) {
76-
output.push(this.options[key].text);
77-
values.push(this.options[key].value);
128+
for (var key in this.multipleOptions) {
129+
if (this.multipleOptions.hasOwnProperty(key) && this.multipleOptions[key].value) {
130+
values.push(this.multipleOptions[key].value);
78131
}
79132
}
80133
81-
this.multiplevalue = output.join(', ');
82-
this.changeValue(values, this.multiplevalue);
134+
this.changeValue(values);
83135
},
84-
selectOption(value, text, changed) {
85-
this.selectedValue = text;
86-
this.changeValue(value, null, changed);
136+
selectOption(value, text) {
137+
this.selectedText = text;
138+
this.changeValue(value);
87139
}
88140
},
89141
mounted() {
90-
this.parentContainer = this.parentContent = getClosestVueParent(this.$parent, 'md-input-container');
142+
this.parentContainer = getClosestVueParent(this.$parent, 'md-input-container');
143+
144+
this.setTextAndvalue(this.value);
91145
92146
if (this.parentContainer) {
93147
this.parentContainer.setValue(this.value);

src/core/utils/isArray.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const isArray = (value) => {
2+
return value && value.constructor === Array;
3+
};
4+
5+
export default isArray;

0 commit comments

Comments
 (0)