Files
sure/app/javascript/controllers/money_field_controller.js
Claude ef4750c2c5 Skip reformatting blank amount fields on currency change
parseLocaleFloat returns 0 for empty strings, which caused blank amount
fields to be overwritten with "0.00" when the user changed currency.
Guard against this by checking for a non-empty value before parsing.

https://claude.ai/code/session_01ThfszjiCmbDDPyb4TZqk2X
2026-03-10 17:15:44 +00:00

31 lines
1020 B
JavaScript

import { Controller } from "@hotwired/stimulus";
import parseLocaleFloat from "utils/parse_locale_float";
import { CurrenciesService } from "services/currencies_service";
// Connects to data-controller="money-field"
// when currency select change, update the input value with the correct placeholder and step
export default class extends Controller {
static targets = ["amount", "currency", "symbol"];
handleCurrencyChange(e) {
const selectedCurrency = e.target.value;
this.updateAmount(selectedCurrency);
}
updateAmount(currency) {
new CurrenciesService().get(currency).then((currency) => {
this.amountTarget.step = currency.step;
const rawValue = this.amountTarget.value.trim();
if (rawValue !== "") {
const parsedAmount = parseLocaleFloat(rawValue);
if (Number.isFinite(parsedAmount)) {
this.amountTarget.value = parsedAmount.toFixed(currency.default_precision);
}
}
this.symbolTarget.innerText = currency.symbol;
});
}
}