Skip to content

Commit

Permalink
fix: actually fix getElementValue for select this time
Browse files Browse the repository at this point in the history
  • Loading branch information
mikerourke committed Jan 20, 2025
1 parent 1ee41c1 commit 3ecb43d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/elements/__tests__/getElementValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,57 @@ describe("the getElementValue function", () => {
expect(result).toBe("22");
});

it("returns a numeric value for a select input", () => {
// biome-ignore format:
const element = render(`
<select>
<option value="1" selected>1</option>
<option value="2">2</option>
</select>
`);

const result = getElementValue(element);

expect(result).toBe(1);
});

it("returns a string value for a select input", () => {
// biome-ignore format:
const element = render(`
<select>
<option value="a" selected>A</option>
<option value="b">B</option>
</select>
`);

const result = getElementValue(element);

expect(result).toBe("a");
});

it("returns a boolean value for a select input", () => {
// biome-ignore format:
const element = render(`
<select>
<option value="true" selected>True</option>
<option value="false">False</option>
</select>
`);

const result = getElementValue(element);

expect(result).toBe(true);
});

it("returns the first option for a select input with nothing selected", () => {
// biome-ignore format:
const element = render(`<select><option value="a">A</option><option value="b">B</option></select>`);

const result = getElementValue(element);

expect(result).toBe("a");
});

it("returns a Date for a date input", () => {
const element = render(`<input type="date" value="2024-01-01" />`);

Expand Down
11 changes: 11 additions & 0 deletions src/elements/getElementValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { cast, isNotNil } from "@laserware/arcade";
import { parseDOMValue } from "../internal/domValues.ts";
import { isElementType } from "./isElementType.ts";

import { toElementOrThrow } from "./toElement.ts";
import type { Target } from "./types.ts";
Expand All @@ -10,6 +12,8 @@ import type { Target } from "./types.ts";
* is a valid [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) (not `null`),
* a boolean if the input is a `checkbox` or `radio` type, otherwise is returned as a `string`.
*
* If the element is a `select` element, it returns an empty string
*
* @template T Type of the value that gets returned.
*
* @param target Element, EventTarget, or CSS selector.
Expand All @@ -22,6 +26,13 @@ export function getElementValue<T>(target: Target | null): T {
// biome-ignore format:
const element = toElementOrThrow<"input">(target, "Cannot get value for element");

if (isElementType(element, "select")) {
const value =
element.value === "" ? element.value : parseDOMValue(element.value);

return cast<T>(value);
}

// Note that the order of these checks is important. Calling `valueAsNumber` on
// date input will return the Unix epoch, which is _not_ what we want to return,
// so we do the date check first:
Expand Down

0 comments on commit 3ecb43d

Please sign in to comment.