Created
May 20, 2022 02:30
-
-
Save leopolicastro/c54ed340718b438edc820f34898953f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller } from "@hotwired/stimulus"; | |
export default class extends Controller { | |
static values = { | |
maxallowed: Number, | |
}; | |
currencyFormatter(e) { | |
let value = e.target.value; | |
const formatter = new Intl.NumberFormat("en-US", { | |
style: "currency", | |
currency: "USD", | |
minimumFractionDigits: 2, | |
}); | |
// remove all currency formatting | |
value = parseFloat( | |
value | |
.replaceAll(",", "") | |
.replace("$", "") | |
.replace(".00", "") | |
.replace(".", "") | |
); | |
if (value > this.maxallowedValue) { | |
e.target.value = formatter.format(this.maxallowedValue); | |
} else { | |
e.target.value = formatter.format(value / 100); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment