Skip to content
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

Add subgroups + custom group tags #670

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions dev/projects/grouping/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export default {
model: {
name: "Brian Blessed",
email: "[email protected]",
city: "Springfield",
state: "IL",
age: 25,
others: {
more: "More",
things: "Things"
Expand All @@ -51,6 +54,41 @@ export default {
}
]
},
{
groups: [
{
legend: "Location",
fields: [
{
type: "input",
inputType: "text",
label: "City",
model: "city",
styleClasses: "half-width"
},
{
type: "input",
inputType: "text",
label: "State",
model: "state",
styleClasses: "half-width"
}
]
},
{
legend: "Demographics",
fields: [
{
type: "input",
inputType: "number",
label: "Age",
model: "age",
styleClasses: "half-width"
}
]
}
]
},
{
legend: "Other Details",
fields: [
Expand Down Expand Up @@ -91,6 +129,16 @@ export default {
};
</script>

<style>
/* To demonstrate subgroups */
section section legend {
color: #777;
border: 0;
margin-bottom: 10px;
font-size: 18px;
}
</style>

<style lang="scss">
@import "../../style.scss";
</style>
2 changes: 1 addition & 1 deletion dev/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pre {
}
}

fieldset.vue-form-generator {
.vue-form-generator {
.form-group.half-width {
width: 50%;
}
Expand Down
11 changes: 9 additions & 2 deletions src/formGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ div.vue-form-generator(v-if='schema != null')
form-group(v-if='fieldVisible(field)', :vfg="vfg", :field="field", :errors="errors", :model="model", :options="options", @validated="onFieldValidated", @model-updated="onModelUpdated")

template(v-for='group in groups')
fieldset(:is='tag', :class='getFieldRowClasses(group)')
fieldset(:is='group.tag || tag', :class='getFieldRowClasses(group)', v-bind="{ [group.legendAttr]: group.legend }")
legend(v-if='group.legend') {{ group.legend }}
template(v-for='field in group.fields')
form-group(v-if='fieldVisible(field)', :vfg="vfg", :field="field", :errors="errors", :model="model", :options="options", @validated="onFieldValidated", @model-updated="onModelUpdated")
template(v-for='subGroup in group.groups')
fieldset(:is='subGroup.tag || tag', :class='getFieldRowClasses(subGroup)', v-bind="{ [subGroup.legendAttr]: subGroup.legend }")
legend(v-if='subGroup.legend') {{ subGroup.legend }}
template(v-for='nestedField in subGroup.fields')
form-group(v-if='fieldVisible(nestedField)', :vfg="vfg", :field="nestedField", :errors="errors", :model="model", :options="options", @validated="onFieldValidated", @model-updated="onModelUpdated")
</template>

<script>
Expand Down Expand Up @@ -201,7 +206,9 @@ export default {
this.errors.splice(0);

forEach(this.$children, child => {
child.clearValidationErrors();
if (child.clearValidationErrors) {
child.clearValidationErrors();
}
});
},
}
Expand Down
82 changes: 82 additions & 0 deletions test/unit/specs/VueFormGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,88 @@ describe("VueFormGenerator.vue", () => {
});
});

describe("check subgroups", () => {
let schema = {
groups: [
{
legend: "Test Legend",
groups: [
{
legend: "Nested Legend",
legendAttr: "title",
fields: [
{
type: "input",
inputType: "text",
label: "Name",
model: "name",
}
]
}
]
},
]
};
let label;
let fieldset;

before(() => {
createFormGenerator({ schema });
label = wrapper.find("fieldset fieldset legend");
fieldset = wrapper.find("fieldset fieldset");
});

it("should render nested legend", () => {
expect(label.exists()).to.be.true;
expect(label.text()).to.be.equal("Nested Legend");
});

it("should render nested legend in specified attribute", () => {
expect(fieldset.attributes().title).to.be.equal("Nested Legend");
});
});

describe("check group tag definition", () => {
let schema = {
groups: [
{
tag: "section",
legend: "Test Legend",
groups: [
{
tag: "aside",
legend: "Nested Legend",
fields: [
{
type: "input",
inputType: "text",
label: "Name",
model: "name",
}
]
}
]
},
]
};

before(() => {
createFormGenerator({ schema });
});

it("should create custom tag", () => {
const section = wrapper.find("section");
expect(section.exists()).to.be.true;
expect(section.is("section")).to.be.true;
});

it("should create custom tag (nested)", () => {
const aside = wrapper.find("section aside");
expect(aside.exists()).to.be.true;
expect(aside.is("aside")).to.be.true;
});
});

describe("check label classes", () => {
let schema = {
fields: [
Expand Down