Class: Brut::FrontEnd::Components::Inputs::ButtonTag

Inherits:
Brut::FrontEnd::Components::Input show all
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

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

#html_escape, #t

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from Brut::Framework::Errors

#abstract_method!, #bug!

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.

Parameters:

  • form (Brut::FrontEnd::Form)

    The form that is being rendered. This method will consult this class to understand the requirements on this input so its HTML is generated correctly.

  • input_name (String)

    the name of the input, which should be a member of form

  • index (Integer) (defaults to: nil)

    if this input is part of an array, this is the index into that array. This is used to get the input's value.

  • html_attributes (Hash)

    any additional HTML attributes to include on the <input> element. Note that if you set type here to be reset or button, this button will not submit the form (as per the HTML spec). Also note that the various form* attributes will take effect and work as desired.



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_templateObject



38
39
40
41
42
# File 'lib/brut/front_end/components/inputs/button_tag.rb', line 38

def view_template
  button(**@attributes) do
    yield
  end
end