Skip to content

Commit

Permalink
Add support for schema.legend & field id prefixes
Browse files Browse the repository at this point in the history
* Add support for an optional legend for each schema/fieldset. Expects the schema to look something  like this:

```
schema: {
  legend: "Contact Details",
  fields: [
    {
      type: "input",
      inputType: "text",
      label: "Name",
      model: "name"
    },
...
```

* Add support for field id prefixes, at the form level. So you can add a `fieldIdPrefix: 'prefix_here_'` to your `formOptions` and this will be prepended to _all_ field id's.
  • Loading branch information
Duncan Lock committed May 25, 2017
1 parent 5552079 commit a6165c8
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 10 deletions.
59 changes: 59 additions & 0 deletions dev/multipleforms/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<template lang="html">
<form>
<vue-form-generator :schema="section1" :model="model" :options="formOptions"></vue-form-generator>
<vue-form-generator :schema="section2" :model="model" :options="formOptions"></vue-form-generator>
</form>
</template>

<script>
import Vue from "vue";
export default {
data () {
return {
model: {
name: 'Brian Blessed',
email: "[email protected]",
pref_1: 'blah'
},
section1: {
legend: "Contact Details",
fields: [
{
type: "input",
inputType: "text",
label: "Name",
model: "name"
},
{
type: "input",
inputType: "email",
label: "Email",
model: "email"
}
]
},
section2: {
fields: [
{
type: "input",
inputType: "text",
label: "Pref 1",
model: "pref_1"
}
]
},
formOptions: {
fieldIdPrefix: 'frm1_'
}
}
},
created() {
window.app = this;
}
}
</script>
12 changes: 12 additions & 0 deletions dev/multipleforms/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>vue-form-generator multiple forms demo</title>
</head>
<body>
<div class="container-fluid"></div>
<div id="app"></div>
<script src="/mforms.js"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions dev/multipleforms/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Vue from "vue";
import VueFormGenerator from "../../src";
Vue.use(VueFormGenerator);

import App from './app.vue';

new Vue({
...App
}).$mount('#app');
4 changes: 2 additions & 2 deletions src/fields/abstractField.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ export default {
// then slugify it.
if (typeof schema.id !== "undefined") {
// If an ID's been explicitly set, use it unchanged
return schema.id;
return schema.idPrefix + schema.id;
} else {
// Return the slugified version of either:
return (schema.inputName || schema.label || schema.model)
return schema.idPrefix + (schema.inputName || schema.label || schema.model)
// NB: This is a very simple, conservative, slugify function,
// avoiding extra dependencies.
.toString()
Expand Down
8 changes: 8 additions & 0 deletions src/formGenerator.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template lang="pug">
div
fieldset.vue-form-generator(v-if='schema != null', :is='tag')
legend(v-if='schema.legend') {{ schema.legend }}
template(v-for='field in fields')
.form-group(v-if='fieldVisible(field)', :class='getFieldRowClasses(field)')
label(v-if="fieldTypeHasLabel(field)", :for="getFieldID(field)")
Expand Down Expand Up @@ -122,6 +123,13 @@ div
}
},
beforeMount() {
// Add idPrefix to fields if fieldIdPrefix is set
for (let field of this.schema.fields) {
field.idPrefix = this.options.fieldIdPrefix || "";
}
},
mounted() {
this.$nextTick(() => {
if (this.model) {
Expand Down
17 changes: 9 additions & 8 deletions webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ var loaders = [
test: /\.json$/,
loader: 'json'
},
{
test: /\.(woff2?|svg)$/,
loader: "url"
//loader: "url?limit=10000"
{
test: /\.(woff2?|svg)$/,
loader: "url"
//loader: "url?limit=10000"
},
{
test: /\.(ttf|eot)$/,
loader: "url"
{
test: /\.(ttf|eot)$/,
loader: "url"
}
];

module.exports = {
devtool: "source-map",

entry: {
full: path.resolve("dev", "full", "main.js"),
mselect: path.resolve("dev", "multiselect", "main.js"),
mforms: path.resolve("dev", "multipleforms", "main.js"),
checklist: path.resolve("dev", "checklist", "main.js")
},

Expand Down

0 comments on commit a6165c8

Please sign in to comment.