Class: StimulusPlumbers::Form::Field

Inherits:
Base
  • Object
show all
Defined in:
lib/stimulus_plumbers/form/field.rb

Constant Summary collapse

TYPES =
%i[
  text email number url tel color month week range datetime_local
  text_area file password date time select search code credit_card
].freeze
COLLECTION_TYPES =
%i[radio check_box collection_select grouped_collection_select].freeze
OPTIONS =
(Base::OPTIONS + %i[hide_label]).freeze
LABEL_MODES =

:aria uses aria-labelledby because non-labelable components cannot use <label for>.

%i[native aria].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#described_by, #error?, #render_errors, #render_hint, #theme

Constructor Details

#initialize(template, hide_label: false, label_mode: :native, **kwargs) ⇒ Field

Returns a new instance of Field.



33
34
35
36
37
# File 'lib/stimulus_plumbers/form/field.rb', line 33

def initialize(template, hide_label: false, label_mode: :native, **kwargs)
  super(template, **kwargs)
  @hide_label = hide_label
  @label_mode = self.class.validate_label_mode!(label_mode)
end

Instance Attribute Details

#hide_labelObject (readonly)

Returns the value of attribute hide_label.



19
20
21
# File 'lib/stimulus_plumbers/form/field.rb', line 19

def hide_label
  @hide_label
end

#label_modeObject (readonly)

Returns the value of attribute label_mode.



19
20
21
# File 'lib/stimulus_plumbers/form/field.rb', line 19

def label_mode
  @label_mode
end

Class Method Details

.label_id(input_id) ⇒ Object



22
23
24
# File 'lib/stimulus_plumbers/form/field.rb', line 22

def label_id(input_id)
  [input_id, "label"].compact.join("_")
end

.validate_label_mode!(mode) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/stimulus_plumbers/form/field.rb', line 26

def validate_label_mode!(mode)
  return mode if LABEL_MODES.include?(mode)

  raise ArgumentError, "unknown label_mode: #{mode.inspect} (expected one of #{LABEL_MODES.join(", ")})"
end

Instance Method Details

#label_hidden?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/stimulus_plumbers/form/field.rb', line 39

def label_hidden?
  @hide_label
end

#native_label?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/stimulus_plumbers/form/field.rb', line 43

def native_label?
  @label_mode == :native
end

#render(object, attribute, input_id:, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/stimulus_plumbers/form/field.rb', line 47

def render(object, attribute, input_id:, &block)
  @label ||= attribute.to_s.humanize
  case native_label? ? @floating : nil
  when *StimulusPlumbers::Themes::Schema::Form::Floating::Ranges::TYPE
    render_floating_field(object, attribute, input_id, &block)
  else
    render_default_field(object, attribute, input_id, &block)
  end
end