Class: Avo::FieldWrapperComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Concerns::HasResourceStimulusControllers
Defined in:
app/components/avo/field_wrapper_component.rb

Instance Method Summary collapse

Methods included from Concerns::HasResourceStimulusControllers

#add_stimulus_attributes_for, #get_stimulus_controllers, #stimulus_data_attributes

Instance Method Details

#after_initializeObject



24
25
26
27
# File 'app/components/avo/field_wrapper_component.rb', line 24

def after_initialize
  @action = @field.action
  @classes = @args.dig(:class) || ""
end

#classes(extra_classes = "") ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/components/avo/field_wrapper_component.rb', line 29

def classes(extra_classes = "")
  class_names("field-wrapper",
    @classes,
    extra_classes,
    @field.width_class,
    @field.get_html(:classes, view: @view, element: :wrapper),
    {
      "field-wrapper--stacked": stacked?,
      "field-wrapper--full-width": full_width?,
      "field-wrapper--density-#{@density}": @density && @density != :default,
    })
end

#content_classesObject



62
63
64
# File 'app/components/avo/field_wrapper_component.rb', line 62

def content_classes
  class_names("field-wrapper__content", @field.get_html(:classes, view: @view, element: :content))
end

#content_dataObject



70
71
72
# File 'app/components/avo/field_wrapper_component.rb', line 70

def content_data
  {slot: "value"}.merge(@field.get_html(:data, view: @view, element: :content))
end

#content_styleObject



66
67
68
# File 'app/components/avo/field_wrapper_component.rb', line 66

def content_style
  @field.get_html(:style, view: @view, element: :content)
end

#dataObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/components/avo/field_wrapper_component.rb', line 96

def data
  attributes = {
    field_id: @field.id,
    field_type: @field.type,
    **@data
  }

  # Fetch the data attributes off the html option
  wrapper_data_attributes = @field.get_html :data, view: @view, element: :wrapper
  if wrapper_data_attributes.present?
    attributes.merge! wrapper_data_attributes
  end

  # Add the built-in stimulus integration data tags.
  if @resource.present?
    add_stimulus_attributes_for(@resource, attributes)
  end

  if @action.present?
    add_stimulus_attributes_for(@action, attributes)
  end

  attributes
end

#full_width?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'app/components/avo/field_wrapper_component.rb', line 132

def full_width?
  @full_width
end

#helpObject



84
85
86
# File 'app/components/avo/field_wrapper_component.rb', line 84

def help
  Avo::ExecutionContext.new(target: @help || @field.help, record: record, resource: @resource, view: @view).handle
end

#labelObject



74
75
76
# File 'app/components/avo/field_wrapper_component.rb', line 74

def label
  @label || @field.name
end

#label_classesObject



46
47
48
# File 'app/components/avo/field_wrapper_component.rb', line 46

def label_classes
  class_names("field-wrapper__label", @field.get_html(:classes, view: @view, element: :label))
end

#label_dataObject

The slot goes first so field-declared data merges on top of it rather than replacing it — [data-slot="label"] is a selector the app relies on.



58
59
60
# File 'app/components/avo/field_wrapper_component.rb', line 58

def label_data
  {slot: "label"}.merge(@field.get_html(:data, view: @view, element: :label))
end

#label_forObject



78
79
80
# File 'app/components/avo/field_wrapper_component.rb', line 78

def label_for
  @label_for || @field.form_field_label
end

#label_helpObject



88
89
90
# File 'app/components/avo/field_wrapper_component.rb', line 88

def label_help
  Avo::ExecutionContext.new(target: @label_help || @field.label_help, record: record, resource: @resource, view: @view).handle
end

#label_styleObject

Returned as-is so a blank value stays nil and content_tag omits the attribute instead of rendering an empty style on every field wrapper.



52
53
54
# File 'app/components/avo/field_wrapper_component.rb', line 52

def label_style
  @field.get_html(:style, view: @view, element: :label)
end

#recordObject



92
93
94
# File 'app/components/avo/field_wrapper_component.rb', line 92

def record
  @resource.present? ? @resource.record : nil
end

#render_dash?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/components/avo/field_wrapper_component.rb', line 136

def render_dash?
  @field.value.blank? && @dash_if_blank
end

#stacked?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
# File 'app/components/avo/field_wrapper_component.rb', line 121

def stacked?
  # Override on the declaration level
  return @stacked unless @stacked.nil?

  # Fetch it from the field
  return @field.stacked unless @field.stacked.nil?

  # Fallback to defaults
  Avo.configuration.field_wrapper_layout == :stacked
end

#styleObject



42
43
44
# File 'app/components/avo/field_wrapper_component.rb', line 42

def style
  "#{@style} #{@field.get_html(:style, view: @view, element: :wrapper)}"
end