Class: Avo::FieldWrapperComponent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HasResourceStimulusControllers

#add_stimulus_attributes_for, #get_stimulus_controllers, #stimulus_data_attributes

Constructor Details

#initialize(dash_if_blank: true, data: {}, compact: false, help: nil, field: nil, form: nil, full_width: false, label: nil, resource: nil, short: false, stacked: nil, style: "", view: :show, label_for: nil, **args) ⇒ FieldWrapperComponent

Returns a new instance of FieldWrapperComponent.



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
# File 'app/components/avo/field_wrapper_component.rb', line 14

def initialize(
  dash_if_blank: true,
  data: {},
  compact: false,
  help: nil, # do we really need it?
  field: nil,
  form: nil,
  full_width: false,
  label: nil, # do we really need it?
  resource: nil,
  short: false,
  stacked: nil,
  style: "",
  view: :show,
  label_for: nil,
  **args
)
  @args = args
  @classes = args[:class].present? ? args[:class] : ""
  @dash_if_blank = dash_if_blank
  @data = data
  @compact = compact
  @help = help
  @field = field
  @form = form
  @full_width = full_width
  @label = label
  @resource = resource
  @action = field.action
  @short = short
  @stacked = stacked
  @style = style
  @view = view
  @label_for = label_for
end

Instance Attribute Details

#compactObject (readonly)

Returns the value of attribute compact.



7
8
9
# File 'app/components/avo/field_wrapper_component.rb', line 7

def compact
  @compact
end

#dash_if_blankObject (readonly)

Returns the value of attribute dash_if_blank.



6
7
8
# File 'app/components/avo/field_wrapper_component.rb', line 6

def dash_if_blank
  @dash_if_blank
end

#fieldObject (readonly)

Returns the value of attribute field.



8
9
10
# File 'app/components/avo/field_wrapper_component.rb', line 8

def field
  @field
end

#formObject (readonly)

Returns the value of attribute form.



9
10
11
# File 'app/components/avo/field_wrapper_component.rb', line 9

def form
  @form
end

#full_widthObject (readonly)

Returns the value of attribute full_width.



10
11
12
# File 'app/components/avo/field_wrapper_component.rb', line 10

def full_width
  @full_width
end

#resourceObject (readonly)

Returns the value of attribute resource.



11
12
13
# File 'app/components/avo/field_wrapper_component.rb', line 11

def resource
  @resource
end

#viewObject (readonly)

Returns the value of attribute view.



12
13
14
# File 'app/components/avo/field_wrapper_component.rb', line 12

def view
  @view
end

Instance Method Details

#classes(extra_classes = "") ⇒ Object



50
51
52
# File 'app/components/avo/field_wrapper_component.rb', line 50

def classes(extra_classes = "")
  "field-wrapper relative flex flex-col grow pb-2 md:pb-0 leading-tight min-h-14 h-full #{stacked? ? "field-wrapper-layout-stacked" : "field-wrapper-layout-inline md:flex-row md:items-center"} #{compact? ? "field-wrapper-size-compact" : "field-wrapper-size-regular"} #{full_width? ? "field-width-full" : "field-width-regular"} #{@classes || ""} #{extra_classes || ""} #{@field.get_html(:classes, view: view, element: :wrapper)}"
end

#compact?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/components/avo/field_wrapper_component.rb', line 112

def compact?
  @compact
end

#dataObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/components/avo/field_wrapper_component.rb', line 76

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)


120
121
122
# File 'app/components/avo/field_wrapper_component.rb', line 120

def full_width?
  @full_width
end

#helpObject



68
69
70
# File 'app/components/avo/field_wrapper_component.rb', line 68

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

#labelObject



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

def label
  @label || @field.name
end

#label_forObject



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

def label_for
  @label_for || @field.form_field_label
end

#recordObject



72
73
74
# File 'app/components/avo/field_wrapper_component.rb', line 72

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

#short?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/components/avo/field_wrapper_component.rb', line 116

def short?
  @short
end

#stacked?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
# File 'app/components/avo/field_wrapper_component.rb', line 101

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



54
55
56
# File 'app/components/avo/field_wrapper_component.rb', line 54

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