Class: NitroKit::ButtonTo

Inherits:
Component
  • Object
show all
Includes:
Phlex::Rails::Helpers::FormTag
Defined in:
app/components/nitro_kit/button_to.rb

Constant Summary collapse

METHODS =
%i[get post patch put delete].freeze

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, href:, method: :post, variant: :default, size: :md, icon: nil, icon_end: nil, label: nil, id: nil, disabled: false, loading: false, html: {}, aria: {}, data: {}, button_html: {}, button_aria: {}, button_data: {}, desperately_need_a_class: nil) ⇒ ButtonTo

Returns a new instance of ButtonTo.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'app/components/nitro_kit/button_to.rb', line 9

def initialize(
  text = nil,
  href:,
  method: :post,
  variant: :default,
  size: :md,
  icon: nil,
  icon_end: nil,
  label: nil,
  id: nil,
  disabled: false,
  loading: false,
  html: {},
  aria: {},
  data: {},
  button_html: {},
  button_aria: {},
  button_data: {},
  desperately_need_a_class: nil
)
  unless href.is_a?(String) && href.present?
    raise ArgumentError, "ButtonTo href: must be a non-blank String"
  end

  @text = text
  @href = href
  @method = validate_choice!(:method, method.to_s.to_sym, METHODS)
  @button = Button.new(
    text,
    variant:,
    size:,
    icon:,
    icon_end:,
    label:,
    type: :submit,
    disabled:,
    loading:,
    html: button_html,
    aria: button_aria,
    data: button_data
  )

  super(
    component: :button_to,
    attributes: { id: },
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href.



61
62
63
# File 'app/components/nitro_kit/button_to.rb', line 61

def href
  @href
end

#methodObject (readonly)

Returns the value of attribute method.



61
62
63
# File 'app/components/nitro_kit/button_to.rb', line 61

def method
  @method
end

Instance Method Details

#view_template(&block) ⇒ Object



63
64
65
66
67
# File 'app/components/nitro_kit/button_to.rb', line 63

def view_template(&block)
  form_tag(href, method:, **root_attributes) do
    render_in_slot(@button, :button, &block)
  end
end