Skip to content

Commit 1f67ccd

Browse files
committed
Use hidden input for currency value
1 parent 32b952e commit 1f67ccd

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

assets/js/hooks/_masked_input.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ export default {
2323
}
2424

2525
this.mask = IMask(this.maskedInput, this.maskOptions)
26+
this.mask.unmaskedValue = this.rawValue(this.hiddenInput.value)
27+
this.mask.on('accept', this.handleMaskChange.bind(this))
28+
},
29+
updated () {
30+
this.handleMaskChange.bind(this)
31+
},
32+
handleMaskChange () {
33+
this.hiddenInput.value = this.rawValue(this.mask.value)
34+
this.hiddenInput.dispatchEvent(new Event('input', { bubbles: true }))
35+
},
36+
rawValue (value) {
37+
return value
38+
.replace(this.el.dataset.unit || '', '')
39+
.trim()
40+
.replace(new RegExp(`\\${this.el.dataset.thousandsSeparator}`, 'g'), '')
2641
},
2742
destroyed () {
2843
this.mask.destroy()

lib/backpex/html/form.ex

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,8 @@ defmodule Backpex.HTML.Form do
231231
@input_class || "[&_>_input]:input [&_>_input]:w-full",
232232
@errors != [] && (@error_class || "[&_>_input]:input-error [&_>_input]:bg-error/10")
233233
]}>
234-
<input
235-
id={"#{@id}_masked"}
236-
name={@name}
237-
value={@value}
238-
data-masked-input
239-
phx-update="ignore"
240-
class={@input_class}
241-
{@rest}
242-
/>
234+
<input id={"#{@id}_masked"} name={@name} data-masked-input phx-update="ignore" {@rest} />
235+
<input type="hidden" value={@value} name={@name} data-hidden-input />
243236
</span>
244237
</div>
245238
<.error :for={msg <- @errors} :if={not @hide_errors}>{msg}</.error>

0 commit comments

Comments
 (0)