diff --git a/src/Field.ts b/src/Field.ts index 99ba206..0cc9ff0 100644 --- a/src/Field.ts +++ b/src/Field.ts @@ -1,5 +1,6 @@ import Widget from "./Widget"; import { replaceEntities, isTrue } from "./helpers/attributeParser"; +import { parseBoolAttribute } from "./helpers/nodeParser"; class Field extends Widget { /** @@ -125,12 +126,12 @@ class Field extends Widget { return this._fieldType; } - _autoRefresh?: number = undefined; - get autoRefresh(): number | undefined { - return this._autoRefresh; + _autoRefresh?: boolean = false; + get autoRefresh(): boolean { + return this._autoRefresh ?? false; } - set autoRefresh(value: number | undefined) { + set autoRefresh(value: boolean) { this._autoRefresh = value; } @@ -202,9 +203,7 @@ class Field extends Widget { this.tooltipInline = isTrue(props.help_inline); } if (props.autorefresh) { - this.autoRefresh = isNaN(+props.autorefresh) - ? undefined - : +props.autorefresh; + this.autoRefresh = parseBoolAttribute(props.autorefresh); } } } diff --git a/src/spec/Field.spec.ts b/src/spec/Field.spec.ts index 9eb2071..35b2df1 100644 --- a/src/spec/Field.spec.ts +++ b/src/spec/Field.spec.ts @@ -3,32 +3,30 @@ import Field from "../Field"; describe("Field", () => { describe("with the autoRefresh property", () => { - it("should work with integer", () => { - const props = { - autorefresh: 10, - }; + it("should be false as default", () => { + const props = {}; const field = new Field(props); - expect(field.autoRefresh).toBe(10); + expect(field.autoRefresh).toBe(false); }); it("should work with text", () => { const props = { - autorefresh: "10", + autorefresh: "1", }; const field = new Field(props); - expect(field.autoRefresh).toBe(10); + expect(field.autoRefresh).toBe(true); }); - describe("if autorefresh is not a valid number", () => { - it("should return undefined", () => { + describe("if autorefresh is not a valid boold", () => { + it("should return false", () => { const props = { autorefresh: "abc", }; const field = new Field(props); - expect(field.autoRefresh).toBe(undefined); + expect(field.autoRefresh).toBe(false); }); }); it("should return true for readOnly if autoRefresh is set", () => { const props = { - autorefresh: 10, + autorefresh: true, }; const field = new Field(props); expect(field.readOnly).toBe(true);