<% # headmin/forms/select # # ==== Required parameters # * +form+ - Form object # * +attribute+ - Name of the attribute of the form model # * +collection+ - Values to create option tags for # # ==== Optional parameters # * +aria+ - Provide a hash to define all aria attributes # * +autocomplete+ - Value to be autofilled by the browser # * +autofocus+ - Set to true to focus on this field when the page renders # * +data+ - Optional HTML data attributes # * +disabled+ - One or more values to be disabled in the option tags # * +include_blank+ - Set to true or prompt string if the first option tag is a blank # * +multiple+ - Set to true if multiple selections are allowed # * +required+ - Set to true to mark as required # * +selected+ - Value to be marked as "selected" # * +size+ - The number of visible rows in a multiple select field # * +tags+ - Set to true if new options are allowed to be created # # ==== Extra parameters # Listed in 'headmin/forms/base' # # ==== References # https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select # https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select # # ==== Examples # Basic version # <%= render 'headmin/forms/select', form: form, attribute: :color, collection: ['red', 'green', 'blue'] %#> # # Disabled # <%= render 'headmin/forms/select', form: form, attribute: :color, collection: ['red', 'green', 'blue'], disabled: true %#> # # Disable specific options # <%= render 'headmin/forms/select', form: form, attribute: :color, collection: ['red', 'green', 'blue'], disabled: ['red'] %#> # # Show blank option # <%= render 'headmin/forms/select', form: form, attribute: :color, collection: ['red', 'green', 'blue'], include_blank: 'Pick a color' %#> # # Select a default option # <%= render 'headmin/forms/select', form: form, attribute: :color, collection: ['red', 'green', 'blue'], selected: 'green' %#> # # Allow multiple options to be selected # <%= render 'headmin/forms/select', form: form, attribute: :color, collection: ['red', 'green', 'blue'], multiple: true %#> # # Allow multiple options to be selected and new options to be created # <%= render 'headmin/forms/select', form: form, attribute: :color, collection: ['red', 'green', 'blue'], tags: true %#> options_keys = %i(include_blank selected disabled) options = local_assigns.slice(*options_keys).merge( selected: form.object&.send(attribute), ) html_option_keys = %i(aria autocomplete autofocus data disabled id multiple required size) html_options = local_assigns.slice(*html_option_keys).merge( aria: { describedby: form_field_validation_id(form, attribute) }, class: ['form-select', form_field_validation_class(form, attribute)].join(' '), data: { tags: local_assigns[:tags], controller: 'select'}, disabled: local_assigns[:disabled] == true, multiple: local_assigns[:multiple] || local_assigns[:tags], ) %> <%= render 'headmin/forms/base', local_assigns do |form| %> <%= form.select(attribute, collection, options, html_options) %> <% end %>