Class: Daisy::DataInput::FilterComponent

Inherits:
LocoMotion::BaseComponent show all
Includes:
ViewComponent::SlotableDefault
Defined in:
app/components/daisy/data_input/filter_component.rb

Defined Under Namespace

Classes: FilterOptionComponent, FilterResetComponent

Constant Summary

Constants inherited from LocoMotion::BaseComponent

LocoMotion::BaseComponent::EMPTY_PART_IGNORED_TAGS, LocoMotion::BaseComponent::SELF_CLOSING_TAGS

Instance Attribute Summary collapse

Attributes inherited from LocoMotion::BaseComponent

#config, #loco_parent

Instance Method Summary collapse

Methods inherited from LocoMotion::BaseComponent

build, #component_ref, #config_option, #cssify, define_modifier, define_modifiers, define_part, define_parts, define_size, define_sizes, #empty_part_content, #inspect, #part, register_component_initializer, register_component_setup, #rendered_css, #rendered_data, #rendered_html, #rendered_stimulus_controllers, #rendered_tag_name, renders_many, renders_one, set_component_name, #set_loco_parent, #strip_spaces

Constructor Details

#initialize(**kws) ⇒ FilterComponent

Initialize a new filter component.

Options Hash (**kws):

  • name (String)

    Required name attribute for the radio button group.

  • options (Array)

    An array of options to display in the filter. Can be an array of strings, symbols, or hashes with :label keys.

  • value (String)

    The current value of the filter (for form integration).



130
131
132
133
134
135
136
137
# File 'app/components/daisy/data_input/filter_component.rb', line 130

def initialize(**kws)
  super(**kws)

  @name = config_option(:name)
  @id = config_option(:id, SecureRandom.uuid)
  @options_list = config_option(:options)
  @value = config_option(:value)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



116
117
118
# File 'app/components/daisy/data_input/filter_component.rb', line 116

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



116
117
118
# File 'app/components/daisy/data_input/filter_component.rb', line 116

def name
  @name
end

Instance Method Details

#before_renderObject

Setup the component before rendering.



142
143
144
145
146
# File 'app/components/daisy/data_input/filter_component.rb', line 142

def before_render
  super

  setup_component
end

#default_reset_buttonObject



148
149
150
# File 'app/components/daisy/data_input/filter_component.rb', line 148

def default_reset_button
  FilterResetComponent.new(name: @name)
end

#render_filter_optionsString

Renders the filter options based on the configuration. This method is used by the template to render options consistently.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/components/daisy/data_input/filter_component.rb', line 185

def render_filter_options
  result = ""

  if options?
    options.each do |option|
      result += render(option)
    end
  elsif standard_options.present?
    standard_options.each do |option|
      result += render(option)
    end
  end

  result.html_safe
end

#standard_optionsArray<FilterOptionComponent>

Converts the options array into FilterOptionComponent instances. Handles both hash options (with label keys) and simple string/symbol options.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/components/daisy/data_input/filter_component.rb', line 158

def standard_options
  return [] unless options_list

  options_list.map.with_index do |option, index|
    label = option.is_a?(Hash) ? option[:label] : option.to_s
    value = option.is_a?(Hash) ? option[:value] : option

    # Check if this option should be selected based on the component's value
    checked = @value.present? && @value.to_s == value.to_s

    Daisy::DataInput::FilterComponent::FilterOptionComponent.new(
      loco_parent: component_ref,
      name: @name,
      label: label,
      value: value,
      checked: checked,
      index: index.to_s  # Ensure index is a string
    )
  end
end