Skip to content

Commit e312446

Browse files
authored
Merge pull request #310 from hansi90/inputs_name
Use `name` in checklist input fields.
2 parents 3353860 + 19cb7bb commit e312446

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

dev/checklist/app.vue

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default {
2323
label: "Skills",
2424
model: "skills",
2525
required: true,
26+
inputName:"skill",
2627
min: 2,
2728
listBox: true,
2829
values: ["HTML5", "Javascript", "CSS3", "CoffeeScript", "AngularJS", "ReactJS", "VueJS"],

src/fields/core/fieldChecklist.vue

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.listbox.form-control(v-if="schema.listBox", :disabled="disabled")
44
.list-row(v-for="item in items", :class="{'is-checked': isItemChecked(item)}")
55
label
6-
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @change="onChanged($event, item)")
6+
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @change="onChanged($event, item)", :name="getInputName(item)")
77
| {{ getItemName(item) }}
88

99
.combobox.form-control(v-if="!schema.listBox", :disabled="disabled")
@@ -14,13 +14,14 @@
1414
.dropList
1515
.list-row(v-if="comboExpanded", v-for="item in items", :class="{'is-checked': isItemChecked(item)}")
1616
label
17-
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @change="onChanged($event, item)")
17+
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @change="onChanged($event, item)", :name="getInputName(item)")
1818
| {{ getItemName(item) }}
1919
</template>
2020

2121
<script>
2222
import {isObject, isNil, clone} from "lodash";
2323
import abstractField from "../abstractField";
24+
import { slugify } from "../../utils/schema";
2425
2526
export default {
2627
mixins: [ abstractField ],
@@ -45,10 +46,18 @@
4546
return this.value.length;
4647
4748
return 0;
48-
}
49+
}
4950
},
5051
5152
methods: {
53+
54+
getInputName(item){
55+
if(this.schema && this.schema.inputName && this.schema.inputName.length > 0){
56+
return slugify(this.schema.inputName + "_" + this.getItemValue(item));
57+
}
58+
return slugify(this.getItemValue(item));
59+
},
60+
5261
getItemValue(item) {
5362
if (isObject(item)){
5463
if (typeof this.schema["checklistOptions"] !== "undefined" && typeof this.schema["checklistOptions"]["value"] !== "undefined") {

src/utils/schema.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {get, set, each, isObject, isArray, isFunction, cloneDeep} from "lodash";
1+
import { get, set, each, isObject, isArray, isFunction, cloneDeep } from "lodash";
22

33
// Create a new model by schema default values
4-
module.exports.createDefaultObject = function (schema, obj = {}){
4+
module.exports.createDefaultObject = function (schema, obj = {}) {
55
each(schema.fields, (field) => {
66
if (get(obj, field.model) === undefined && field.default !== undefined) {
77
if (isFunction(field.default)) {
@@ -16,18 +16,18 @@ module.exports.createDefaultObject = function (schema, obj = {}){
1616
};
1717

1818
// Get a new model which contains only properties of multi-edit fields
19-
module.exports.getMultipleFields = function(schema) {
19+
module.exports.getMultipleFields = function (schema) {
2020
let res = [];
2121
each(schema.fields, (field) => {
22-
if (field.multi === true)
22+
if (field.multi === true)
2323
res.push(field);
2424
});
2525

2626
return res;
2727
};
2828

2929
// Merge many models to one 'work model' by schema
30-
module.exports.mergeMultiObjectFields = function(schema, objs) {
30+
module.exports.mergeMultiObjectFields = function (schema, objs) {
3131
let model = {};
3232

3333
let fields = module.exports.getMultipleFields(schema);
@@ -54,7 +54,7 @@ module.exports.mergeMultiObjectFields = function(schema, objs) {
5454
return model;
5555
};
5656

57-
module.exports.slugifyFormID = function(schema, prefix = "") {
57+
module.exports.slugifyFormID = function (schema, prefix = "") {
5858
// Try to get a reasonable default id from the schema,
5959
// then slugify it.
6060
if (typeof schema.id !== "undefined") {
@@ -78,3 +78,21 @@ module.exports.slugifyFormID = function(schema, prefix = "") {
7878
.replace(/([^a-zA-Z0-9-]+)/g, "");
7979
}
8080
};
81+
82+
module.exports.slugify = function (name = "") {
83+
// Return the slugified version of either:
84+
return name
85+
// NB: This is a very simple, conservative, slugify function,
86+
// avoiding extra dependencies.
87+
.toString()
88+
.trim()
89+
//.toLowerCase()
90+
// Spaces & underscores to dashes
91+
.replace(/ /g, "-")
92+
// Multiple dashes to one
93+
.replace(/-{2,}/g, "-")
94+
// Remove leading & trailing dashes
95+
.replace(/^-+|-+$/g, "")
96+
// Remove anything that isn't a (English/ASCII) letter, number or dash.
97+
.replace(/([^a-zA-Z0-9-_/./:]+)/g, "");
98+
};

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

+69
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ describe("fieldChecklist.vue", function() {
398398
type: "checklist",
399399
label: "Skills",
400400
model: "skills",
401+
inputName:"",
401402
listBox: true,
402403
values() {
403404
return [
@@ -441,6 +442,40 @@ describe("fieldChecklist.vue", function() {
441442
expect(isChecked(6)).to.be.true;
442443
});
443444

445+
446+
it("should contain input name field withouth inputName", (done) => {
447+
448+
checkboxes = listbox.querySelectorAll("input[type=checkbox]");
449+
expect(checkboxes[0].name).to.be.equal("1");
450+
expect(checkboxes[1].name).to.be.equal("2");
451+
expect(checkboxes[2].name).to.be.equal("3");
452+
expect(checkboxes[3].name).to.be.equal("4");
453+
expect(checkboxes[4].name).to.be.equal("5");
454+
expect(checkboxes[5].name).to.be.equal("6");
455+
expect(checkboxes[6].name).to.be.equal("7");
456+
457+
done();
458+
459+
});
460+
461+
it("should contain input name field with inputName", (done) => {
462+
463+
schema.inputName="skill";
464+
465+
vm.$nextTick( () => {
466+
checkboxes = listbox.querySelectorAll("input[type=checkbox]");
467+
expect(checkboxes[0].name).to.be.equal("skill_1");
468+
expect(checkboxes[1].name).to.be.equal("skill_2");
469+
expect(checkboxes[2].name).to.be.equal("skill_3");
470+
expect(checkboxes[3].name).to.be.equal("skill_4");
471+
expect(checkboxes[4].name).to.be.equal("skill_5");
472+
expect(checkboxes[5].name).to.be.equal("skill_6");
473+
expect(checkboxes[6].name).to.be.equal("skill_7");
474+
475+
done();
476+
});
477+
});
478+
444479
describe("test values reactivity to changes", () => {
445480

446481
it("listbox value should be the model value after changed", (done) => {
@@ -524,6 +559,7 @@ describe("fieldChecklist.vue", function() {
524559
type: "checklist",
525560
label: "Skills",
526561
model: "skills",
562+
inputName:"",
527563
values: [
528564
"HTML5",
529565
"Javascript",
@@ -581,6 +617,39 @@ describe("fieldChecklist.vue", function() {
581617
done();
582618
});
583619
});
620+
621+
it("should contain input name field withouth inputName", (done) => {
622+
623+
checkboxes = dropList.querySelectorAll("input[type=checkbox]");
624+
expect(checkboxes[0].name).to.be.equal("HTML5");
625+
expect(checkboxes[1].name).to.be.equal("Javascript");
626+
expect(checkboxes[2].name).to.be.equal("CSS3");
627+
expect(checkboxes[3].name).to.be.equal("CoffeeScript");
628+
expect(checkboxes[4].name).to.be.equal("AngularJS");
629+
expect(checkboxes[5].name).to.be.equal("ReactJS");
630+
expect(checkboxes[6].name).to.be.equal("VueJS");
631+
632+
done();
633+
634+
});
635+
636+
it("should contain input name field with inputName", (done) => {
637+
638+
schema.inputName="skill";
639+
640+
vm.$nextTick( () => {
641+
checkboxes = dropList.querySelectorAll("input[type=checkbox]");
642+
expect(checkboxes[0].name).to.be.equal("skill_HTML5");
643+
expect(checkboxes[1].name).to.be.equal("skill_Javascript");
644+
expect(checkboxes[2].name).to.be.equal("skill_CSS3");
645+
expect(checkboxes[3].name).to.be.equal("skill_CoffeeScript");
646+
expect(checkboxes[4].name).to.be.equal("skill_AngularJS");
647+
expect(checkboxes[5].name).to.be.equal("skill_ReactJS");
648+
expect(checkboxes[6].name).to.be.equal("skill_VueJS");
649+
650+
done();
651+
});
652+
});
584653

585654
it("should checked the values", () => {
586655
expect(isChecked(0)).to.be.false;

0 commit comments

Comments
 (0)