Files
ubicloud/views/components/form/radio_small_cards.erb
Enes Cakir be56a9139f Fix arguments for UI components
Some arguments for UI components are optional, so we don't have to
provide all of them when using them. I had added a `defined?(name)`
check to use default values when an argument is not provided.

Jeremy pointed out that it was working by accident. In the code `name =
defined?(name)`, `defined?(name)` always returns `local-variable`
because it’s on the right-hand side of the assignment, while the local
variable is on the left-hand side.

When we remove the `defined?` calls, the remaining code is equivalent to
`||=`.
2024-11-12 10:24:27 +03:00

43 lines
1.7 KiB
Plaintext

<% name ||= nil
label ||= nil
options ||= {}
selected = flash.dig("old", name) || selected
error ||= flash.dig("errors", name)
description ||= nil
attributes ||= {} %>
<div class="space-y-2">
<label for="<%= name %>" class="text-sm font-medium leading-6 text-gray-900"><%= label %></label>
<fieldset class="radio-small-cards" id="<%= name %>-radios">
<legend class="sr-only"><%= label %></legend>
<div class="grid gap-3 grid-cols-1 sm:grid-cols-2 md:grid-cols-3 xl:grid-cols-4">
<% options.each do |opt_val, opt_text, opt_details| %>
<label>
<input
type="radio"
name="<%= name %>"
value="<%= opt_val %>"
data-details="<%= opt_details %>"
class="peer sr-only"
<%= (opt_val == selected) ? "checked" : "" %>
<% attributes.each do |atr_key, atr_value| %>
<%= atr_key %>="<%= atr_value %>"
<% end%>
>
<span
class="flex items-center justify-center rounded-md py-3 px-3 text-sm font-semibold sm:flex-1 cursor-pointer focus:outline-none
ring-1 ring-gray-300 bg-white text-gray-900 hover:bg-gray-50
peer-focus-visible:ring-2 peer-focus-visible:ring-orange-600 peer-focus-visible:ring-offset-2 peer-checked:bg-orange-600 peer-checked:text-white peer-checked:hover:bg-orange-700"
><%= opt_text %></span>
</label>
<% end %>
</div>
</fieldset>
<% if error %>
<p class="text-sm text-red-600 leading-6" id="<%= name %>-error"><%= error %></p>
<% end %>
<% if description %>
<p class="text-sm text-gray-500 leading-6" id="<%= name %>-description"><%= description %></p>
<% end %>
</div>