Skip to content

Commit a60c0db

Browse files
committed
Use name in checklist input fields with slugify. Fix #243
1 parent 9c6c8d8 commit a60c0db

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

src/fields/core/fieldChecklist.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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 ],
@@ -54,7 +55,7 @@
5455
if(this.schema && this.schema.inputName && this.schema.inputName.length > 0){
5556
return this.schema.inputName + "_" + this.getItemValue(item);
5657
}
57-
return this.getItemValue(item);
58+
return slugify(this.getItemValue(item));
5859
},
5960
6061
getItemValue(item) {

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

+35
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) => {

0 commit comments

Comments
 (0)