From 79a65a48476c8bbf8c42d76b95bb6e390acdb2b3 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 6 May 2024 18:09:10 +0200 Subject: [PATCH] refactor(champ): update champ address --- app/components/editable_champ/address_component.rb | 11 +++++++++++ .../address_component/address_component.html.haml | 6 +++--- app/models/champs/address_champ.rb | 12 ++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/components/editable_champ/address_component.rb b/app/components/editable_champ/address_component.rb index 0bcefb5e0..f0c7a8d75 100644 --- a/app/components/editable_champ/address_component.rb +++ b/app/components/editable_champ/address_component.rb @@ -2,4 +2,15 @@ class EditableChamp::AddressComponent < EditableChamp::EditableChampBaseComponen def dsfr_input_classname 'fr-select' end + + def react_props + react_input_opts(id: @champ.input_id, + class: 'fr-mt-1w', + name: @form.field_name(:value), + selected_key: @champ.value, + items: @champ.selected_items, + loader: data_sources_data_source_adresse_path, + minimum_input_length: 2, + allows_custom_value: true) + end end diff --git a/app/components/editable_champ/address_component/address_component.html.haml b/app/components/editable_champ/address_component/address_component.html.haml index e1029f05a..6df764db4 100644 --- a/app/components/editable_champ/address_component/address_component.html.haml +++ b/app/components/editable_champ/address_component/address_component.html.haml @@ -1,3 +1,3 @@ -= render Dsfr::ComboboxComponent.new form: @form, url: data_sources_data_source_adresse_path, selected: @champ.value, allows_custom_value: true, input_html_options: { name: :value, id: @champ.input_id, class: 'fr-select', describedby: @champ.describedby_id } do - = @form.hidden_field :external_id, data: { value_slot: 'value' } - = @form.hidden_field :feature, data: { value_slot: 'data' } +%react-fragment + = render ReactComponent.new "ComboBox/RemoteComboBox", **react_props do + = render ReactComponent.new "ComboBox/ComboBoxValueSlot", field: :data, name: @form.field_name(:feature) diff --git a/app/models/champs/address_champ.rb b/app/models/champs/address_champ.rb index 1c4d9e78f..c07ced241 100644 --- a/app/models/champs/address_champ.rb +++ b/app/models/champs/address_champ.rb @@ -3,10 +3,6 @@ class Champs::AddressChamp < Champs::TextChamp data.present? end - def feature - data.to_json if full_address? - end - def feature=(value) if value.blank? self.data = nil @@ -22,6 +18,14 @@ class Champs::AddressChamp < Champs::TextChamp self.data = nil end + def selected_items + if value.present? + [{ value:, label: value, data: full_address? ? data : nil }] + else + [] + end + end + def address full_address? ? data : nil end