Skip to content

Commit

Permalink
refactor select input component template to use the rails helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilelkihal authored and syphax-bouazzouni committed Aug 6, 2023
1 parent 205bb52 commit 591e0db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
30 changes: 23 additions & 7 deletions app/components/select_input_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,35 @@ class SelectInputComponent < ViewComponent::Base

def initialize(id:, name:, values:, selected:, multiple: false, open_to_add_values: true)
super
@id = id
@id = id || ""
@name = name
@values = values
@selected = selected
@multiple = multiple
@open_to_add_values = open_to_add_values
end

def call
select_input_tag(@id, @values, @selected, multiple: @multiple, open_to_add_values: @open_to_add_values)
end

private

def select_input_tag(id, values, selected, options = {})
multiple = options[:multiple] || false
open_to_add_values = options[:open_to_add_values] || false

def options_values
if @selected.nil? || @selected.empty?
@selected = 0
@values.unshift('')
end
options_for_select(@values, @selected)
select_html_options = {
id: "select_#{id}",
autocomplete: "off",
multiple: multiple,
data: {
controller: "select-input",
'select-input-multiple-value': multiple,
'select-input-open-add-value': open_to_add_values
}
}
#binding.pry
select_tag(id, options_for_select(values, selected), select_html_options)
end
end

This file was deleted.

0 comments on commit 591e0db

Please sign in to comment.