Skip to content

Better behavior for radios and checklist #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions src/fields/core/fieldChecklist.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template lang="pug">
.wrapper
.listbox.form-control(v-if="schema.listBox", :disabled="disabled")
.list-row(v-for="item in items", :class="{'is-checked': getItemIsChecked(item)}")
.list-row(v-for="item in items", :class="{'is-checked': isItemChecked(item)}")
label
input(type="checkbox", :checked="getItemIsChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
| {{ getItemName(item) }}

.combobox.form-control(v-if="!schema.listBox", :disabled="disabled")
Expand All @@ -12,9 +12,9 @@
.arrow

.dropList
.list-row(v-if="comboExpanded", v-for="item in items", :class="{'is-checked': getItemIsChecked(item)}")
.list-row(v-if="comboExpanded", v-for="item in items", :class="{'is-checked': isItemChecked(item)}")
label
input(type="checkbox", :checked="getItemIsChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
| {{ getItemName(item) }}
</template>

Expand Down Expand Up @@ -49,22 +49,39 @@
},

methods: {
getItemID(item) {
if (isObject(item) && item.id)
return item.id;

return item;
getItemValue(item) {
if (isObject(item)){
if (typeof this.schema["checklistOptions"] !== "undefined" && typeof this.schema["checklistOptions"]["value"] !== "undefined") {
return item[this.schema.checklistOptions.value];
} else {
if (typeof item["value"] !== "undefined") {
return item.value;
} else {
throw "value is not defined. If you want to use another key name, add a `value` property under `checklistOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/checklist.html#checklist-field-with-object-values";
}
}
} else {
return item;
}
},

getItemName(item) {
if (isObject(item) && item.name)
return item.name;

return item;
if (isObject(item)){
if (typeof this.schema["checklistOptions"] !== "undefined" && typeof this.schema["checklistOptions"]["name"] !== "undefined") {
return item[this.schema.checklistOptions.name];
} else {
if (typeof item["name"] !== "undefined") {
return item.name;
} else {
throw "name is not defined. If you want to use another key name, add a `name` property under `checklistOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/checklist.html#checklist-field-with-object-values";
}
}
} else {
return item;
}
},

getItemIsChecked(item) {
return (this.value && this.value.indexOf(this.getItemID(item)) != -1);
isItemChecked(item) {
return (this.value && this.value.indexOf(this.getItemValue(item)) != -1);
},

onChanged(event, item) {
Expand All @@ -73,9 +90,9 @@
}

if (event.target.checked) {
this.value.push(this.getItemID(item));
this.value.push(this.getItemValue(item));
} else {
this.value.splice(this.value.indexOf(this.getItemID(item)), 1);
this.value.splice(this.value.indexOf(this.getItemValue(item)), 1);
}
},

Expand Down
49 changes: 28 additions & 21 deletions src/fields/core/fieldRadios.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,41 @@
},

methods: {
onSelection(item) {
if (isObject(item) && this.schema.radiosOptions.value && item[this.schema.radiosOptions.value]){
this.value = item[this.schema.radiosOptions.value];
} else{
this.value = item;
}
},
getItemValue(item) {
if (isObject(item) && this.schema.radiosOptions.value && item[this.schema.radiosOptions.value]){
return item[this.schema.radiosOptions.value];
if (isObject(item)){
if (typeof this.schema["radiosOptions"] !== "undefined" && typeof this.schema["radiosOptions"]["value"] !== "undefined") {
return item[this.schema.radiosOptions.value];
} else {
if (typeof item["value"] !== "undefined") {
return item.value;
} else {
throw "value is not defined. If you want to use another key name, add a `value` property under `radiosOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/radios.html#radios-field-with-object-values";
}
}
} else {
return item;
}

return item;
},
getItemName(item) {
if (isObject(item) && this.schema.radiosOptions.name && item[this.schema.radiosOptions.name]){
return item[this.schema.radiosOptions.name];
if (isObject(item)){
if (typeof this.schema["radiosOptions"] !== "undefined" && typeof this.schema["radiosOptions"]["name"] !== "undefined") {
return item[this.schema.radiosOptions.name];
} else {
if (typeof item["name"] !== "undefined") {
return item.name;
} else {
throw "name is not defined. If you want to use another key name, add a `name` property under `radiosOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/radios.html#radios-field-with-object-values";
}
}
} else {
return item;
}

return item;
},
onSelection(item) {
this.value = this.getItemValue(item);
},
isItemChecked(item) {
let currentValue;
if (isObject(item) && this.schema.radiosOptions.value && item[this.schema.radiosOptions.value]){
currentValue = item[this.schema.radiosOptions.value];
} else{
currentValue = item;
}
let currentValue = this.getItemValue(item);
return (currentValue === this.value);
},
}
Expand Down
158 changes: 143 additions & 15 deletions test/unit/specs/fields/fieldChecklist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,20 @@ describe("fieldChecklist.vue", function() {

});

describe("check static values with { id, name } objects", () => {
describe("check static values with { value, name } objects (default key name)", () => {
let schema = {
type: "checklist",
label: "Skills",
model: "skills",
listBox: true,
values: [
{ id: 1, name: "HTML5" },
{ id: 2, name: "Javascript" },
{ id: 3, name: "CSS3" },
{ id: 4, name: "CoffeeScript" },
{ id: 5, name: "AngularJS" },
{ id: 6, name: "ReactJS" },
{ id: 7, name: "VueJS" }
{ value: 1, name: "HTML5" },
{ value: 2, name: "Javascript" },
{ value: 3, name: "CSS3" },
{ value: 4, name: "CoffeeScript" },
{ value: 5, name: "AngularJS" },
{ value: 6, name: "ReactJS" },
{ value: 7, name: "VueJS" }
]
};
let model = { skills: [2, 7] };
Expand Down Expand Up @@ -265,6 +265,134 @@ describe("fieldChecklist.vue", function() {

});

describe("check static values with { id, label } objects (custom key name with `checklistOptions`)", () => {
let schema = {
type: "checklist",
label: "Skills",
model: "skills",
listBox: true,
values: [
{ id: 1, label: "HTML5" },
{ id: 2, label: "Javascript" },
{ id: 3, label: "CSS3" },
{ id: 4, label: "CoffeeScript" },
{ id: 5, label: "AngularJS" },
{ id: 6, label: "ReactJS" },
{ id: 7, label: "VueJS" }
],
checklistOptions: {
value: "id",
name: "label"
}
};
let model = { skills: [2, 7] };
let listbox;
let checkboxes;
let listRowList;

function isChecked(idx) {
return(checkboxes[idx].checked);
}

before( () => {
createField(this, schema, model, false);
listbox = el.querySelector(".listbox");
checkboxes = listbox.querySelectorAll("input[type=checkbox]");
listRowList = listbox.querySelectorAll(".list-row");
});

it("should contain items", () => {
expect(checkboxes.length).to.be.equal(7);
});

it("should checked the values", () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.true;
expect(isChecked(2)).to.be.false;
expect(isChecked(3)).to.be.false;
expect(isChecked(4)).to.be.false;
expect(isChecked(5)).to.be.false;
expect(isChecked(6)).to.be.true;
});

describe("test values reactivity to changes", () => {

it("listbox value should be the model value after changed", (done) => {
model.skills = [3];
vm.$nextTick( () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.false;
expect(isChecked(2)).to.be.true;
expect(isChecked(3)).to.be.false;
expect(isChecked(4)).to.be.false;
expect(isChecked(5)).to.be.false;
expect(isChecked(6)).to.be.false;
done();
});

});

it("model value should be the listbox value if changed", (done) => {
checkboxes[0].checked = true;
trigger(checkboxes[0], "input");

vm.$nextTick( () => {
expect(model.skills).to.be.deep.equal([3, 1]);
done();
});

});

});

describe("test 'is-checked' class attribution reactivity to changes", () => {

it(".list-row with checked input should have a 'is-checked' class", () => {
expect(listRowList[0].classList.contains("is-checked")).to.be.true;
expect(listRowList[1].classList.contains("is-checked")).to.be.false;
expect(listRowList[2].classList.contains("is-checked")).to.be.true;
expect(listRowList[3].classList.contains("is-checked")).to.be.false;
expect(listRowList[4].classList.contains("is-checked")).to.be.false;
expect(listRowList[5].classList.contains("is-checked")).to.be.false;
expect(listRowList[6].classList.contains("is-checked")).to.be.false;
});

it(".list-row with checked input should have a 'is-checked' class after model value is changed", (done) => {
model.skills = [4];
vm.$nextTick( () => {
expect(listRowList[0].classList.contains("is-checked")).to.be.false;
expect(listRowList[1].classList.contains("is-checked")).to.be.false;
expect(listRowList[2].classList.contains("is-checked")).to.be.false;
expect(listRowList[3].classList.contains("is-checked")).to.be.true;
expect(listRowList[4].classList.contains("is-checked")).to.be.false;
expect(listRowList[5].classList.contains("is-checked")).to.be.false;
expect(listRowList[6].classList.contains("is-checked")).to.be.false;
done();
});

});

it(".list-row with checked input should have a 'is-checked' class after listbox value is changed", (done) => {
checkboxes[0].checked = true;
trigger(checkboxes[0], "input");

vm.$nextTick( () => {
expect(listRowList[0].classList.contains("is-checked")).to.be.true;
expect(listRowList[1].classList.contains("is-checked")).to.be.false;
expect(listRowList[2].classList.contains("is-checked")).to.be.false;
expect(listRowList[3].classList.contains("is-checked")).to.be.true;
expect(listRowList[4].classList.contains("is-checked")).to.be.false;
expect(listRowList[5].classList.contains("is-checked")).to.be.false;
expect(listRowList[6].classList.contains("is-checked")).to.be.false;
done();
});

});

});

});

describe("check function values", () => {
let schema = {
type: "checklist",
Expand All @@ -273,13 +401,13 @@ describe("fieldChecklist.vue", function() {
listBox: true,
values() {
return [
{ id: 1, name: "HTML5" },
{ id: 2, name: "Javascript" },
{ id: 3, name: "CSS3" },
{ id: 4, name: "CoffeeScript" },
{ id: 5, name: "AngularJS" },
{ id: 6, name: "ReactJS" },
{ id: 7, name: "VueJS" }
{ value: 1, name: "HTML5" },
{ value: 2, name: "Javascript" },
{ value: 3, name: "CSS3" },
{ value: 4, name: "CoffeeScript" },
{ value: 5, name: "AngularJS" },
{ value: 6, name: "ReactJS" },
{ value: 7, name: "VueJS" }
];
}
};
Expand Down
Loading