From 9e26ee932e0886f1f9178751f30ac02eabf6cff6 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 7 Jul 2022 11:23:38 +0200 Subject: [PATCH] feat(champ): customizable placeholders for text & textarea champs Closes #7429 --- .../champ_component/champ_component.html.haml | 7 +++++ .../types_de_champ_controller.rb | 1 + app/models/type_de_champ.rb | 15 ++++++++- .../dossiers/editable_champs/_text.html.haml | 2 +- .../editable_champs/_textarea.html.haml | 2 +- config/locales/fr.yml | 6 ++++ .../types_de_champ_controller_spec.rb | 3 +- .../administrateurs/types_de_champ_spec.rb | 31 +++++++++++++++++++ .../shared/dossiers/_edit.html.haml_spec.rb | 18 +++++++++++ 9 files changed, 81 insertions(+), 4 deletions(-) diff --git a/app/components/types_de_champ_editor/champ_component/champ_component.html.haml b/app/components/types_de_champ_editor/champ_component/champ_component.html.haml index 536d9c9d1..b1977ebed 100644 --- a/app/components/types_de_champ_editor/champ_component/champ_component.html.haml +++ b/app/components/types_de_champ_editor/champ_component/champ_component.html.haml @@ -34,6 +34,13 @@ = form.label :description, "Description du champ (optionnel)", for: dom_id(type_de_champ, :description) = form.text_area :description, class: 'small-margin small width-100', rows: 3, id: dom_id(type_de_champ, :description) + - if type_de_champ.generic? + .cell.mt-1 + = form.label :placeholder, t(".generic.custom_placeholder_title"), class: 'flex-grow', for: dom_id(type_de_champ, :placeholder) + = form.text_field :placeholder, class: 'small-margin small width-100', placeholder: t("#{type_de_champ.type_champ}.placeholder", scope: "shared.dossiers.editable_champs"), id: dom_id(type_de_champ, :placeholder) + %p + = t(".generic.custom_placeholder_hint") + .flex.justify-start.mt-1 - if type_de_champ.drop_down_list? .flex.column.justify-start.width-33 diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index 1f56605a9..bfe2b3f9f 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -68,6 +68,7 @@ module Administrateurs :drop_down_secondary_libelle, :drop_down_secondary_description, :piece_justificative_template, + :placeholder, editable_options: [ :cadastres, :unesco, diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index c771340a9..851d0f7e9 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -58,7 +58,16 @@ class TypeDeChamp < ApplicationRecord mesri: 'mesri' } - store_accessor :options, :cadastres, :old_pj, :drop_down_options, :skip_pj_validation, :skip_content_type_pj_validation, :drop_down_secondary_libelle, :drop_down_secondary_description, :drop_down_other + store_accessor :options, :cadastres, + :drop_down_options, + :drop_down_other, + :drop_down_secondary_description, + :drop_down_secondary_libelle, + :old_pj, + :placeholder, + :skip_content_type_pj_validation, + :skip_pj_validation + has_many :revision_types_de_champ, -> { revision_ordered }, class_name: 'ProcedureRevisionTypeDeChamp', dependent: :destroy, inverse_of: :type_de_champ has_one :revision_type_de_champ, -> { revision_ordered }, class_name: 'ProcedureRevisionTypeDeChamp', inverse_of: false has_many :revisions, -> { ordered }, through: :revision_types_de_champ @@ -217,6 +226,10 @@ class TypeDeChamp < ApplicationRecord type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list) end + def generic? + type_champ == TypeDeChamp.type_champs.fetch(:text) || type_champ == TypeDeChamp.type_champs.fetch(:textarea) + end + def exclude_from_view? type_champ == TypeDeChamp.type_champs.fetch(:explication) end diff --git a/app/views/shared/dossiers/editable_champs/_text.html.haml b/app/views/shared/dossiers/editable_champs/_text.html.haml index 0408b7bad..c05eac8f6 100644 --- a/app/views/shared/dossiers/editable_champs/_text.html.haml +++ b/app/views/shared/dossiers/editable_champs/_text.html.haml @@ -1,5 +1,5 @@ = form.text_field :value, id: champ.input_id, - placeholder: t(".placeholder"), + placeholder: champ.type_de_champ.placeholder.presence || t(".placeholder"), required: champ.mandatory?, aria: { describedby: champ.describedby_id } diff --git a/app/views/shared/dossiers/editable_champs/_textarea.html.haml b/app/views/shared/dossiers/editable_champs/_textarea.html.haml index 2d666a10a..894a3ddbe 100644 --- a/app/views/shared/dossiers/editable_champs/_textarea.html.haml +++ b/app/views/shared/dossiers/editable_champs/_textarea.html.haml @@ -4,4 +4,4 @@ rows: 6, required: champ.mandatory?, value: html_to_string(champ.value), - placeholder: t(".placeholder") + placeholder: champ.type_de_champ.placeholder.presence || t(".placeholder") diff --git a/config/locales/fr.yml b/config/locales/fr.yml index b83bf48fd..8e74c0884 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -575,3 +575,9 @@ fr: weekly_distribution_details: "au cours des 6 derniers mois" procedure_description: estimated_fill_duration: "Temps de remplissage estimé : %{estimated_minutes} mn" + + types_de_champ_editor: + champ_component: + generic: + custom_placeholder_title: "Spécimen de saisie (optionnel)" + custom_placeholder_hint: Modèle de réponse visible dans le champ avant la saisie par l'usager diff --git a/spec/controllers/administrateurs/types_de_champ_controller_spec.rb b/spec/controllers/administrateurs/types_de_champ_controller_spec.rb index 6e60e9cf7..f4b95fae5 100644 --- a/spec/controllers/administrateurs/types_de_champ_controller_spec.rb +++ b/spec/controllers/administrateurs/types_de_champ_controller_spec.rb @@ -18,7 +18,8 @@ describe Administrateurs::TypesDeChampController, type: :controller do type_de_champ: { type_champ: type_champ, libelle: 'Nouveau champ', - private: false + private: false, + placeholder: "custom placeholder" } }, format: :turbo_stream end diff --git a/spec/system/administrateurs/types_de_champ_spec.rb b/spec/system/administrateurs/types_de_champ_spec.rb index 7c9da9b56..9c1fa6d19 100644 --- a/spec/system/administrateurs/types_de_champ_spec.rb +++ b/spec/system/administrateurs/types_de_champ_spec.rb @@ -165,4 +165,35 @@ describe 'As an administrateur I can edit types de champ', js: true do end expect(page).not_to have_content('Durée de remplissage estimée') end + + describe "placeholders for generic types" do + let(:placeholder) { "my placeholder" } + before do + add_champ + end + + it "text champ" do + select('Texte', from: 'Type de champ') + expect(page).to have_content('Spécimen de saisie') + + fill_in 'Spécimen de saisie', with: placeholder + + wait_until { procedure.draft_types_de_champ.first.placeholder == placeholder } + + page.refresh + expect(page).to have_selector("input[value='#{placeholder}']") + end + + it "textarea champ" do + select('Zone de texte', from: 'Type de champ') + expect(page).to have_content('Spécimen de saisie') + + fill_in 'Spécimen de saisie', with: placeholder + + wait_until { procedure.draft_types_de_champ.first.placeholder == placeholder } + + page.refresh + expect(page).to have_selector("input[value='#{placeholder}']") + end + end end diff --git a/spec/views/shared/dossiers/_edit.html.haml_spec.rb b/spec/views/shared/dossiers/_edit.html.haml_spec.rb index fa358473f..0e0903d4e 100644 --- a/spec/views/shared/dossiers/_edit.html.haml_spec.rb +++ b/spec/views/shared/dossiers/_edit.html.haml_spec.rb @@ -34,6 +34,24 @@ describe 'shared/dossiers/edit.html.haml', type: :view do expect(subject).to have_css('input[type="tel"][placeholder^="0612"]') end end + + context "with generic champs" do + let(:champ_text) { create(:champ_text, dossier: dossier) } + let(:champs) { [champ_text, champ_textarea] } + + it "renders default placeholders" do + expect(subject).to have_css('input[placeholder*="réponse"]') + expect(subject).to have_css('textarea[placeholder*="réponse"]') + end + + it "renders customized placeholders" do + champ_text.type_de_champ.placeholder = "custom1 placeholder" + champ_textarea.type_de_champ.placeholder = "custom2 placeholder" + + expect(subject).to have_css('input[placeholder*="custom1"]') + expect(subject).to have_css('textarea[placeholder*="custom2"]') + end + end end context 'with a single-value list' do