Skip to content

Commit ec4f914

Browse files
committed
Update masked input
1 parent b145a4d commit ec4f914

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

assets/js/hooks/_masked_input.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ export default {
88
this.initializeMask()
99
},
1010
initializeMask () {
11-
const maskPattern = this.el.dataset.unit ? `num ${this.el.dataset.unit}` : 'num'
11+
const maskPattern = this.el.dataset.maskPattern
12+
13+
console.log('Mask pattern:', maskPattern)
14+
15+
if (!maskPattern) {
16+
console.error('You must provide a mask pattern in the data-masked-pattern attribute.')
17+
return
18+
}
1219

1320
this.maskOptions = {
1421
mask: maskPattern,

lib/backpex/fields/currency.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ defmodule Backpex.Fields.Currency do
1313
type: :string,
1414
default: "€"
1515
],
16+
unit_position: [
17+
doc: "Position of the unit relative to the value, either `:before` or `:after`.",
18+
type: {:in, [:before, :after]},
19+
default: :before
20+
],
1621
radix: [
1722
doc:
1823
"Character used as the decimal separator, e.g. ',' or '.'. Make sure this value matches the one you've configured in your Money library.",
@@ -79,6 +84,8 @@ defmodule Backpex.Fields.Currency do
7984

8085
@impl Backpex.Field
8186
def render_form(assigns) do
87+
assigns = assign(assigns, :mask_pattern, build_mask_pattern(assigns.field_options))
88+
8289
~H"""
8390
<div>
8491
<Layout.field_container>
@@ -92,8 +99,8 @@ defmodule Backpex.Fields.Currency do
9299
help_text={Backpex.Field.help_text(@field_options, assigns)}
93100
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}
94101
phx-throttle={Backpex.Field.throttle(@field_options, assigns)}
95-
unit={@field_options[:unit]}
96102
radix={@field_options[:radix]}
103+
mask_pattern={@mask_pattern}
97104
thousands_separator={@field_options[:thousands_separator]}
98105
/>
99106
</Layout.field_container>
@@ -108,4 +115,7 @@ defmodule Backpex.Fields.Currency do
108115
ilike(fragment("CAST(? AS TEXT)", field(schema_name, ^field_name)), ^search_string)
109116
)
110117
end
118+
119+
defp build_mask_pattern(%{unit_position: :before} = field_option), do: "#{field_option.unit} num"
120+
defp build_mask_pattern(%{unit_position: :after} = field_option), do: "num #{field_option.unit}"
111121
end

lib/backpex/html/form.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ defmodule Backpex.HTML.Form do
197197
attr :translate_error_fun, :any, default: &Function.identity/1, doc: "a custom function to map form errors"
198198
attr :hide_errors, :boolean, default: false, doc: "if errors should be hidden"
199199

200-
attr :unit, :string, default: nil
201200
attr :radix, :string, default: "."
202201
attr :thousands_separator, :string, default: ","
202+
attr :mask_pattern, :string, default: "num"
203203

204204
attr :rest, :global, include: ~w(accept autocomplete capture cols disabled form list max maxlength min minlength
205205
multiple pattern placeholder readonly required rows size step)
@@ -217,14 +217,15 @@ defmodule Backpex.HTML.Form do
217217

218218
def masked_input(assigns) do
219219
~H"""
220+
220221
<div class={["fieldset py-0", @class]}>
221222
<span :if={@label} class="label mb-1">{@label}</span>
222223
<div
223224
id={@id}
224225
phx-hook="BackpexMaskedInput"
225226
data-radix={@radix}
226-
data-unit={@unit}
227227
data-thousands-separator={@thousands_separator}
228+
data-mask-pattern={@mask_pattern}
228229
>
229230
<%!-- As the input ignores updates, we need to wrap it in a span to apply the styles correctly --%>
230231
<span class={[

0 commit comments

Comments
 (0)