fix(textarea): fix character limit option

This commit is contained in:
Paul Chavard 2023-04-28 23:28:13 +02:00
parent 471da7eb61
commit fe041b4c6c
11 changed files with 70 additions and 48 deletions

View file

@ -1,7 +1,3 @@
- character_limit = @champ.textarea_character_limit.to_i if !@champ.textarea_character_limit&.empty?
- character_count = @champ.character_count(@champ.value)
- analyze_character_count = @champ.analyze_character_count(character_count, character_limit)
~ @form.text_area :value, ~ @form.text_area :value,
id: @champ.input_id, id: @champ.input_id,
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
@ -9,12 +5,10 @@
required: @champ.required?, required: @champ.required?,
value: html_to_string(@champ.value) value: html_to_string(@champ.value)
- if @champ.textarea_character_limit? - if @champ.character_limit_info?
%span.fr-icon-information-fill.fr-text-default--info.characters-count
= t('.remaining_characters', remaining_words: @champ.remaining_characters)
- if analyze_character_count == :info - if @champ.character_limit_warning?
%span.fr-icon-information-fill.fr-text-default--info.characters-count %span.fr-icon-close-circle-fill.fr-text-default--error.characters-count
= t('.remaining_characters', remaining_words: @champ.remaining_characters(character_count, character_limit)) = t('.excess_characters', excess_words: @champ.excess_characters)
- if analyze_character_count == :warning
%span.fr-icon-close-circle-fill.fr-text-default--error.characters-count
= t('.excess_characters', excess_words: @champ.excess_characters(character_count, character_limit))

View file

@ -36,8 +36,8 @@ fr:
add_condition: Une condition a été ajoutée sur le champ « %{label} ». La nouvelle condition est « %{to} ». add_condition: Une condition a été ajoutée sur le champ « %{label} ». La nouvelle condition est « %{to} ».
remove_condition: La condition du champ « %{label} » a été supprimée. remove_condition: La condition du champ « %{label} » a été supprimée.
update_condition: La condition du champ « %{label} » a été modifiée. La nouvelle condition est « %{to} ». update_condition: La condition du champ « %{label} » a été modifiée. La nouvelle condition est « %{to} ».
update_textarea_character_limit: La limite de caractères du champ texte « %{label} » a été modifiée. La nouvelle limite est « %{to} ». update_character_limit: La limite de caractères du champ texte « %{label} » a été modifiée. La nouvelle limite est « %{to} ».
remove_textarea_character_limit: La limite de caractères du champ texte « %{label} » a été supprimée. remove_character_limit: La limite de caractères du champ texte « %{label} » a été supprimée.
private: private:
add: Lannotation privée « %{label} » a été ajoutée. add: Lannotation privée « %{label} » a été ajoutée.
remove: Lannotation privée « %{label} » a été supprimée. remove: Lannotation privée « %{label} » a été supprimée.

View file

@ -131,13 +131,13 @@
- if !total_dossiers.zero? && !change.can_rebase? - if !total_dossiers.zero? && !change.can_rebase?
.fr-alert.fr-alert--warning.fr-mt-1v .fr-alert.fr-alert--warning.fr-mt-1v
%p= t('.breakigng_change', count: total_dossiers) %p= t('.breakigng_change', count: total_dossiers)
- when :textarea_character_limit - when :character_limit
- if change.to.nil? - if change.to.blank?
- list.with_item do - list.with_item do
= t(".#{prefix}.remove_textarea_character_limit", label: change.label, to: change.to) = t(".#{prefix}.remove_character_limit", label: change.label, to: change.to)
- else - else
- list.with_item do - list.with_item do
= t(".#{prefix}.update_textarea_character_limit", label: change.label, to: change.to) = t(".#{prefix}.update_character_limit", label: change.label, to: change.to)
- if @public_move_changes.present? - if @public_move_changes.present?
- list.with_item do - list.with_item do

View file

@ -121,4 +121,14 @@ class TypesDeChampEditor::ChampComponent < ApplicationComponent
def conditional_enabled? def conditional_enabled?
!type_de_champ.private? !type_de_champ.private?
end end
def options_for_character_limit
[
[t('.character_limit.unlimited'), nil],
[t('.character_limit.limit', limit: 400), 400],
[t('.character_limit.limit', limit: 1000), 1000],
[t('.character_limit.limit', limit: 5000), 5000],
[t('.character_limit.limit', limit: 10000), 10000]
]
end
end end

View file

@ -10,3 +10,6 @@ fr:
natura_2000: Natura 2000 natura_2000: Natura 2000
zones_humides: Zones humides dimportance internationale zones_humides: Zones humides dimportance internationale
znieff: ZNIEFF znieff: ZNIEFF
character_limit:
unlimited: Pas de limite de charactères
limit: Limité à %{limit} caractères

View file

@ -103,9 +103,9 @@
= form.text_area :collapsible_explanation_text, class: "small-margin small", id: dom_id(type_de_champ, :collapsible_explanation_text) = form.text_area :collapsible_explanation_text, class: "small-margin small", id: dom_id(type_de_champ, :collapsible_explanation_text)
- if type_de_champ.textarea? - if type_de_champ.textarea?
.cell .cell
= form.label :textarea_character_limit, for: dom_id(type_de_champ, :textarea_character_limit) do = form.label :character_limit, for: dom_id(type_de_champ, :character_limit) do
Spécifier un nombre maximal de caractères (non restrictif) : Spécifier un nombre maximal de caractères (non restrictif) :
= form.number_field :textarea_character_limit, min: 400, value: form.object.textarea_character_limit, id: dom_id(type_de_champ, :textarea_character_limit) = form.select :character_limit, options_for_character_limit, id: dom_id(type_de_champ, :character_limit)
- if type_de_champ.block? - if type_de_champ.block?
.flex.justify-start.section.ml-1 .flex.justify-start.section.ml-1

View file

@ -129,7 +129,7 @@ module Administrateurs
:collapsible_explanation_enabled, :collapsible_explanation_enabled,
:collapsible_explanation_text, :collapsible_explanation_text,
:header_section_level, :header_section_level,
:textarea_character_limit, :character_limit,
editable_options: [ editable_options: [
:cadastres, :cadastres,
:unesco, :unesco,

View file

@ -75,8 +75,8 @@ class Champ < ApplicationRecord
:mandatory?, :mandatory?,
:prefillable?, :prefillable?,
:refresh_after_update?, :refresh_after_update?,
:textarea_character_limit?, :character_limit?,
:textarea_character_limit, :character_limit,
to: :type_de_champ to: :type_de_champ
delegate :to_typed_id, :to_typed_id_for_query, to: :type_de_champ, prefix: true delegate :to_typed_id, :to_typed_id_for_query, to: :type_de_champ, prefix: true

View file

@ -25,28 +25,43 @@ class Champs::TextareaChamp < Champs::TextChamp
value.present? ? ActionView::Base.full_sanitizer.sanitize(value) : nil value.present? ? ActionView::Base.full_sanitizer.sanitize(value) : nil
end end
def character_count(text) def remaining_characters
return text&.bytesize character_limit_base - character_count if character_count >= character_limit_threshold_75
end end
def analyze_character_count(characters, limit) def excess_characters
if characters character_count - character_limit_base if character_count > character_limit_base
threshold_75 = limit * 0.75 end
if characters >= limit def character_limit_info?
analyze_character_count == :info
end
def character_limit_warning?
analyze_character_count == :warning
end
private
def character_count
return value&.bytesize
end
def character_limit_base
character_limit&.to_i
end
def character_limit_threshold_75
character_limit_base * 0.75
end
def analyze_character_count
if character_limit? && character_count.present?
if character_count >= character_limit_base
return :warning return :warning
elsif characters >= threshold_75 elsif character_count >= character_limit_threshold_75
return :info return :info
end end
end end
end end
def remaining_characters(characters, limit)
threshold_75 = limit * 0.75
limit - characters if characters >= threshold_75
end
def excess_characters(characters, limit)
characters - limit if characters > limit
end
end end

View file

@ -378,11 +378,11 @@ class ProcedureRevision < ApplicationRecord
to_type_de_champ.piece_justificative_template_filename) to_type_de_champ.piece_justificative_template_filename)
end end
elsif to_type_de_champ.textarea? elsif to_type_de_champ.textarea?
if from_type_de_champ.textarea_character_limit != to_type_de_champ.textarea_character_limit if from_type_de_champ.character_limit != to_type_de_champ.character_limit
changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ, changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ,
:textarea_character_limit, :character_limit,
from_type_de_champ.textarea_character_limit, from_type_de_champ.character_limit,
to_type_de_champ.textarea_character_limit) to_type_de_champ.character_limit)
end end
end end
changes changes

View file

@ -119,7 +119,7 @@ class TypeDeChamp < ApplicationRecord
:drop_down_secondary_libelle, :drop_down_secondary_libelle,
:drop_down_secondary_description, :drop_down_secondary_description,
:drop_down_other, :drop_down_other,
:textarea_character_limit, :character_limit,
:collapsible_explanation_enabled, :collapsible_explanation_enabled,
:collapsible_explanation_text, :collapsible_explanation_text,
:header_section_level :header_section_level
@ -179,10 +179,10 @@ class TypeDeChamp < ApplicationRecord
validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :type_champ, presence: true, allow_blank: false, allow_nil: false validates :type_champ, presence: true, allow_blank: false, allow_nil: false
validates :textarea_character_limit, numericality: { validates :character_limit, numericality: {
greater_than_or_equal_to: MINIMUM_TEXTAREA_CHARACTER_LIMIT_LENGTH, greater_than_or_equal_to: MINIMUM_TEXTAREA_CHARACTER_LIMIT_LENGTH,
only_integer: true, only_integer: true,
allow_nil: true allow_blank: true
} }
before_validation :check_mandatory before_validation :check_mandatory
@ -242,8 +242,8 @@ class TypeDeChamp < ApplicationRecord
drop_down_other == "1" || drop_down_other == true drop_down_other == "1" || drop_down_other == true
end end
def textarea_character_limit? def character_limit?
textarea_character_limit.present? character_limit.present?
end end
def collapsible_explanation_enabled? def collapsible_explanation_enabled?