Skip to content

Commit

Permalink
Add base JSON field (#130)
Browse files Browse the repository at this point in the history
* feat(json): add json field

* json field improvements (#131)

* fix: adjustments in json field

* fix: adjust test

---------

Co-authored-by: Marc Güell Segarra <[email protected]>
  • Loading branch information
ecarreras and mguellsegarra authored Oct 29, 2024
1 parent c329f0f commit 45b01fa
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/JSONField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import CodeEditor from "./CodeEditor";

class JSONField extends CodeEditor {
_lang = "json";
}

export default JSONField;
9 changes: 8 additions & 1 deletion src/WidgetFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Avatar from "./Avatar";
import Time from "./Time";
import Alert from "./Alert";
import Comments from "./Comments";
import JSONField from "./JSONField";

class WidgetFactory {
/**
Expand Down Expand Up @@ -172,13 +173,19 @@ class WidgetFactory {
case "comments_timeline":
this._widgetClass = Comments;
break;

case "json":
this._widgetClass = JSONField;
break;
case "arrow_steps":
this._widgetClass = JSONField;
break;
default:
break;
}
}

createWidget(type: string, props: any) {
this._widgetClass = undefined;
let finalType = type;

this.setWidgetClass(type);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import Time from "./Time";
import HTMLPreview from "./HTMLPreview";
import Alert from "./Alert";
import Comments from "./Comments";
import JSONField from "./JSONField";

import {
Graph,
Expand Down Expand Up @@ -136,4 +137,5 @@ export {
YAxisOpts,
MinMaxValues,
Comments,
JSONField,
};
30 changes: 30 additions & 0 deletions src/spec/Form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5975,4 +5975,34 @@ describe("A Form", () => {
const field_char = form.findById("field_char");
expect(field_char!.domain!).toBe("[('value', '=', 'form')]");
});
it("a field with no supported type should fallback to field generic widget", () => {
const fields = {
field_char: {
digits: [16, 2],
is_function: true,
readonly: 1,
string: "Etapa",
type: "json",
views: {},
widget: "arrow_steps",
},
};

const xmlViewForm = `<?xml version="1.0"?>
<form string="Form1">
<field name="field_char" widget="arrow_steps" colspan="4" nolabel="1"/>
</form>`;

const form = new Form(fields);
form.parse(xmlViewForm, {
values: {
field_char: "test",
},
});

const field_char = form.findById("field_char") as Field;
expect(field_char).toBeDefined();
expect(field_char?.type).toBe("arrow_steps");
expect(field_char?.id).toBe("field_char");
});
});
22 changes: 22 additions & 0 deletions src/spec/JSONField.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import WidgetFactory from "../WidgetFactory";
import JSONField from "../JSONField";
import { it, expect, describe } from "vitest";

describe("A Json field", () => {
it("should be created by the widget factory", () => {
const widgetFactory = new WidgetFactory();
const props = {
name: "json_field",
};
const widget = widgetFactory.createWidget("json", props);
expect(widget).toBeInstanceOf(JSONField);
});
it("should have lang set to json as default", () => {
const widgetFactory = new WidgetFactory();
const props = {
name: "json_field",
};
const widget = widgetFactory.createWidget("json", props);
expect(widget.lang).toBe("json");
});
});
11 changes: 10 additions & 1 deletion src/spec/WidgetFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Time,
HTMLPreview,
Comments,
JSONField,
} from "..";

describe("A WidgetFactory", () => {
Expand Down Expand Up @@ -120,11 +121,19 @@ describe("A WidgetFactory", () => {
expect(widget).toBeInstanceOf(HTMLPreview);
expect(widget.type).toBe("html_preview");
});
it("should be albe to Comments type", () => {
it("should be able to Comments type", () => {
const widgetFactory = new WidgetFactory();
const props = {};
const widget = widgetFactory.createWidget("comments_timeline", props);
expect(widget).toBeInstanceOf(Comments);
expect(widget.type).toBe("comments_timeline");
});
// Add a test for JSON field type
it("should be able to retrieve JSON type", () => {
const widgetFactory = new WidgetFactory();
const props = {};
const widget = widgetFactory.createWidget("json", props);
expect(widget).toBeInstanceOf(JSONField);
expect(widget.type).toBe("json");
});
});

0 comments on commit 45b01fa

Please sign in to comment.