mirror of
https://github.com/we-promise/sure.git
synced 2026-04-09 07:14:47 +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
28 lines
935 B
JavaScript
28 lines
935 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 parsedAmount = parseLocaleFloat(this.amountTarget.value);
|
|
if (Number.isFinite(parsedAmount)) {
|
|
this.amountTarget.value = parsedAmount.toFixed(currency.default_precision);
|
|
}
|
|
|
|
this.symbolTarget.innerText = currency.symbol;
|
|
});
|
|
}
|
|
}
|