Skip to content

Commit 065d0a7

Browse files
committed
Add support for readonly and featured field functions
1 parent e08dbbb commit 065d0a7

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

src/formGenerator.vue

+24-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ div
106106
let baseClasses = {
107107
error: field.errors && field.errors.length > 0,
108108
disabled: this.fieldDisabled(field),
109-
readonly: field.readonly,
110-
featured: field.featured,
109+
readonly: this.fieldReadonly(field),
110+
featured: this.fieldFeatured(field),
111111
required: this.fieldRequired(field)
112112
};
113113
@@ -161,6 +161,28 @@ div
161161
return field.visible;
162162
},
163163
164+
// Get readonly prop of field
165+
fieldReadonly(field) {
166+
if (isFunction(field.readonly))
167+
return field.readonly(this.model);
168+
169+
if (isNil(field.readonly))
170+
return false;
171+
172+
return field.readonly;
173+
},
174+
175+
// Get featured prop of field
176+
fieldFeatured(field) {
177+
if (isFunction(field.featured))
178+
return field.featured(this.model);
179+
180+
if (isNil(field.featured))
181+
return false;
182+
183+
return field.featured;
184+
},
185+
164186
// Validating the model properties
165187
validate() {
166188
this.clearValidationErrors();

test/unit/specs/VueFormGenerator.spec.js

+70
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,76 @@ describe("VueFormGenerator.vue", () => {
306306

307307
});
308308

309+
describe("check fieldReadonly with function", () => {
310+
let schema = {
311+
fields: [
312+
{
313+
type: "text",
314+
label: "Name",
315+
model: "name",
316+
readonly(model) { return model.status; }
317+
}
318+
]
319+
};
320+
321+
let model = {
322+
name: "John Doe",
323+
status: true
324+
};
325+
326+
before( () => {
327+
createFormGenerator(schema, model);
328+
});
329+
330+
it("should be readonly", () => {
331+
expect(el.querySelector(".form-group").classList.contains("readonly")).to.be.true;
332+
});
333+
334+
it("should be writable", (done) => {
335+
model.status = false;
336+
vm.$nextTick(() => {
337+
expect(el.querySelector(".form-group").classList.contains("readonly")).to.be.false;
338+
done();
339+
});
340+
});
341+
342+
});
343+
344+
describe("check fieldFeatured with function", () => {
345+
let schema = {
346+
fields: [
347+
{
348+
type: "text",
349+
label: "Name",
350+
model: "name",
351+
featured(model) { return model.status; }
352+
}
353+
]
354+
};
355+
356+
let model = {
357+
name: "John Doe",
358+
status: true
359+
};
360+
361+
before( () => {
362+
createFormGenerator(schema, model);
363+
});
364+
365+
it("should be featured", () => {
366+
expect(el.querySelector(".form-group").classList.contains("featured")).to.be.true;
367+
});
368+
369+
it("should not be featured", (done) => {
370+
model.status = false;
371+
vm.$nextTick(() => {
372+
expect(el.querySelector(".form-group").classList.contains("featured")).to.be.false;
373+
done();
374+
});
375+
});
376+
377+
});
378+
309379
describe("check fieldRequired with function", () => {
310380
let schema = {
311381
fields: [

0 commit comments

Comments
 (0)