Class: Brut::FrontEnd::Components::Inputs::SelectTagWithOptions
- Inherits:
-
Brut::FrontEnd::Components::Input
- Object
- Phlex::HTML
- Brut::FrontEnd::Component
- Brut::FrontEnd::Components::Input
- Brut::FrontEnd::Components::Inputs::SelectTagWithOptions
- Defined in:
- lib/brut/front_end/components/inputs/select_tag_with_options.rb
Overview
Renders an HTML <select>
.
Instance Method Summary collapse
-
#initialize(form:, input_name:, options:, include_blank: false, value_attribute:, option_text_attribute:, index: nil, html_attributes: {}) ⇒ Brut::FrontEnd::Components::Inputs::SelectTagWithOptions
constructor
Creates the appropriate select input for the given Form and input name.
-
#view_template ⇒ Object
Methods inherited from Brut::FrontEnd::Component
#around_template, component_name, #component_name
Methods included from Brut::FrontEnd::Component::Helpers
#entity, #global_component, #inline_svg
Methods included from I18n::ForHTML
Methods included from I18n::BaseMethods
#l, #t, #t_direct, #this_field_value
Methods included from Brut::Framework::Errors
Constructor Details
#initialize(form:, input_name:, options:, include_blank: false, value_attribute:, option_text_attribute:, index: nil, html_attributes: {}) ⇒ Brut::FrontEnd::Components::Inputs::SelectTagWithOptions
Creates the appropriate select input for the given Form and input name.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/brut/front_end/components/inputs/select_tag_with_options.rb', line 29 def initialize(form:, input_name:, options:, include_blank: false, value_attribute:, option_text_attribute:, index: nil, html_attributes: {}) html_attributes = html_attributes.map { |key,value| [ key.to_sym, value ] }.to_h default_html_attributes = {} index ||= 0 input = form.input(input_name, index:) default_html_attributes[:required] = input.required if !form.new? && !input.valid? default_html_attributes["data-invalid"] = true input.validity_state.each do |constraint,violated| if violated default_html_attributes["data-#{constraint}"] = true end end end name = if input.array? "#{input.name}[]" else input.name end input_value = input.value @options = @include_blank = IncludeBlank.from_param(include_blank) @value_attribute = value_attribute @option_text_attribute = option_text_attribute @html_attributes = default_html_attributes.merge(html_attributes) @html_attributes[:name] = name if input_value.nil? || input_value.to_s.strip == "" @selected_value = nil # explicitly nothing is selected else if input_value.kind_of?(Array) raise "WTF: #{name}" # XXX? end option = .detect { |option| input_value == option.send(@value_attribute) } if option.nil? raise ArgumentError, "selected_value #{input_value}/#{input_value.class} was not the value for #{value_attribute} on any of the options: #{.map { |option| option.send(value_attribute) }.join(', ')}" end @selected_value = option.send(@value_attribute) end end |
Instance Method Details
#view_template ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/brut/front_end/components/inputs/select_tag_with_options.rb', line 81 def view_template select(**@html_attributes) { if @include_blank option(**@include_blank.option_attributes) { @include_blank.text_content } end = @options.each do |option| value = option.send(@value_attribute) option_attributes = { value: value } if value == @selected_value option_attributes[:selected] = true end option(**option_attributes) { option.send(@option_text_attribute) } end } end |