From 2ff81c169546f2a2cc2ff6a1ef8736e951438d7f Mon Sep 17 00:00:00 2001 From: Claas Date: Thu, 14 Aug 2025 00:26:24 +0200 Subject: [PATCH] Add support for Pounds --- src/App.tsx | 113 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 99 insertions(+), 14 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f39984e..fed6971 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,10 @@ -import { createResource, For, Show } from "solid-js"; +import { + createEffect, + createResource, + createSignal, + For, + Show, +} from "solid-js"; import { openDatabase } from "./data"; export default function App() { @@ -45,6 +51,42 @@ export default function App() { refetch(); } + const [unit, setUnit] = createSignal<"kg" | "lb">("kg"); + // function handleChange(even: Event & { currentTarget: HTMLInputElement }) { + // const value = even.currentTarget.value; + // if (value !== "kg" && value !== "lb") throw new Error("Expected kg or lb"); + + // setUnit(value); + // } + + function format(weight: number, unit: "kg" | "lb") { + if (unit === "kg") return weight; + // If 1lb = 0.45359237kg + // Then divide both sides by 45359237 to get 1/45359237lb and 0.00000001kg + // Multiply both sides by 100_000_000 to get 2.2046226218lbs = 1kg + // 1/45359237*100_000_000 + return weight * 2.2046226218; + } + + let weightInput: HTMLInputElement | undefined; + // const value = () => { + // const currentUnit = unit(); + // if (!weightInput) return; + + // return format(weightInput.valueAsNumber, currentUnit); + // }; + + createEffect(() => { + // weightInput?.value + const currentUnit = unit(); + if (!weightInput?.valueAsNumber) return; + console.debug("Updating weight input", weightInput); + weightInput.valueAsNumber = + currentUnit === "kg" + ? weightInput.valueAsNumber / 2.2046226218 + : weightInput.valueAsNumber * 2.2046226218; + }); + return (

Waight

@@ -53,7 +95,15 @@ export default function App() { {(entry) => (
  • - {entry.weight} ㎏ + + {format(entry.weight, unit()).toLocaleString(undefined, { + maximumFractionDigits: 2, + style: unit() === "kg" ? "unit" : "currency", + currency: unit() === "lb" ? "gbp" : undefined, + unit: unit() === "kg" ? "kilogram" : undefined, + })} + + {/* {unit() === "kg" ? "㎏" : "£"} */}