Skip to content

Commit

Permalink
feat: clean up number input detals
Browse files Browse the repository at this point in the history
  • Loading branch information
airjp73 committed Jun 18, 2024
1 parent 931b6b9 commit c0db431
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
29 changes: 12 additions & 17 deletions packages/core/src/dom/dom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { FormScope, FormStore } from "../form";
import { FormStore } from "../form";
import { getFieldValue } from "../getters";
import { MultiValueMap } from "../native-form-data/MultiValueMap";
import * as R from "remeda";
import { FieldErrors } from "../types";
import { getNextCheckboxValue } from "./getCheckboxChecked";
import { RefStore } from "../store";

export const setFormControlValue = (element: HTMLElement, value: unknown) => {
if (element instanceof HTMLInputElement) {
Expand All @@ -18,7 +14,7 @@ export const setFormControlValue = (element: HTMLElement, value: unknown) => {
element.checked = value === element.value;
break;
case "number":
if (value === undefined) element.value = "";
if (value == null || value === "") element.value = "";
else element.valueAsNumber = Number(value);
break;
case "file":
Expand All @@ -36,14 +32,19 @@ export const setFormControlValue = (element: HTMLElement, value: unknown) => {

export const getFormControlValue = (element: HTMLElement) => {
if (element instanceof HTMLInputElement) {
switch (element.type) {
const input = element as HTMLInputElement;
switch (input.type) {
case "checkbox":
case "radio":
return element.checked;
return input.checked;
case "number":
return element.valueAsNumber;
if (input.value === "") return null;
return input.valueAsNumber;
case "file":
if (input.multiple) return Array.from(input.files ?? []);
return input.files?.[0] ?? null;
default:
return element.value;
return input.value;
}
}

Expand Down Expand Up @@ -171,17 +172,11 @@ export const getNextNativeValue = ({
}

if (element.type === "number") {
if (derivedValue == null || derivedValue === "") return null;
if (typeof currentValue === "string") return String(derivedValue);
return Number(derivedValue);
}

if (element.type === "file") {
const input = element as HTMLInputElement;
const files = Array.from(input.files ?? []);
if (input.multiple) return files;
return files[0];
}

return derivedValue;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/test/input-types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe("number inputs", () => {

// Clearing the input
await userEvent.clear(screen.getByTestId("age"));
expect(screen.getByTestId("age")).toHaveValue("");
expect(screen.getByTestId("age-value").textContent).toEqual("");
expect(screen.getByTestId("age")).toHaveValue(null);
expect(screen.getByTestId("age-value").textContent).toEqual("null");
});

it("default values set as strings", async () => {
Expand Down

0 comments on commit c0db431

Please sign in to comment.