2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-05-05 09:13:05 +02:00
|
|
|
module Dsfr
|
|
|
|
module InputErrorable
|
|
|
|
extend ActiveSupport::Concern
|
2023-07-31 15:18:31 +02:00
|
|
|
|
2023-05-05 09:13:05 +02:00
|
|
|
included do
|
|
|
|
delegate :object, to: :@form
|
|
|
|
delegate :errors, to: :object
|
|
|
|
|
2023-07-31 15:18:31 +02:00
|
|
|
renders_one :hint
|
|
|
|
|
2023-08-21 16:18:31 +02:00
|
|
|
def dsfr_group_classname
|
|
|
|
if dsfr_champ_container == :fieldset
|
|
|
|
'fr-fieldset'
|
2023-09-28 17:36:45 +02:00
|
|
|
elsif dsfr_input_classname.present? # non fillable element
|
2023-08-21 16:18:31 +02:00
|
|
|
"#{dsfr_input_classname}-group"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def input_group_error_class_names
|
2023-09-28 17:36:45 +02:00
|
|
|
return {} if dsfr_group_classname.nil?
|
|
|
|
|
2023-08-21 16:18:31 +02:00
|
|
|
{
|
|
|
|
"#{dsfr_group_classname}--error" => errors_on_attribute?,
|
2024-10-07 18:10:37 +02:00
|
|
|
"#{dsfr_group_classname}--valid" => !errors_on_attribute? && errors_on_another_attribute? && object.public_send(attribute).present?
|
2023-08-21 16:18:31 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors_on_attribute?
|
|
|
|
errors.has_key?(attribute_or_rich_body)
|
|
|
|
end
|
|
|
|
|
|
|
|
# errors helpers
|
|
|
|
def error_full_messages
|
|
|
|
errors.full_messages_for(attribute_or_rich_body)
|
|
|
|
end
|
|
|
|
|
2024-04-09 16:19:54 +02:00
|
|
|
def fieldset_error_opts
|
|
|
|
if dsfr_champ_container == :fieldset && errors_on_attribute?
|
|
|
|
{ aria: { labelledby: "#{describedby_id} #{object.labelledby_id}" } }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-05 09:13:05 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
# lookup for edge case from `form.rich_text_area`
|
|
|
|
# rich text uses _rich_#{attribute}, but it is saved on #{attribute}, as well as error messages
|
|
|
|
def attribute_or_rich_body
|
|
|
|
case @input_type
|
|
|
|
when :rich_text_area
|
2024-10-07 18:10:37 +02:00
|
|
|
attribute.to_s.sub(/\Arich_/, '').to_sym
|
2023-05-05 09:13:05 +02:00
|
|
|
else
|
2024-10-07 18:10:37 +02:00
|
|
|
attribute
|
2023-05-05 09:13:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-17 17:26:49 +02:00
|
|
|
def fr_fieldset?
|
2023-08-21 16:18:31 +02:00
|
|
|
!['fr-input', 'fr-radio', 'fr-select'].include?(dsfr_input_classname)
|
2023-08-17 17:26:49 +02:00
|
|
|
end
|
|
|
|
|
2023-05-05 09:13:05 +02:00
|
|
|
def input_error_class_names
|
2023-08-17 17:26:49 +02:00
|
|
|
{
|
|
|
|
"#{dsfr_input_classname}--error": errors_on_attribute?
|
|
|
|
}
|
2023-05-05 09:13:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def input_error_opts
|
|
|
|
{
|
|
|
|
aria: {
|
2024-04-09 16:43:44 +02:00
|
|
|
describedby: describedby_id
|
2023-05-05 09:13:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-05-06 18:08:26 +02:00
|
|
|
def react_input_opts(other_opts = {})
|
|
|
|
input_opts(other_opts, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def input_opts(other_opts = {}, react = false)
|
2023-05-05 09:13:05 +02:00
|
|
|
@opts = @opts.deep_merge!(other_opts)
|
2024-05-06 18:08:26 +02:00
|
|
|
@opts[react ? :class_name : :class] = class_names(map_array_to_hash_with_true(@opts[:class])
|
2023-05-05 09:13:05 +02:00
|
|
|
.merge({
|
|
|
|
'fr-password__input': password?,
|
2024-05-06 18:08:26 +02:00
|
|
|
'fr-input': !react,
|
2023-05-05 09:13:05 +02:00
|
|
|
'fr-mb-0': true
|
|
|
|
}.merge(input_error_class_names)))
|
|
|
|
if errors_on_attribute?
|
2024-05-06 18:08:26 +02:00
|
|
|
@opts.deep_merge!('aria-describedby': describedby_id)
|
2024-04-09 17:36:40 +02:00
|
|
|
elsif hintable?
|
2024-05-06 18:08:26 +02:00
|
|
|
@opts.deep_merge!('aria-describedby': hint_id)
|
2023-05-05 09:13:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if @required
|
2024-05-06 18:08:26 +02:00
|
|
|
@opts[react ? :is_required : :required] = true
|
2023-05-05 09:13:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if email?
|
|
|
|
@opts.deep_merge!(data: {
|
|
|
|
action: "blur->email-input#checkEmail",
|
|
|
|
'email-input-target': 'input'
|
|
|
|
})
|
|
|
|
end
|
2024-03-07 12:06:00 +01:00
|
|
|
|
2024-06-19 21:50:38 +02:00
|
|
|
@opts.deep_merge!(data: { controller: token_list(@opts.dig(:data, :controller), 'autoresize' => autoresize?) })
|
2024-03-07 12:06:00 +01:00
|
|
|
|
2023-05-05 09:13:05 +02:00
|
|
|
@opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors_on_another_attribute?
|
|
|
|
!errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def map_array_to_hash_with_true(array_or_string_or_nil)
|
|
|
|
Array(array_or_string_or_nil).to_h { [_1, true] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def hint
|
2023-07-31 15:18:31 +02:00
|
|
|
get_slot(:hint).presence || default_hint
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_hint
|
2024-04-11 11:34:06 +02:00
|
|
|
if I18n.exists?("activerecord.attributes.#{object.class.name.underscore}.hints.#{@attribute}")
|
|
|
|
I18n.t("activerecord.attributes.#{object.class.name.underscore}.hints.#{@attribute}")
|
|
|
|
elsif I18n.exists?("activerecord.attributes.#{object.class.name.underscore}.hints.#{@attribute}_html")
|
|
|
|
I18n.t("activerecord.attributes.#{object.class.name.underscore}.hints.#{@attribute}_html").html_safe
|
|
|
|
end
|
2023-05-05 09:13:05 +02:00
|
|
|
end
|
|
|
|
|
2024-07-24 17:14:06 +02:00
|
|
|
def hint? = hint.present?
|
|
|
|
|
2023-05-05 09:13:05 +02:00
|
|
|
def password?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def email?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2024-03-07 12:06:00 +01:00
|
|
|
def autoresize?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2024-04-09 17:36:40 +02:00
|
|
|
def hintable?
|
|
|
|
false
|
|
|
|
end
|
2023-05-05 09:13:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|