Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/master' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Jan 30, 2017
2 parents 454fa0e + d5d4508 commit 3a1c267
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -1,5 +1,5 @@
# vue-form-generator [![NPM version](https://img.shields.io/npm/v/vue-form-generator.svg)](https://www.npmjs.com/package/vue-form-generator)
A schema-based form generator component for Vue.js v1.x.x
A schema-based form generator component for Vue.js.

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/912039aa815e40de8315032519aa7e6c)](https://www.codacy.com/app/mereg-norbert/vue-form-generator?utm_source=github.com&utm_medium=referral&utm_content=icebob/vue-form-generator&utm_campaign=Badge_Grade)
[![Build Status](https://travis-ci.org/icebob/vue-form-generator.svg?branch=master)](https://travis-ci.org/icebob/vue-form-generator)
Expand All @@ -11,7 +11,7 @@ A schema-based form generator component for Vue.js v1.x.x
[![devDependency Status](https://david-dm.org/icebob/vue-form-generator/dev-status.svg)](https://david-dm.org/icebob/vue-form-generator#info=devDependencies)
[![Downloads](https://img.shields.io/npm/dt/vue-form-generator.svg)](https://www.npmjs.com/package/vue-form-generator)

**Vue v2.x support is under development!**
[**Vue v2.x pre-release is available!**](https://github.com/icebob/vue-form-generator/releases)

## Demo
[JSFiddle simple example](https://jsfiddle.net/icebob/0mg1v81e/)
Expand Down
39 changes: 36 additions & 3 deletions src/formGenerator.vue
Expand Up @@ -118,9 +118,9 @@ div
let baseClasses = {
error: field.errors && field.errors.length > 0,
disabled: this.fieldDisabled(field),
readonly: field.readonly,
featured: field.featured,
required: field.required
readonly: this.fieldReadonly(field),
featured: this.fieldFeatured(field),
required: this.fieldRequired(field)
};
if (isArray(field.styleClasses)) {
Expand Down Expand Up @@ -151,6 +151,17 @@ div
return field.disabled;
},
// Get required prop of field
fieldRequired(field) {
if (isFunction(field.required))
return field.required(this.model);
if (isNil(field.required))
return false;
return field.required;
},
// Get visible prop of field
fieldVisible(field) {
if (isFunction(field.visible))
Expand All @@ -162,6 +173,28 @@ div
return field.visible;
},
// Get readonly prop of field
fieldReadonly(field) {
if (isFunction(field.readonly))
return field.readonly(this.model);
if (isNil(field.readonly))
return false;
return field.readonly;
},
// Get featured prop of field
fieldFeatured(field) {
if (isFunction(field.featured))
return field.featured(this.model);
if (isNil(field.featured))
return false;
return field.featured;
},
// Validating the model properties
validate() {
// console.log("Validate!", this.model);
Expand Down
105 changes: 105 additions & 0 deletions test/unit/specs/VueFormGenerator.spec.js
Expand Up @@ -311,6 +311,111 @@ describe("VueFormGenerator.vue", () => {

});

describe("check fieldReadonly with function", () => {
let schema = {
fields: [
{
type: "text",
label: "Name",
model: "name",
readonly(model) { return model.status; }
}
]
};

let model = {
name: "John Doe",
status: true
};

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

it("should be readonly", () => {
expect(el.querySelector(".form-group").classList.contains("readonly")).to.be.true;
});

it("should be writable", (done) => {
model.status = false;
vm.$nextTick(() => {
expect(el.querySelector(".form-group").classList.contains("readonly")).to.be.false;
done();
});
});

});

describe("check fieldFeatured with function", () => {
let schema = {
fields: [
{
type: "text",
label: "Name",
model: "name",
featured(model) { return model.status; }
}
]
};

let model = {
name: "John Doe",
status: true
};

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

it("should be featured", () => {
expect(el.querySelector(".form-group").classList.contains("featured")).to.be.true;
});

it("should not be featured", (done) => {
model.status = false;
vm.$nextTick(() => {
expect(el.querySelector(".form-group").classList.contains("featured")).to.be.false;
done();
});
});

});

describe("check fieldRequired with function", () => {
let schema = {
fields: [
{
type: "text",
label: "Name",
model: "name",
required(model) { return model.status; }
}
]
};

let model = {
name: "John Doe",
status: true
};

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

it("should be required", () => {
expect(el.querySelector(".form-group").classList.contains("required")).to.be.true;
});

it("should be optional", (done) => {
model.status = false;
vm.$nextTick(() => {
expect(el.querySelector(".form-group").classList.contains("required")).to.be.false;
done();
});
});

});

describe("check fieldVisible with function", () => {
let schema = {
fields: [
Expand Down

0 comments on commit 3a1c267

Please sign in to comment.