Class: Maglev::Uikit::Form::LinkComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/maglev/uikit/form/link_component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

#button_class_names

Constructor Details

#initialize(name:, path:, options: {}) ⇒ LinkComponent

Value: { link_type:, link_id:, href:, text: }



10
11
12
13
14
# File 'app/components/maglev/uikit/form/link_component.rb', line 10

def initialize(name:, path:, options: {})
  @name = name
  @options = options
  @path = path.gsub(':id', dom_id)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'app/components/maglev/uikit/form/link_component.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'app/components/maglev/uikit/form/link_component.rb', line 7

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'app/components/maglev/uikit/form/link_component.rb', line 7

def path
  @path
end

Class Method Details



78
79
80
# File 'app/components/maglev/uikit/form/link_component.rb', line 78

def self.default_link_type_klass
  Maglev::Uikit::Form::Link::UrlLinkComponent
end


60
61
62
63
64
65
66
67
# File 'app/components/maglev/uikit/form/link_component.rb', line 60

def self.link_type_component(input_name:, value:, path:)
  # Default to url if the link type is not found (shouldn't happen)
  klass = link_type_klasses_map.fetch(value[:link_type]&.to_sym, default_link_type_klass)

  return unless klass

  klass.new(input_name: input_name, link: value, path: path)
end


69
70
71
72
73
74
75
76
# File 'app/components/maglev/uikit/form/link_component.rb', line 69

def self.link_type_klasses_map
  {
    email: Maglev::Uikit::Form::Link::EmailLinkComponent,
    url: Maglev::Uikit::Form::Link::UrlLinkComponent,
    page: Maglev::Uikit::Form::Link::PageLinkComponent,
    static_page: Maglev::Uikit::Form::Link::StaticPageLinkComponent
  }
end

Instance Method Details

#dom_idObject



16
17
18
# File 'app/components/maglev/uikit/form/link_component.rb', line 16

def dom_id
  name.to_s.parameterize.underscore
end

#errorObject



40
41
42
# File 'app/components/maglev/uikit/form/link_component.rb', line 40

def error
  options[:error]
end

#labelObject



28
29
30
# File 'app/components/maglev/uikit/form/link_component.rb', line 28

def label
  options[:label]
end


48
49
50
51
52
53
54
# File 'app/components/maglev/uikit/form/link_component.rb', line 48

def link_text
  {
    name: "#{name}[text]",
    value: value[:text],
    placeholder: t('maglev.uikit.form.link.text_placeholder')
  }.merge(options[:link_text] || {})
end


56
57
58
# File 'app/components/maglev/uikit/form/link_component.rb', line 56

def link_type_component
  self.class.link_type_component(input_name: name, value: value, path: path)
end

#placeholderObject



36
37
38
# File 'app/components/maglev/uikit/form/link_component.rb', line 36

def placeholder
  options[:placeholder].presence || t('maglev.uikit.form.link.placeholder')
end

#turbo_frame_idObject



20
21
22
# File 'app/components/maglev/uikit/form/link_component.rb', line 20

def turbo_frame_id
  "#{dom_id}-frame"
end

#valueObject



24
25
26
# File 'app/components/maglev/uikit/form/link_component.rb', line 24

def value
  (options[:value] || {}).with_indifferent_access
end

#value?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/components/maglev/uikit/form/link_component.rb', line 32

def value?
  value&.compact_blank.present?
end

#with_text?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/components/maglev/uikit/form/link_component.rb', line 44

def with_text?
  !!options[:with_text]
end