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

Methods included from LocoMotion::Concerns::InspectableComponent

#build_inspect_string

Constructor Details

#initialize(**kws) ⇒ FilterComponent

Initialize a new filter component.

Parameters:

  • kws (Hash)

    The keyword arguments for the 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).



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

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.



114
115
116
# File 'app/components/daisy/data_input/filter_component.rb', line 114

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



114
115
116
# File 'app/components/daisy/data_input/filter_component.rb', line 114

def name
  @name
end

Instance Method Details

#before_renderObject

Setup the component before rendering.



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

def before_render
  super

  setup_component
end

#default_reset_buttonObject



146
147
148
# File 'app/components/daisy/data_input/filter_component.rb', line 146

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.

Returns:

  • (String)

    The HTML for all options in the filter.



183
184
185
186
187
188
189
190
191
192
193
# File 'app/components/daisy/data_input/filter_component.rb', line 183

def render_filter_options
  result = +""

  if options?
    options.each { |option| result << render(option) }
  elsif standard_options.present?
    standard_options.each { |option| result << render(option) }
  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.

Returns:



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

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