Etemplate: Fix readonly was still giving a value

This commit is contained in:
nathan 2021-12-10 13:24:06 -07:00
parent a82c080ca5
commit ea2004173f
6 changed files with 46 additions and 21 deletions

View File

@ -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)

View File

@ -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 = [

View File

@ -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;
}

View File

@ -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() =>
{
(<Et2InputWidgetInterface>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><unknown>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);

View File

@ -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

View File

@ -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<Et2Textbox>(html`
<et2-textbox></et2-textbox>
`);
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<Et2Textbox>(html`
<et2-textbox></et2-textbox>
`);
});
beforeEach(before);
it('is defined', () =>
{
@ -29,4 +34,5 @@ describe("Textbox widget", () =>
element.set_label("Yay label");
assert.isEmpty(element.shadowRoot.querySelectorAll('.et2_label'));
})
});
});
inputBasicTests(before, "I'm a good test value", "input");