Skip to content

Commit 2f29943

Browse files
committed
rename options to formOptions because conflicted with vueMultiSelect property
Move core of `getFieldID` to utils
1 parent 890ed44 commit 2f29943

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

src/fields/abstractField.js

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { get as objGet, each, isFunction, isString, isArray } from "lodash";
22
import validators from "../utils/validators";
3+
import { slugifyFormID } from "../utils/schema";
34

45
function convertValidator(validator) {
56
if (isString(validator)) {
@@ -17,7 +18,7 @@ export default {
1718
props: [
1819
"model",
1920
"schema",
20-
"options",
21+
"formOptions",
2122
"disabled"
2223
],
2324

@@ -164,30 +165,8 @@ export default {
164165
},
165166

166167
getFieldID(schema) {
167-
const idPrefix = this.options && this.options.fieldIdPrefix ? this.options.fieldIdPrefix : "";
168-
// Try to get a reasonable default id from the schema,
169-
// then slugify it.
170-
if (typeof schema.id !== "undefined") {
171-
// If an ID's been explicitly set, use it unchanged
172-
return idPrefix + schema.id;
173-
} else {
174-
// Return the slugified version of either:
175-
return idPrefix + (schema.inputName || schema.label || schema.model)
176-
// NB: This is a very simple, conservative, slugify function,
177-
// avoiding extra dependencies.
178-
.toString()
179-
.trim()
180-
.toLowerCase()
181-
// Spaces & underscores to dashes
182-
.replace(/ |_/g, "-")
183-
// Multiple dashes to one
184-
.replace(/-{2,}/g, "-")
185-
// Remove leading & trailing dashes
186-
.replace(/^-+|-+$/g, "")
187-
// Remove anything that isn't a (English/ASCII) letter, number or dash.
188-
.replace(/([^a-zA-Z0-9-]+)/g, "")
189-
;
190-
}
168+
const idPrefix = this.formOptions && this.formOptions.fieldIdPrefix ? this.formOptions.fieldIdPrefix : "";
169+
return slugifyFormID(schema, idPrefix);
191170
}
192171

193172
}

src/formGenerator.vue

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ div.vue-form-generator(v-if='schema != null')
99
i.icon
1010
.helpText(v-html='field.help')
1111
.field-wrap
12-
component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema='field', :options='options', @model-updated='modelUpdated', @validated="onFieldValidated")
12+
component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema='field', :formOptions='options', @model-updated='modelUpdated', @validated="onFieldValidated")
1313
.buttons(v-if='buttonVisibility(field)')
1414
button(v-for='btn in field.buttons', @click='buttonClickHandler(btn, field)', :class='btn.classes') {{ btn.label }}
1515
.hint(v-if='field.hint') {{ field.hint }}
@@ -27,7 +27,7 @@ div.vue-form-generator(v-if='schema != null')
2727
i.icon
2828
.helpText(v-html='field.help')
2929
.field-wrap
30-
component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema='field', :options='options',@model-updated='modelUpdated', @validated="onFieldValidated")
30+
component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema='field', :formOptions='options',@model-updated='modelUpdated', @validated="onFieldValidated")
3131
.buttons(v-if='buttonVisibility(field)')
3232
button(v-for='btn in field.buttons', @click='buttonClickHandler(btn, field)', :class='btn.classes') {{ btn.label }}
3333
.hint(v-if='field.hint') {{ field.hint }}
@@ -38,7 +38,7 @@ div.vue-form-generator(v-if='schema != null')
3838
<script>
3939
// import Vue from "vue";
4040
import {each, isFunction, isNil, isArray, isString} from "lodash";
41-
import getFieldID from "./fields/abstractField";
41+
import { slugifyFormID } from "./utils/schema";
4242
4343
// Load all fields from '../fields' folder
4444
let fieldComponents = {};
@@ -64,8 +64,6 @@ div.vue-form-generator(v-if='schema != null')
6464
export default {
6565
components: fieldComponents,
6666
67-
mixins: [ getFieldID ],
68-
6967
props: {
7068
schema: Object,
7169
@@ -343,7 +341,12 @@ div.vue-form-generator(v-if='schema != null')
343341
fieldErrors(field) {
344342
let res = this.errors.filter(e => e.field == field);
345343
return res.map(item => item.error);
346-
}
344+
},
345+
346+
getFieldID(schema) {
347+
const idPrefix = this.options && this.options.fieldIdPrefix ? this.options.fieldIdPrefix : "";
348+
return slugifyFormID(schema, idPrefix);
349+
}
347350
}
348351
};
349352

src/utils/schema.js

+26
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,30 @@ module.exports.mergeMultiObjectFields = function(schema, objs) {
5252
});
5353

5454
return model;
55+
};
56+
57+
module.exports.slugifyFormID = function(schema, prefix = "") {
58+
// Try to get a reasonable default id from the schema,
59+
// then slugify it.
60+
if (typeof schema.id !== "undefined") {
61+
// If an ID's been explicitly set, use it unchanged
62+
return prefix + schema.id;
63+
} else {
64+
// Return the slugified version of either:
65+
return prefix + (schema.inputName || schema.label || schema.model)
66+
// NB: This is a very simple, conservative, slugify function,
67+
// avoiding extra dependencies.
68+
.toString()
69+
.trim()
70+
.toLowerCase()
71+
// Spaces & underscores to dashes
72+
.replace(/ |_/g, "-")
73+
// Multiple dashes to one
74+
.replace(/-{2,}/g, "-")
75+
// Remove leading & trailing dashes
76+
.replace(/^-+|-+$/g, "")
77+
// Remove anything that isn't a (English/ASCII) letter, number or dash.
78+
.replace(/([^a-zA-Z0-9-]+)/g, "")
79+
;
80+
}
5581
};

0 commit comments

Comments
 (0)