Skip to content

Commit

Permalink
feat(field): add a new property autorefresh
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarreras committed Nov 18, 2024
1 parent 70bd1b6 commit 8ffdd7c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
23 changes: 23 additions & 0 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,26 @@ class Field extends Widget {
return this._fieldType;
}

_autoRefresh?: number = undefined;
get autoRefresh(): number | undefined {
return this._autoRefresh;
}

set autoRefresh(value: number | undefined) {
this._autoRefresh = value;
}

get readOnly(): boolean | undefined {
if (this.autoRefresh) {
return true;
} else {
return super.readOnly;
}
}

constructor(props: any) {
super(props);
console.log("Field props", props);

// Activated by default
this._activated = true;
Expand Down Expand Up @@ -180,6 +198,11 @@ class Field extends Widget {
if (props.help_inline) {
this.tooltipInline = isTrue(props.help_inline);
}
if (props.autorefresh) {
this.autoRefresh = isNaN(+props.autorefresh)
? undefined
: +props.autorefresh;
}
}
}

Expand Down
15 changes: 2 additions & 13 deletions src/Widget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { replaceEntities } from "./helpers/attributeParser";
import { parseBoolAttribute } from "./helpers/nodeParser";

abstract class Widget {
/**
Expand Down Expand Up @@ -124,19 +125,7 @@ abstract class Widget {
this._colspan = +props.colspan;
}
if (props.readonly !== undefined) {
if (
props.readonly === "1" ||
props.readonly === 1 ||
props.readonly === true
) {
this._readOnly = true;
} else if (
props.readonly === "0" ||
props.readonly === 0 ||
props.readonly === false
) {
this._readOnly = false;
}
this._readOnly = parseBoolAttribute(props.readonly);
}
if (props.invisible) {
if (
Expand Down
37 changes: 37 additions & 0 deletions src/spec/Field.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, expect } from "vitest";
import Field from "../Field";

describe("Field", () => {
describe("with the autoRefresh property", () => {
it("should work with integer", () => {
const props = {
autorefresh: 10,
};
const field = new Field(props);
expect(field.autoRefresh).toBe(10);
});
it("should work with text", () => {
const props = {
autorefresh: "10",
};
const field = new Field(props);
expect(field.autoRefresh).toBe(10);
});
describe("if autorefresh is not a valid number", () => {
it("should return undefined", () => {
const props = {
autorefresh: "abc",
};
const field = new Field(props);
expect(field.autoRefresh).toBe(undefined);
});
});
it("should return true for readOnly if autoRefresh is set", () => {
const props = {
autorefresh: 10,
};
const field = new Field(props);
expect(field.readOnly).toBe(true);
});
});
});

0 comments on commit 8ffdd7c

Please sign in to comment.