feat(champ): customizable placeholders for text & textarea champs

Closes #7429
This commit is contained in:
Colin Darie 2022-07-07 11:23:38 +02:00
parent e26cee8d9d
commit 9e26ee932e
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
9 changed files with 81 additions and 4 deletions

View file

@ -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

View file

@ -68,6 +68,7 @@ module Administrateurs
:drop_down_secondary_libelle,
:drop_down_secondary_description,
:piece_justificative_template,
:placeholder,
editable_options: [
:cadastres,
:unesco,

View file

@ -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

View file

@ -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 }

View file

@ -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")

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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