Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Exponential Notation in NumberField #6868

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,24 @@ test.describe("NumberField", () => {
await expect(control).toHaveValue("");
});

test("should allow only one e entry", async ({ page }) => {
const value = "11e111e1e1e1e1e1";

const expectedValue = "11e11111111";

await page.goto(fixtureURL("number-field--number-field"));

const element = page.locator("fast-number-field");

const control = element.locator(".control");

await element.type(value);

await expect(element).toHaveJSProperty("value", expectedValue);

await expect(control).toHaveValue(expectedValue);
});

test('should set the `step` property to "1" by default', async () => {
await root.evaluate(node => {
node.innerHTML = /* html */ `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export class FASTNumberField extends FormAssociatedNumberField {
* @internal
*/
public handleTextInput(): void {
this.field.value = this.field.value.replace(/[^0-9\-+e.]/g, "");
this.control.value = this.control.value.replace(/[^0-9\-+e.]|(?<=e.*)e/g, "");
this.isUserInput = true;
this.value = this.field.value;
}
Expand All @@ -354,6 +354,18 @@ export class FASTNumberField extends FormAssociatedNumberField {
* @internal
*/
public handleChange(): void {
this.value = this.value.toLowerCase();
this.value = this.value.replace(/(?<=e.*)e/g, "");
const parts = this.value.split("e");
if (parts.length > 1) {
let base = parts[0];
let exponent = parts[1];

base = base === "" && exponent !== "" ? "0" : base;
exponent = exponent.replace(/[^0-9\-+]/g, "");
this.value = base + (exponent !== "" ? "e" + exponent : "");
this.control.value = this.value;
}
this.$emit("change");
}

Expand Down