-
Notifications
You must be signed in to change notification settings - Fork 64
Use masked input for Backpex.Fields.Currency
#1307
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
Open
Flo0807
wants to merge
27
commits into
develop
Choose a base branch
from
feature/1280-improve-currency-field
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+6,822
−198
Open
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
530097f
Replace custom amount type with money amount type
Flo0807 ffa9a6a
Add masked input
Flo0807 c52444d
Refactor currency field to use masked input
Flo0807 502803b
Remove money dependency from backpex
Flo0807 f9e4374
Install money in demo
Flo0807 c331680
Use hidden input for currency value
Flo0807 2c8a149
Update masked input
Flo0807 49e319b
Update js build
Flo0807 1e6e4ef
Rename masked_input to masked_number_input
Flo0807 3d03c2d
Remove debug message
Flo0807 90f039d
Remove binding
Flo0807 5b80804
Update default for `unit_position`
Flo0807 4a56ecf
Refactor number input to currency input
Flo0807 ba1bf96
Update lib/backpex/html/form.ex
Flo0807 1c81961
Update dependency igniter to v0.6.28 (#1428)
renovate[bot] 48ac227
Update assets
Flo0807 caefd4b
Install backpex node dependencies
Flo0807 fe1b3b1
Merge branch 'develop' into feature/1280-improve-currency-field
Flo0807 5ca315e
Run `mix format`
Flo0807 b11c8d8
Merge branch 'develop' into feature/1280-improve-currency-field
Flo0807 1b5c979
Fix imask cannot be resolved
Flo0807 07cc077
Use USD as currency default
Flo0807 7e81909
Remove unused NODE_PATH env
Flo0807 79c2ad0
Install backpex deps
Flo0807 f5f2118
Add v0.16 upgrade guide
Flo0807 fbd5d60
Fix spelling mistake
Flo0807 e2b3487
Merge branch 'develop' into feature/1280-improve-currency-field
Flo0807 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import IMask from 'imask' | ||
|
||
export default { | ||
mounted () { | ||
this.maskedInput = this.el.querySelector('[data-masked-input]') | ||
this.hiddenInput = this.el.querySelector('[data-hidden-input]') | ||
|
||
this.initializeMask() | ||
}, | ||
initializeMask () { | ||
const maskPattern = this.el.dataset.maskPattern | ||
|
||
if (!maskPattern) { | ||
console.error('You must provide a mask pattern in the data-masked-pattern attribute.') | ||
return | ||
} | ||
|
||
this.maskOptions = { | ||
mask: maskPattern, | ||
lazy: false, | ||
blocks: { | ||
num: { | ||
mask: Number, | ||
thousandsSeparator: this.el.dataset.thousandsSeparator, | ||
radix: this.el.dataset.radix | ||
} | ||
} | ||
} | ||
|
||
this.mask = IMask(this.maskedInput, this.maskOptions) | ||
this.mask.unmaskedValue = this.rawValue(this.hiddenInput.value) | ||
this.mask.on('accept', this.handleMaskChange.bind(this)) | ||
}, | ||
updated () { | ||
this.handleMaskChange() | ||
}, | ||
handleMaskChange () { | ||
this.hiddenInput.value = this.rawValue(this.mask.value) | ||
}, | ||
destroyed () { | ||
this.mask.destroy() | ||
}, | ||
rawValue (value) { | ||
return value | ||
.replace(this.el.dataset.unit || '', '') | ||
.trim() | ||
.replace(new RegExp(`\\${this.el.dataset.thousandsSeparator}`, 'g'), '') | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Upgrading to v0.16 | ||
|
||
## Bump Your Deps | ||
|
||
Update Backpex to the latest version: | ||
|
||
```elixir | ||
defp deps do | ||
[ | ||
{:backpex, "~> 0.16.0"} | ||
] | ||
end | ||
``` | ||
|
||
## `Backpex.Fields.Currency` has been updated | ||
|
||
The appearance of the currency field has changed completely. We now use a custom masked input instead of a number input which improves UX a lot. | ||
In addition we've removed the `money` dependency from Backpex so you can use whatever library you want to use for currencies in your app. | ||
The removal of `money` includes our custom [`Backpex.Ecto.AmountType`]() ecto type. | ||
|
||
If you used the `money` library before, these are the changes you have to make: | ||
|
||
1. Configure `unit`, `unit_position`, `radix` and `thousands_separator` for `Backpex.Fields.Currency` | ||
|
||
```elixir | ||
def fields do | ||
[ | ||
price: %{ | ||
module: Backpex.Fields.Currency, | ||
label: "Price", | ||
unit: "€", | ||
unit_position: :after, | ||
radix: ",", | ||
thousands_separator: "." | ||
}, | ||
... | ||
] | ||
end | ||
``` | ||
|
||
See [field-specific options](`Backpex.Fields.Currency`) for default values. | ||
|
||
2. Configure defaults for `money` in your `config/config.exs` | ||
|
||
```elixir | ||
config :money, | ||
default_currency: :EUR, | ||
separator: ".", | ||
delimiter: ",", | ||
symbol_on_right: true, | ||
symbol_space: true | ||
Comment on lines
+28
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align config keys:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, got it.. thats the money config vs. backpex config.. hmhmh |
||
``` | ||
|
||
The default values should ideally match the field options from step 1. | ||
|
||
3. Replace [`Backpex.Ecto.AmountType`]() with [`Backpex.Ecto.AmountType`]() | ||
Flo0807 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```elixir | ||
schema "products" do | ||
field :price, Money.Ecto.Amount.Type | ||
... | ||
end | ||
``` | ||
|
||
4. Add money dependency to your app | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:money, "~> 1.14"}, | ||
... | ||
] | ||
end | ||
``` |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.