Class: Brut::FrontEnd::Components::Inputs::ButtonTag
- Inherits:
-
Brut::FrontEnd::Components::Input
- Object
- Phlex::HTML
- Brut::FrontEnd::Component
- Brut::FrontEnd::Components::Input
- Brut::FrontEnd::Components::Inputs::ButtonTag
- Defined in:
- lib/brut/front_end/components/inputs/button_tag.rb
Overview
Creates a button tag to be used inside a form. This is only
needed if your form declared a button
and you wish the value
to be included whern the form is submitted (for example, if you have
more than one submit button and wish to know which one was clicked on the
back end).
Instance Method Summary collapse
-
#initialize(form:, input_name:, index: nil, **html_attributes) ⇒ ButtonTag
constructor
Creates the appropriate button 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:, index: nil, **html_attributes) ⇒ ButtonTag
Creates the appropriate button for the given Form and input name. Note that if the
form has no value for this input, the value "true"
will be used, as this allows you to create
buttons without worrying about an often irrelevant value.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/brut/front_end/components/inputs/button_tag.rb', line 19 def initialize(form:, input_name:, index: nil, **html_attributes) input = form.input(input_name, index:) if input.class.name != "Brut::FrontEnd::Forms::Button" raise ArgumentError, "#{self.class} can only be used with `button` elements, not #{input.class.name} form elements" end default_html_attributes = {} html_attributes = html_attributes.map { |key,value| [ key.to_sym, value ] }.to_h default_html_attributes[:name] = if input.array? "#{input.name}[]" else input.name end value = RichString.from_string(input.value) default_html_attributes[:value] = (value || "true").to_s @attributes = default_html_attributes.merge(html_attributes) end |
Instance Method Details
#view_template ⇒ Object
38 39 40 41 42 |
# File 'lib/brut/front_end/components/inputs/button_tag.rb', line 38 def view_template (**@attributes) do yield end end |