diff options
| author | bndw <ben@bdw.to> | 2023-04-26 21:53:13 -0700 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2023-04-26 21:53:13 -0700 |
| commit | 2b47b0e47cd234ceb2b226e18bc10053425e21f9 (patch) | |
| tree | 40983c8575cdab99c2afb3e3dc342da9dc66744c | |
| parent | 8686078ae801fdc15df5a40ee158a43373d37c1c (diff) | |
fix: usd input
| -rw-r--r-- | app/components/calculator.module.css | 9 | ||||
| -rw-r--r-- | app/components/calculator.tsx | 40 |
2 files changed, 25 insertions, 24 deletions
diff --git a/app/components/calculator.module.css b/app/components/calculator.module.css index 265760d..5e0e996 100644 --- a/app/components/calculator.module.css +++ b/app/components/calculator.module.css | |||
| @@ -1,6 +1,3 @@ | |||
| 1 | .form { | ||
| 2 | } | ||
| 3 | |||
| 4 | .input { | 1 | .input { |
| 5 | border-bottom: solid 1px #eee; | 2 | border-bottom: solid 1px #eee; |
| 6 | box-sizing: border-box; | 3 | box-sizing: border-box; |
| @@ -13,3 +10,9 @@ | |||
| 13 | .input:focus { | 10 | .input:focus { |
| 14 | outline: none; | 11 | outline: none; |
| 15 | } | 12 | } |
| 13 | |||
| 14 | .usd { | ||
| 15 | font-size: 3em; | ||
| 16 | margin: 8px 0; | ||
| 17 | padding-left: 12px; | ||
| 18 | } | ||
diff --git a/app/components/calculator.tsx b/app/components/calculator.tsx index 006a755..532781d 100644 --- a/app/components/calculator.tsx +++ b/app/components/calculator.tsx | |||
| @@ -9,15 +9,6 @@ export const Calculator = () => { | |||
| 9 | const [btc, setBtc] = useState(""); | 9 | const [btc, setBtc] = useState(""); |
| 10 | const [usd, setUsd] = useState(""); | 10 | const [usd, setUsd] = useState(""); |
| 11 | 11 | ||
| 12 | const formatCurrency = (val: string) => { | ||
| 13 | const formatter = new Intl.NumberFormat("en-US", { | ||
| 14 | style: "currency", | ||
| 15 | currency: "USD", | ||
| 16 | maximumFractionDigits: 2, | ||
| 17 | }); | ||
| 18 | return formatter.format(parseNumber(val)); | ||
| 19 | }; | ||
| 20 | |||
| 21 | const formatDecimal = (val: any) => { | 12 | const formatDecimal = (val: any) => { |
| 22 | return val.toLocaleString("fullwide", { | 13 | return val.toLocaleString("fullwide", { |
| 23 | useGrouping: true, | 14 | useGrouping: true, |
| @@ -46,7 +37,7 @@ export const Calculator = () => { | |||
| 46 | 37 | ||
| 47 | newbtc = val / 100000000; | 38 | newbtc = val / 100000000; |
| 48 | setBtc(formatDecimal(newbtc)); | 39 | setBtc(formatDecimal(newbtc)); |
| 49 | setUsd(formatCurrency(formatDecimal(newbtc * btcPrice))); | 40 | setUsd(formatDecimal(newbtc * btcPrice)); |
| 50 | break; | 41 | break; |
| 51 | case "btc": | 42 | case "btc": |
| 52 | setBtc(v); | 43 | setBtc(v); |
| @@ -57,14 +48,17 @@ export const Calculator = () => { | |||
| 57 | setBtc(formatDecimal(val)); | 48 | setBtc(formatDecimal(val)); |
| 58 | 49 | ||
| 59 | setSats(formatDecimal(val * 100000000)); | 50 | setSats(formatDecimal(val * 100000000)); |
| 60 | setUsd(formatCurrency(formatDecimal(val * btcPrice))); | 51 | setUsd(formatDecimal(val * btcPrice)); |
| 61 | break; | 52 | break; |
| 62 | case "usd": | 53 | case "usd": |
| 63 | setUsd(formatCurrency(v)); | 54 | setUsd(v); |
| 64 | if (isNaN(val)) { | 55 | |
| 56 | if (isNaN(val) || v.endsWith(".")) { | ||
| 65 | return; | 57 | return; |
| 66 | } | 58 | } |
| 67 | 59 | ||
| 60 | setUsd(formatDecimal(val)); | ||
| 61 | |||
| 68 | newbtc = val / btcPrice; | 62 | newbtc = val / btcPrice; |
| 69 | setBtc(formatDecimal(newbtc)); | 63 | setBtc(formatDecimal(newbtc)); |
| 70 | setSats(formatDecimal(newbtc * 100000000)); | 64 | setSats(formatDecimal(newbtc * 100000000)); |
| @@ -100,14 +94,18 @@ export const Calculator = () => { | |||
| 100 | /> | 94 | /> |
| 101 | 95 | ||
| 102 | <label htmlFor="usd">USD</label> | 96 | <label htmlFor="usd">USD</label> |
| 103 | <input | 97 | <div className="flex"> |
| 104 | className={styles.input} | 98 | <span className={styles.usd}>$</span> |
| 105 | onChange={(e) => handleUpdate("usd", e.target.value)} | 99 | <input |
| 106 | value={usd} | 100 | className={styles.input} |
| 107 | type="text" | 101 | style={{ padding: "0" }} |
| 108 | id="usd" | 102 | onChange={(e) => handleUpdate("usd", e.target.value)} |
| 109 | name="usd" | 103 | value={usd} |
| 110 | /> | 104 | type="text" |
| 105 | id="usd" | ||
| 106 | name="usd" | ||
| 107 | /> | ||
| 108 | </div> | ||
| 111 | </form> | 109 | </form> |
| 112 | ); | 110 | ); |
| 113 | }; | 111 | }; |
