Skip to content

Commit 4a56ecf

Browse files
committed
Refactor number input to currency input
1 parent 5b80804 commit 4a56ecf

File tree

8 files changed

+41
-41
lines changed

8 files changed

+41
-41
lines changed

assets/js/hooks/_masked_number_input.js renamed to assets/js/hooks/_currency_input.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,14 @@ export default {
3636
},
3737
handleMaskChange () {
3838
this.hiddenInput.value = this.rawValue(this.mask.value)
39-
this.hiddenInput.dispatchEvent(new Event('input', { bubbles: true }))
39+
},
40+
destroyed () {
41+
this.mask.destroy()
4042
},
4143
rawValue (value) {
4244
return value
4345
.replace(this.el.dataset.unit || '', '')
4446
.trim()
4547
.replace(new RegExp(`\\${this.el.dataset.thousandsSeparator}`, 'g'), '')
46-
},
47-
destroyed () {
48-
this.mask.destroy()
4948
}
5049
}

assets/js/hooks/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export { default as BackpexSidebarSections } from './_sidebar_sections'
44
export { default as BackpexStickyActions } from './_sticky_actions'
55
export { default as BackpexThemeSelector } from './_theme_selector'
66
export { default as BackpexTooltip } from './_tooltip'
7-
export { default as BackpexMaskedNumberInput } from './_masked_number_input'
7+
export { default as BackpexCurrencyInput } from './_currency_input'

lib/backpex/fields/currency.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,23 @@ defmodule Backpex.Fields.Currency do
8484

8585
@impl Backpex.Field
8686
def render_form(assigns) do
87-
assigns = assign(assigns, :mask_pattern, build_mask_pattern(assigns.field_options))
88-
8987
~H"""
9088
<div>
9189
<Layout.field_container>
9290
<:label align={Backpex.Field.align_label(@field_options, assigns)}>
9391
<Layout.input_label text={@field_options[:label]} />
9492
</:label>
95-
<BackpexForm.masked_number_input
93+
<BackpexForm.currency_input
9694
type="text"
9795
field={@form[@name]}
9896
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
9997
help_text={Backpex.Field.help_text(@field_options, assigns)}
10098
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}
10199
phx-throttle={Backpex.Field.throttle(@field_options, assigns)}
102100
radix={@field_options[:radix]}
103-
mask_pattern={@mask_pattern}
104101
thousands_separator={@field_options[:thousands_separator]}
102+
unit={@field_options[:unit]}
103+
unit_position={@field_options[:unit_position]}
105104
/>
106105
</Layout.field_container>
107106
</div>
@@ -115,7 +114,4 @@ defmodule Backpex.Fields.Currency do
115114
ilike(fragment("CAST(? AS TEXT)", field(schema_name, ^field_name)), ^search_string)
116115
)
117116
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}"
121117
end

lib/backpex/html/form.ex

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ defmodule Backpex.HTML.Form do
172172
173173
## Examples
174174
175-
<.masked_number_input field={@form[:amount]} unit="€" />
176-
<.masked_number_input id="amount-input" name="amount" value="20" unit="€" readonly />
175+
<.currency_input field={@form[:amount]} unit="€" unit_position={:after} />
176+
<.currency_input id="amount-input" name="amount" value="20" unit="$" unit_position={:before} readonly />
177177
"""
178178
attr :id, :any, default: nil
179179
attr :name, :any
@@ -197,33 +197,37 @@ 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, required: true
201+
attr :unit_position, :atom, required: true, values: ~w(before after)a
200202
attr :radix, :string, default: "."
201203
attr :thousands_separator, :string, default: ","
202-
attr :mask_pattern, :string, default: "num"
203204

204205
attr :rest, :global, include: ~w(accept autocomplete capture cols disabled form list max maxlength min minlength
205206
multiple pattern placeholder readonly required rows size step)
206207

207-
def masked_number_input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
208+
def currency_input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
208209
errors = if Phoenix.Component.used_input?(field), do: field.errors, else: []
209210

210211
assigns
211212
|> assign(field: nil, id: assigns.id || field.id)
212213
|> assign(:errors, translate_form_errors(errors, assigns.translate_error_fun))
213214
|> assign_new(:name, fn -> field.name end)
214215
|> assign_new(:value, fn -> field.value end)
215-
|> masked_number_input()
216+
|> currency_input()
216217
end
217218

218-
def masked_number_input(assigns) do
219+
def currency_input(assigns) do
220+
assigns = assign(assigns, :mask_pattern, build_mask_pattern(assigns.unit_position, assigns.unit))
221+
219222
~H"""
220223
<div class={["fieldset py-0", @class]}>
221224
<span :if={@label} class="label mb-1">{@label}</span>
222225
<div
223226
id={@id}
224-
phx-hook="BackpexMaskedNumberInput"
227+
phx-hook="BackpexCurrencyInput"
225228
data-radix={@radix}
226229
data-thousands-separator={@thousands_separator}
230+
data-unit={@unit}
227231
data-mask-pattern={@mask_pattern}
228232
>
229233
<%!-- As the input ignores updates, we need to wrap it in a span to apply the styles correctly --%>
@@ -241,6 +245,9 @@ defmodule Backpex.HTML.Form do
241245
"""
242246
end
243247

248+
defp build_mask_pattern(:before = _unit_position, unit), do: "#{unit} num"
249+
defp build_mask_pattern(:after = _unit_position, unit), do: "num #{unit}"
250+
244251
@doc """
245252
Generates a generic error message.
246253
"""

priv/static/js/backpex.cjs.js

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

priv/static/js/backpex.cjs.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

priv/static/js/backpex.esm.js

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

priv/static/js/backpex.esm.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)