2024-09-20 08:38:19 -04:00
|
|
|
<%# locals: (form:, amount_method:, currency_method:, **options) %>
|
2024-07-17 08:57:17 -04:00
|
|
|
|
2024-11-18 15:50:47 -05:00
|
|
|
<% currency_value = if options[:currency_value_override].present?
|
|
|
|
options[:currency_value_override]
|
|
|
|
elsif form.object && form.object.respond_to?(currency_method)
|
2024-09-20 08:38:19 -04:00
|
|
|
form.object.public_send(currency_method)
|
|
|
|
end
|
|
|
|
currency = Money::Currency.new(currency_value || options[:default_currency] || "USD") %>
|
2024-07-17 08:57:17 -04:00
|
|
|
|
2024-09-20 08:38:19 -04:00
|
|
|
<div class="form-field pr-0 <%= options[:container_class] %>" data-controller="money-field">
|
2024-10-23 11:20:55 -04:00
|
|
|
<%= form.label options[:label] || t(".label"), class: "form-field__label" do %>
|
|
|
|
<%= options[:label] || t(".label") %>
|
|
|
|
<% if options[:required] %>
|
|
|
|
<span class="text-red-500">*</span>
|
|
|
|
<% end %>
|
|
|
|
<% end %>
|
2024-07-16 14:08:24 -04:00
|
|
|
|
|
|
|
<div class="flex items-center gap-1">
|
|
|
|
<div class="flex items-center grow gap-1">
|
2024-09-20 08:38:19 -04:00
|
|
|
<span class="text-gray-500 text-sm" data-money-field-target="symbol">
|
|
|
|
<%= currency.symbol %>
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<%= form.number_field amount_method,
|
|
|
|
class: "form-field__input",
|
|
|
|
inline: true,
|
|
|
|
placeholder: "100",
|
|
|
|
value: if options[:value]
|
|
|
|
sprintf("%.#{currency.default_precision}f", options[:value])
|
|
|
|
elsif form.object && form.object.respond_to?(amount_method)
|
2024-11-11 09:39:32 -05:00
|
|
|
val = form.object.public_send(amount_method)
|
|
|
|
sprintf("%.#{currency.default_precision}f", val) if val.present?
|
2024-09-20 08:38:19 -04:00
|
|
|
end,
|
|
|
|
min: options[:min] || -99999999999999,
|
|
|
|
max: options[:max] || 99999999999999,
|
|
|
|
step: currency.step,
|
|
|
|
data: {
|
|
|
|
"money-field-target": "amount",
|
|
|
|
"auto-submit-form-target": ("auto" if options[:auto_submit])
|
|
|
|
}.compact,
|
|
|
|
required: options[:required] %>
|
2024-07-16 14:08:24 -04:00
|
|
|
</div>
|
2024-09-20 08:38:19 -04:00
|
|
|
|
|
|
|
<% unless options[:hide_currency] %>
|
2024-07-16 14:08:24 -04:00
|
|
|
<div>
|
2024-09-20 08:38:19 -04:00
|
|
|
<%= form.select currency_method,
|
|
|
|
currencies_for_select.map(&:iso_code),
|
2024-11-18 15:50:47 -05:00
|
|
|
{ inline: true, selected: currency_value },
|
2024-09-20 08:38:19 -04:00
|
|
|
{
|
|
|
|
class: "w-fit pr-5 disabled:text-gray-400 form-field__input",
|
|
|
|
disabled: options[:disable_currency],
|
|
|
|
data: {
|
|
|
|
"money-field-target": "currency",
|
|
|
|
action: "change->money-field#handleCurrencyChange",
|
|
|
|
"auto-submit-form-target": ("auto" if options[:auto_submit])
|
|
|
|
}.compact
|
|
|
|
} %>
|
2024-07-16 14:08:24 -04:00
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
</div>
|