Class: SdrViewComponents::Forms::FieldComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/sdr_view_components/forms/field_component.rb

Overview

Base component for all form fields.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

#args_for, #merge_actions, #merge_classes

Constructor Details

#initialize(form:, field_name:, container_classes: [], variant: :default, **args) ⇒ FieldComponent

Returns a new instance of FieldComponent.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/components/sdr_view_components/forms/field_component.rb', line 9

def initialize(form:, field_name:, container_classes: [], variant: :default, **args)
  @form = form
  @field_name = field_name
  @container_classes = container_classes
  @variant = variant
  # In the help_text_below variant, the help text is rendered below the input field.
  # This is necessary for some types of fields such as checkboxes.
  raise ArgumentError, 'invalid variant' unless %i[default help_text_below].include?(@variant)

  @args = args
  super()
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



22
23
24
# File 'app/components/sdr_view_components/forms/field_component.rb', line 22

def args
  @args
end

#field_nameObject (readonly)

Returns the value of attribute field_name.



22
23
24
# File 'app/components/sdr_view_components/forms/field_component.rb', line 22

def field_name
  @field_name
end

#formObject (readonly)

Returns the value of attribute form.



22
23
24
# File 'app/components/sdr_view_components/forms/field_component.rb', line 22

def form
  @form
end

#variantObject (readonly)

Returns the value of attribute variant.



22
23
24
# File 'app/components/sdr_view_components/forms/field_component.rb', line 22

def variant
  @variant
end

Instance Method Details

#container_argsObject

Returns a hash of arguments for the container element. Prefix and argument with ‘container_’ in order for it to be passed to the container div. (i.e. :container_class will be passed as :class to the container div)



39
40
41
# File 'app/components/sdr_view_components/forms/field_component.rb', line 39

def container_args
  args_for(args:, prefix: 'container_')
end

#container_classesObject



76
77
78
# File 'app/components/sdr_view_components/forms/field_component.rb', line 76

def container_classes
  merge_classes(@container_classes)
end

#error_argsObject

Returns a hash of arguments for the error element. Prefix and argument with ‘error_’ in order for it to be passed to to the invalid feedback element.



46
47
48
# File 'app/components/sdr_view_components/forms/field_component.rb', line 46

def error_args
  args_for(args:, prefix: 'error_')
end

#help_text_argsObject

Returns a hash of arguments for the help text element. Prefix and argument with ‘help_’ in order for it to be passed to to the help text element.



53
54
55
# File 'app/components/sdr_view_components/forms/field_component.rb', line 53

def help_text_args
  args_for(args:, prefix: 'help_').merge({ id: help_text_id })
end

#help_text_below?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/components/sdr_view_components/forms/field_component.rb', line 80

def help_text_below?
  variant == :help_text_below
end

#input_argsObject

Returns a hash of arguments for the input element. Prefix and argument with ‘input_’ in order for it to be passed to the input element.



60
61
62
# File 'app/components/sdr_view_components/forms/field_component.rb', line 60

def input_args
  args_for(args:, prefix: 'input_').merge({ aria: field_aria })
end

#input_componentObject

Any component that inherits from FieldComponent must implement this method or provide its own template.

Raises:

  • (NotImplementedError)


26
27
28
# File 'app/components/sdr_view_components/forms/field_component.rb', line 26

def input_component
  raise NotImplementedError
end

#label_argsObject

Returns a hash of arguments for the label element. Prefix and argument with ‘label_’ in order for it to be passed to the label element.



67
68
69
# File 'app/components/sdr_view_components/forms/field_component.rb', line 67

def label_args
  args_for(args:, prefix: 'label_')
end

#label_field_nameObject

Subclasses may override, e.g., for radio buttons.



72
73
74
# File 'app/components/sdr_view_components/forms/field_component.rb', line 72

def label_field_name
  field_name
end