Skip to content

Add support for readonly and featured field functions #107

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

Merged
Merged
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
26 changes: 24 additions & 2 deletions src/formGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ div
let baseClasses = {
error: field.errors && field.errors.length > 0,
disabled: this.fieldDisabled(field),
readonly: field.readonly,
featured: field.featured,
readonly: this.fieldReadonly(field),
featured: this.fieldFeatured(field),
required: this.fieldRequired(field)
};

Expand Down Expand Up @@ -161,6 +161,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() {
this.clearValidationErrors();
Expand Down
70 changes: 70 additions & 0 deletions test/unit/specs/VueFormGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,76 @@ 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: [
Expand Down