Skip to content

Commit 934146c

Browse files
authored
Merge pull request #161 from cristijora/bug/multiselect-object
#159 Vue-multiselect doesn't work properly with objects
2 parents 16c256e + 84d3e41 commit 934146c

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/fields/optional/fieldVueMultiSelect.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@
5858
}
5959
},
6060
customLabel(){
61-
if (typeof this.schema.selectOptions !== "undefined" && typeof this.schema.selectOptions.customLabel !== "undefined" && this.schema.selectOptions.customLabel === "function") {
61+
if (typeof this.schema.selectOptions !== "undefined" && typeof this.schema.selectOptions.customLabel !== "undefined" && typeof this.schema.selectOptions.customLabel === "function") {
6262
return this.schema.selectOptions.customLabel;
6363
} else {
64-
return function(currentLabel){return currentLabel;};
64+
//this will let the multiselect library use the default behavior if customLabel is not specified
65+
return undefined;
6566
}
6667
}
6768
},

test/unit/specs/fields/fieldVueMultiSelect.spec.js

+62
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,67 @@ describe("fieldVueMultiSelect.vue", function() {
100100
});
101101

102102
});
103+
104+
describe("with objects", () => {
105+
const option = {
106+
name: "Vue.js",
107+
language: "JavaScript"
108+
};
109+
let schema = {...schema};
110+
let model = {
111+
city: [option]
112+
};
113+
schema.values = [
114+
{
115+
name: "Vue.js",
116+
language: "JavaScript"
117+
},
118+
{
119+
name: "Rails",
120+
language: "Ruby"
121+
},
122+
{
123+
name: "Sinatra",
124+
language: "Ruby"
125+
}];
126+
schema.selectOptions = {};
127+
before(() => {
128+
createField(this, schema, model, false);
129+
input = el.querySelector(".multiselect");
130+
});
131+
132+
it("model value should work with objects", (done) => {
133+
schema.selectOptions = {label: "name", trackBy: "name"};
134+
vm.$nextTick(() => {
135+
expect(model.city.length).to.be.equal(1);
136+
expect(model.city[0]).to.be.eql(schema.values[0]);
137+
done();
138+
});
139+
});
140+
141+
it("options should contain only text specified in label", (done) => {
142+
schema.selectOptions = {label: "language", trackBy: "language"};
143+
vm.$nextTick(() => {
144+
let options = input.querySelectorAll("li .multiselect__option");
145+
expect(options[0].querySelector("span").textContent).to.be.equal("JavaScript");
146+
done();
147+
});
148+
});
149+
150+
it("options should contain custom text specified in customLabel", (done) => {
151+
schema.selectOptions = {
152+
label: "name",
153+
trackBy: "name",
154+
customLabel: ({name, language}) => {
155+
return `${name}-${language}`;
156+
}
157+
};
158+
vm.$nextTick(() => {
159+
let options = input.querySelectorAll("li .multiselect__option");
160+
expect(options[0].querySelector("span").textContent).to.be.equal("Vue.js-JavaScript");
161+
done();
162+
});
163+
});
164+
});
103165
});
104166
});

0 commit comments

Comments
 (0)