Class: Practical::Views::Form::InputComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/practical/views/form/input_component.rb

Defined Under Namespace

Classes: FieldOptionsNotCalledError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f:, object_method:, label_options: {}) ⇒ InputComponent

Returns a new instance of InputComponent.



10
11
12
13
14
# File 'app/components/practical/views/form/input_component.rb', line 10

def initialize(f:, object_method:, label_options: {})
  self.f = f
  self.object_method = object_method
  self.label_options = label_options
end

Instance Attribute Details

#fObject

Returns the value of attribute f.



5
6
7
# File 'app/components/practical/views/form/input_component.rb', line 5

def f
  @f
end

#label_optionsObject

Returns the value of attribute label_options.



5
6
7
# File 'app/components/practical/views/form/input_component.rb', line 5

def label_options
  @label_options
end

#object_methodObject

Returns the value of attribute object_method.



5
6
7
# File 'app/components/practical/views/form/input_component.rb', line 5

def object_method
  @object_method
end

Instance Method Details

#around_renderObject



28
29
30
31
32
33
34
35
# File 'app/components/practical/views/form/input_component.rb', line 28

def around_render
  result = yield
  unless @_field_options_called
    raise FieldOptionsNotCalledError, "field_options was not called when rendering an InputComponent; which means this the input & its error container are not connected."
  end

  return result
end

#field_errors_idObject



16
17
18
# File 'app/components/practical/views/form/input_component.rb', line 16

def field_errors_id
  f.field_errors_id(object_method)
end

#field_options(**options) ⇒ Object



20
21
22
23
24
25
26
# File 'app/components/practical/views/form/input_component.rb', line 20

def field_options(**options)
  @_field_options_called = true
  return mix({
    "aria-describedby": field_errors_id,
    "data": {"pf-initial-load-errors": f.errors_for(object_method)&.any? }
  }, options)
end