mirror of
https://github.com/we-promise/sure.git
synced 2026-04-08 23:04:49 +00:00
Number.parseFloat() only recognizes dots as decimal separators, causing inputs like "256,54" (French locale) to be parsed as "256". Added a parseLocaleFloat utility that detects whether comma or dot is the decimal separator and normalizes accordingly before parsing. Fixes #1138 https://claude.ai/code/session_01ThfszjiCmbDDPyb4TZqk2X
35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
import parseLocaleFloat from "utils/parse_locale_float"
|
|
|
|
// Handles the inline cost basis editor in the holding drawer.
|
|
// Shows/hides the form and handles bidirectional total <-> per-share conversion.
|
|
export default class extends Controller {
|
|
static targets = ["form", "total", "perShare", "perShareValue"]
|
|
static values = { qty: Number }
|
|
|
|
toggle(event) {
|
|
event.preventDefault()
|
|
this.formTarget.classList.toggle("hidden")
|
|
}
|
|
|
|
// Called when user types in total cost basis field
|
|
updatePerShare() {
|
|
const total = parseLocaleFloat(this.totalTarget.value)
|
|
const qty = this.qtyValue || 1
|
|
const perShare = qty > 0 ? (total / qty).toFixed(2) : "0.00"
|
|
this.perShareValueTarget.textContent = perShare
|
|
if (this.hasPerShareTarget) {
|
|
this.perShareTarget.value = perShare
|
|
}
|
|
}
|
|
|
|
// Called when user types in per-share field
|
|
updateTotal() {
|
|
const perShare = parseLocaleFloat(this.perShareTarget.value)
|
|
const qty = this.qtyValue || 1
|
|
const total = (perShare * qty).toFixed(2)
|
|
this.totalTarget.value = total
|
|
this.perShareValueTarget.textContent = perShare.toFixed(2)
|
|
}
|
|
}
|