remove enabled_non_empty_options indirection

This commit is contained in:
simon lehericey 2024-09-19 11:40:42 +02:00
parent 8b5f689a67
commit cfb03fc747
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
6 changed files with 14 additions and 22 deletions

View file

@ -23,7 +23,7 @@ class EditableChamp::DropDownListComponent < EditableChamp::EditableChampBaseCom
def contains_long_option?
max_length = 100
@champ.enabled_non_empty_options.any? { _1.size > max_length }
@champ.drop_down_list_enabled_non_empty_options.any? { _1.size > max_length }
end
def react_props
@ -31,7 +31,7 @@ class EditableChamp::DropDownListComponent < EditableChamp::EditableChampBaseCom
class: 'fr-mt-1w',
name: @form.field_name(:value),
selected_key: @champ.selected,
items: @champ.enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1 : [_1, _1] },
items: @champ.drop_down_list_enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1 : [_1, _1] },
empty_filter_key: @champ.drop_down_other? ? Champs::DropDownListChamp::OTHER : nil,
'aria-describedby': @champ.describedby_id,
'aria-labelledby': @champ.labelledby_id)

View file

@ -1,6 +1,6 @@
- if @champ.render_as_radios?
.fr-fieldset__content
- @champ.enabled_non_empty_options.each_with_index do |option, index|
- @champ.drop_down_list_enabled_non_empty_options.each_with_index do |option, index|
.fr-radio-group
= @form.radio_button :value, option, id: dom_id(@champ, "radio_option_#{index}")
%label.fr-label{ for: dom_id(@champ, "radio_option_#{index}") }
@ -22,7 +22,7 @@
= render ReactComponent.new "ComboBox/SingleComboBox", **react_props
- else
= @form.select :value,
@champ.enabled_non_empty_options(other: true),
@champ.drop_down_list_enabled_non_empty_options(other: true),
{ selected: @champ.selected, include_blank: true },
required: @champ.required?,
id: @champ.input_id,

View file

@ -16,7 +16,7 @@ class EditableChamp::MultipleDropDownListComponent < EditableChamp::EditableCham
class: 'fr-mt-1w',
name: @form.field_name(:value, multiple: true),
selected_keys: @champ.selected_options,
items: @champ.enabled_non_empty_options,
items: @champ.drop_down_list_enabled_non_empty_options,
value_separator: false,
'aria-label': @champ.libelle,
'aria-describedby': @champ.describedby_id,

View file

@ -1,5 +1,5 @@
- if @champ.render_as_checkboxes?
= @form.collection_check_boxes :value, @champ.enabled_non_empty_options, :to_s, :to_s do |b|
= @form.collection_check_boxes :value, @champ.drop_down_list_enabled_non_empty_options, :to_s, :to_s do |b|
- capture do
.fr-fieldset__element
.fr-checkbox-group

View file

@ -9,11 +9,11 @@ class Champs::DropDownListChamp < Champ
validate :value_is_in_options, if: -> { !(value.blank? || drop_down_other?) && validate_champ_value_or_prefill? }
def render_as_radios?
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO
drop_down_list_enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO
end
def render_as_combobox?
enabled_non_empty_options.size >= THRESHOLD_NB_OPTIONS_AS_AUTOCOMPLETE
drop_down_list_enabled_non_empty_options.size >= THRESHOLD_NB_OPTIONS_AS_AUTOCOMPLETE
end
def html_label?
@ -28,12 +28,8 @@ class Champs::DropDownListChamp < Champ
other? ? OTHER : value
end
def enabled_non_empty_options(other: false)
drop_down_list_enabled_non_empty_options(other:)
end
def other?
drop_down_other? && (other || (value.present? && enabled_non_empty_options.exclude?(value)))
drop_down_other? && (other || (value.present? && drop_down_list_enabled_non_empty_options.exclude?(value)))
end
def value=(value)
@ -71,7 +67,7 @@ class Champs::DropDownListChamp < Champ
private
def value_is_in_options
return if enabled_non_empty_options.include?(value)
return if drop_down_list_enabled_non_empty_options.include?(value)
errors.add(:value, :not_in_options)
end

View file

@ -3,10 +3,6 @@
class Champs::MultipleDropDownListChamp < Champ
validate :values_are_in_options, if: -> { value.present? && validate_champ_value_or_prefill? }
def enabled_non_empty_options
drop_down_list_enabled_non_empty_options
end
THRESHOLD_NB_OPTIONS_AS_CHECKBOX = 5
def search_terms
@ -18,7 +14,7 @@ class Champs::MultipleDropDownListChamp < Champ
end
def render_as_checkboxes?
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX
drop_down_list_enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX
end
def html_label?
@ -51,7 +47,7 @@ class Champs::MultipleDropDownListChamp < Champ
end
def focusable_input_id
render_as_checkboxes? ? checkbox_id(enabled_non_empty_options.first) : input_id
render_as_checkboxes? ? checkbox_id(drop_down_list_enabled_non_empty_options.first) : input_id
end
def checkbox_id(value)
@ -67,7 +63,7 @@ class Champs::MultipleDropDownListChamp < Champ
end
def unselected_options
enabled_non_empty_options - selected_options
drop_down_list_enabled_non_empty_options - selected_options
end
def value=(value)
@ -97,7 +93,7 @@ class Champs::MultipleDropDownListChamp < Champ
def values_are_in_options
json = selected_options.compact_blank
return if json.empty?
return if (json - enabled_non_empty_options).empty?
return if (json - drop_down_list_enabled_non_empty_options).empty?
errors.add(:value, :not_in_options)
end