From ea2004173fee759a81c0388cd235cabe47fb2172 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 10 Dec 2021 13:24:06 -0700 Subject: [PATCH] Etemplate: Fix readonly was still giving a value --- api/js/etemplate/Et2Date/Et2Date.ts | 8 +++++- api/js/etemplate/Et2Date/test/Et2Date.test.ts | 4 +-- .../Et2InputWidget/Et2InputWidget.ts | 6 ++++- .../Et2InputWidget/test/InputBasicTests.ts | 14 +++++----- api/js/etemplate/Et2Textbox/Et2Textbox.ts | 9 +++++++ .../Et2Textbox/test/Et2Textbox.test.ts | 26 ++++++++++++------- 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/api/js/etemplate/Et2Date/Et2Date.ts b/api/js/etemplate/Et2Date/Et2Date.ts index 171667371e..1c7382869a 100644 --- a/api/js/etemplate/Et2Date/Et2Date.ts +++ b/api/js/etemplate/Et2Date/Et2Date.ts @@ -111,7 +111,7 @@ export function parseTime(timeString) let am_pm = timeString.endsWith("pm") || timeString.endsWith("PM") ? 12 : 0; let strippedString = timeString.replaceAll(/[^0-9:]/gi, ''); - + if(timeString.startsWith("12") && strippedString != timeString) { // 12:xx am -> 0:xx, 12:xx pm -> 12:xx @@ -337,6 +337,12 @@ export class Et2Date extends Et2InputWidget(LionInputDatepicker) { return null; } + + // Empty field, return '' + if(!this.modelValue) + { + return ''; + } // The supplied value was not understandable, return null if(this.modelValue instanceof Unparseable || !this.modelValue) diff --git a/api/js/etemplate/Et2Date/test/Et2Date.test.ts b/api/js/etemplate/Et2Date/test/Et2Date.test.ts index 92bb110c2a..64af9a6a83 100644 --- a/api/js/etemplate/Et2Date/test/Et2Date.test.ts +++ b/api/js/etemplate/Et2Date/test/Et2Date.test.ts @@ -52,13 +52,13 @@ describe("Date widget", () => }) - it("'0' shows no value", async() => + it("'0' shows nothing", async() => { element.set_value("0"); // wait for asychronous changes to the DOM await elementUpdated(element); assert.equal(element.querySelector("input").value, ""); - assert.equal(element.get_value(), null); + assert.equal(element.get_value(), ''); }); const tz_list = [ diff --git a/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts b/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts index 8750ba3bed..24457b6a44 100644 --- a/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts +++ b/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts @@ -97,7 +97,11 @@ const Et2InputWidgetMixin = (superclass) => getValue() { - return typeof this.serializedValue !== "undefined" ? this.serializedValue : this.modalValue; + if(this.readOnly) + { + return null; + } + return this.serializedValue !== "undefined" ? this.serializedValue : this.modalValue; } diff --git a/api/js/etemplate/Et2InputWidget/test/InputBasicTests.ts b/api/js/etemplate/Et2InputWidget/test/InputBasicTests.ts index 58088ff235..bef490c126 100644 --- a/api/js/etemplate/Et2InputWidget/test/InputBasicTests.ts +++ b/api/js/etemplate/Et2InputWidget/test/InputBasicTests.ts @@ -31,14 +31,14 @@ let element : Et2InputWidgetInterface; export function inputBasicTests(before : Function, test_value : string, value_selector : string) { - describe("Readonly tests", () => + describe("Readonly", () => { beforeEach(async() => { element = await before(); }); - it('Does not return a value (via attribute)', async() => + it("does not return a value (via attribute)", async() => { element.readOnly = true; @@ -50,7 +50,7 @@ export function inputBasicTests(before : Function, test_value : string, value_se assert.equal(element.getValue(), null); }); - it('Does not return a value (via method)', async() => + it("does not return a value (via method)", async() => { (element).set_readonly(true); @@ -62,7 +62,7 @@ export function inputBasicTests(before : Function, test_value : string, value_se assert.equal(element.getValue(), null); }); - it('Does not return a value if it goes readonly after having a value', async() => + it("does not return a value if it goes readonly after having a value", async() => { element.set_value(test_value); @@ -81,13 +81,13 @@ export function inputBasicTests(before : Function, test_value : string, value_se { element = await before(); }); - it('No value shows no value', () => + it("no value gives empty string", () => { assert.equal((element).querySelector(value_selector).textContent, ""); - assert.equal(element.get_value(), null); + assert.equal(element.get_value(), ""); }); - it('Value out matches value in', async() => + it("value out matches value in", async() => { element.set_value(test_value); diff --git a/api/js/etemplate/Et2Textbox/Et2Textbox.ts b/api/js/etemplate/Et2Textbox/Et2Textbox.ts index 058a088998..a233a64911 100644 --- a/api/js/etemplate/Et2Textbox/Et2Textbox.ts +++ b/api/js/etemplate/Et2Textbox/Et2Textbox.ts @@ -43,6 +43,15 @@ export class Et2Textbox extends Et2InputWidget(LionInput) { super.connectedCallback(); } + + getValue() + { + if(this.readOnly) + { + return null; + } + return this.value; + } } // @ts-ignore TypeScript is not recognizing that Et2Textbox is a LitElement diff --git a/api/js/etemplate/Et2Textbox/test/Et2Textbox.test.ts b/api/js/etemplate/Et2Textbox/test/Et2Textbox.test.ts index 22cbcd2663..0c2e60568a 100644 --- a/api/js/etemplate/Et2Textbox/test/Et2Textbox.test.ts +++ b/api/js/etemplate/Et2Textbox/test/Et2Textbox.test.ts @@ -4,20 +4,25 @@ import {assert, fixture} from '@open-wc/testing'; import {Et2Textbox} from "../Et2Textbox"; import {html} from "lit-element"; +import {inputBasicTests} from "../../Et2InputWidget/test/InputBasicTests"; + +// Reference to component under test +let element : Et2Textbox; + +async function before() +{ + // Create an element to test with, and wait until it's ready + element = await fixture(html` + + `); + return element; +} describe("Textbox widget", () => { - // Reference to component under test - let element : Et2Textbox; // Setup run before each test - beforeEach(async() => - { - // Create an element to test with, and wait until it's ready - element = await fixture(html` - - `); - }); + beforeEach(before); it('is defined', () => { @@ -29,4 +34,5 @@ describe("Textbox widget", () => element.set_label("Yay label"); assert.isEmpty(element.shadowRoot.querySelectorAll('.et2_label')); }) -}); \ No newline at end of file +}); +inputBasicTests(before, "I'm a good test value", "input"); \ No newline at end of file