Module: ShadcnPhlexcomponents::FormHelpers

Includes:
Phlex::Rails::Helpers::FieldID, Phlex::Rails::Helpers::FieldName, AliasedLabel
Included in:
FormCheckbox, FormCheckboxGroup, FormCombobox, FormDatePicker, FormDateRangePicker, FormInput, FormRadioGroup, FormSelect, FormSlider, FormSwitch, FormTextarea
Defined in:
lib/shadcn_phlexcomponents/components/form/form_helpers.rb

Defined Under Namespace

Modules: AliasedLabel

Instance Method Summary collapse

Instance Method Details

#aria_attributes ⇒ Object



101
102
103
104
105
106
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 101

def aria_attributes
  {
    describedby: describedby,
    invalid: @error.present?.to_s,
  }
end

#default_checked(checked, method) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 126

def default_checked(checked, method)
  return checked if [true, false].include?(checked)
  return unless @model

  if @model.respond_to?(method)
    !!@model.public_send(method)
  end
end

#default_error(error, method) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 135

def default_error(error, method)
  return error unless error.nil?
  return unless @model

  if @model.respond_to?(:errors)
    @model.errors.full_messages_for(method).first
  end
end

#default_value(value, method) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 117

def default_value(value, method)
  return value unless value.nil?
  return unless @model

  if @model.respond_to?(method)
    @model.public_send(method)
  end
end

#describedby ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 108

def describedby
  return if !@hint && !@error.present?

  [
    @hint ? "#{@aria_id}-description" : nil,
    @error.present? ? "#{@aria_id}-message" : nil,
  ].compact.join(" ")
end

#hint(text = nil, **attributes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 26

def hint(text = nil, **attributes, &)
  @yield_hint = true
  @hint = true
  attrs = hint_attributes(**attributes)

  if text
    FormHint(text, aria_id: @aria_id, **attrs)
  else
    FormHint(aria_id: @aria_id, **attrs, &)
  end
end

#hint_attributes(**attributes) ⇒ Object



91
92
93
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 91

def hint_attributes(**attributes)
  attributes
end

#label(text = nil, **attributes) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 15

def label(text = nil, **attributes, &)
  @yield_label = true
  attrs = label_attributes(use_label_styles: false, **attributes)

  if text
    Label(**attrs) { text }
  else
    Label(**attrs, &)
  end
end

#label_and_hint_container_attributes ⇒ Object



95
96
97
98
99
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 95

def label_and_hint_container_attributes
  {
    controller: @yield_label && @yield_hint ? "form-field" : nil,
  }.compact
end

#label_attributes(use_label_styles: false, **attributes) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 81

def label_attributes(use_label_styles: false, **attributes)
  attributes[:class] = [
    use_label_styles ? Label.new.class_variants : nil,
    @error.present? ? "text-destructive" : nil,
    attributes[:class],
  ].compact.join(" ")
  attributes[:for] ||= @id
  attributes
end

#render_error ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 67

def render_error
  if @error.present?
    if @error.is_a?(Array)
      FormError(nil, aria_id: @aria_id, class: "space-y-0.5") do
        @error.each do |error|
          span(class: "block") { error }
        end
      end
    else
      FormError(@error, aria_id: @aria_id)
    end
  end
end

#render_hint ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 54

def render_hint(&)
  # It's currently not possible to separate the content of the yield in Phlex.
  # So we use Javascript to remove the duplicated hint or label.
  if @yield_label && @yield_hint
    div(data: { remove_label: true }, &)
  elsif @yield_hint
    yield
  elsif @hint
    attrs = hint_attributes
    FormHint(@hint, aria_id: @aria_id, **attrs)
  end
end

#render_label ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/shadcn_phlexcomponents/components/form/form_helpers.rb', line 38

def render_label(&)
  # It's currently not possible to separate the content of the yield in Phlex.
  # So we use Javascript to remove the duplicated hint or label.
  if @yield_label && @yield_hint
    div(data: { remove_hint: true }, &)
  elsif @yield_label
    yield
  elsif @label
    attrs = label_attributes(use_label_styles: false)
    Label(**attrs) { @label }
  elsif @label != false
    attrs = label_attributes(use_label_styles: true)
    rails_label(@object_name, @method, nil, **attrs)
  end
end