Class: Forms::Live::Field

Inherits:
Field
  • Object
show all
Defined in:
lib/forms/live/field.rb

Overview

A Forms::Field that merges the blur-time validate trigger into every input's data attributes (via apply_validations, the seam every builder already routes options through).

Instance Method Summary collapse

Constructor Details

#initialize(live_trigger: nil) ⇒ Field

Returns a new instance of Field.



9
10
11
12
# File 'lib/forms/live/field.rb', line 9

def initialize(live_trigger: nil, **)
  super(**)
  @live_trigger = live_trigger
end

Instance Method Details

#apply_validations(options) ⇒ Object



14
15
16
17
18
19
# File 'lib/forms/live/field.rb', line 14

def apply_validations(options)
  merged = super
  return merged unless @live_trigger

  merged.merge(data: merge_data(merged[:data], @live_trigger[:data]))
end

#tag_field(*modifiers, suggestions: []) ⇒ Object

When THIS field is the form's declared live_tags field, render the ROOTLESS variant: no nested reactive root, so the outer DOM-owns the hidden tags field and live :validate collects it. Its wire attrs were hoisted onto the form root by Forms::Live#form_attributes. Any other tag field falls through to the standard (self-rooted, non-live) widget.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/forms/live/field.rb', line 26

def tag_field(*modifiers, suggestions: [], **)
  declaration = @form.class.respond_to?(:live_tags_declaration) && @form.class.live_tags_declaration
  return super unless declaration && declaration[:name] == @name

  # Call-site suggestions win; otherwise fall back to the declaration's.
  suggestions = declaration[:suggestions] if blank_suggestions?(suggestions)
  theme[:rootless_tag_field].new(
    *modifiers,
    name: field_name, id: field_id, value: field_value,
    suggestions:, error: invalid?, **
  )
end