Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x-mask custom decimal precision #3415

Merged
merged 7 commits into from
Mar 7, 2023
Merged

x-mask custom decimal precision #3415

merged 7 commits into from
Mar 7, 2023

Conversation

Restartz
Copy link
Contributor

Allows for custom decimal precision using x-mask $money magic as requested here: #2873

Example usage:

  • To allow for a decimal precision of 3 digits: <input x-data x-mask:dynamic="$money($input, '.', ',', 3)">
  • To only allow for round numbers: <input x-data x-mask:dynamic="$money($input, '.', ',', 0)">

It defaults to 2, so the current behaviour without specified precision should still be the same:
<input x-data x-mask:dynamic="$money($input)">

@ekwoka
Copy link
Contributor

ekwoka commented Feb 12, 2023

Looks good. Maybe at this point it needs to be made into an options object?

@Restartz
Copy link
Contributor Author

I've changed the $money helper to accept an options object as the second parameter.
So precision can now be set as follows:
$money($input, { precision: 3 })

The delimiter and thousands params can also be passed trough the options object:
$money($input, { delimiter: ',', precision: 0 })

Calls like $money($input, ',') or $money($input, ',', ' ') also still work.

@calebporzio
Copy link
Collaborator

Hey @Restartz, thanks for this. I actually prefer the 4th param instead of the options object. Can you remove all the options object stuff and we'll merge? Thanks!

Oh, and can you also add documentation for this?

Restartz added 2 commits March 6, 2023 21:10
Changed formatMoney to accept an options object as the second parameter
while also allowing backwards compatibility for any formatMoney(input, delimiter = '.', thousands) calls

updated tests to use options object

fixed typos in test

add test based on initial value

fix initial value test
@joshhanley
Copy link
Collaborator

joshhanley commented Mar 6, 2023

@Restartz it looks like there are also some merge conflicts. Can you merge the latest main branch changes in to your branch? Once that and docs sorted, then I can merge 🙂

@Restartz
Copy link
Contributor Author

Restartz commented Mar 6, 2023

@joshhanley Done and done! 🙂

@joshhanley joshhanley merged commit 04ff2c7 into alpinejs:main Mar 7, 2023
@joshhanley
Copy link
Collaborator

@Restartz thanks!

@Restartz Restartz deleted the mask-decimal-precision branch March 7, 2023 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants