-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin
committed
Jun 20, 2023
1 parent
2cfa5a9
commit 8047e0e
Showing
35 changed files
with
220 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module Dsfr | ||
module InputErrorable | ||
extend ActiveSupport::Concern | ||
included do | ||
delegate :object, to: :@form | ||
delegate :errors, to: :object | ||
|
||
# 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 | ||
@attribute.to_s.sub(/\Arich_/, '').to_sym | ||
else | ||
@attribute | ||
end | ||
end | ||
|
||
def input_group_error_class_names | ||
{ | ||
"fr-input-group--error": errors_on_attribute?, | ||
"fr-input-group--valid": !errors_on_attribute? && errors_on_another_attribute? | ||
} | ||
end | ||
|
||
def input_error_class_names | ||
{ 'fr-input--error': errors_on_attribute? } | ||
end | ||
|
||
def input_error_opts | ||
{ | ||
aria: { | ||
describedby: describedby_id, | ||
invalid: errors_on_attribute? | ||
} | ||
} | ||
end | ||
|
||
def input_opts(other_opts = {}) | ||
@opts = @opts.deep_merge!(other_opts) | ||
@opts[:class] = class_names(map_array_to_hash_with_true(@opts[:class]) | ||
.merge({ | ||
'fr-password__input': password?, | ||
'fr-input': true, | ||
'fr-mb-0': true | ||
}.merge(input_error_class_names))) | ||
|
||
if errors_on_attribute? | ||
@opts.deep_merge!(aria: { | ||
describedby: describedby_id, | ||
invalid: errors_on_attribute? | ||
}) | ||
end | ||
|
||
if @required | ||
@opts[:required] = true | ||
end | ||
|
||
if email? | ||
@opts.deep_merge!(data: { | ||
action: "blur->email-input#checkEmail", | ||
'email-input-target': 'input' | ||
}) | ||
end | ||
@opts | ||
end | ||
|
||
def describedby_id | ||
dom_id(@champ, :error_messages) | ||
end | ||
|
||
def errors_on_another_attribute? | ||
!errors.empty? | ||
end | ||
|
||
def errors_on_attribute? | ||
errors.has_key?(attribute_or_rich_body) | ||
end | ||
|
||
# errors helpers | ||
def error_messages | ||
errors.full_messages_for(attribute_or_rich_body) | ||
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 | ||
I18n.t("activerecord.attributes.#{object.class.name.underscore}.hints.#{@attribute}") | ||
end | ||
|
||
def password? | ||
false | ||
end | ||
|
||
def email? | ||
false | ||
end | ||
|
||
def hint? | ||
I18n.exists?("activerecord.attributes.#{object.class.name.underscore}.hints.#{@attribute}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
class EditableChamp::ChampLabelComponent < ApplicationComponent | ||
include Dsfr::InputErrorable | ||
|
||
def initialize(form:, champ:, seen_at: nil) | ||
@form, @champ, @seen_at = form, champ, seen_at | ||
@attribute = :value | ||
end | ||
end |
11 changes: 5 additions & 6 deletions
11
app/components/editable_champ/champ_label_component/champ_label_component.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
= # we do this trick because some html elements should use 'label' and some should be plain paragraphs | ||
- if @champ.html_label? | ||
= @form.label @champ.main_value_name, id: @champ.labelledby_id, for: @champ.input_id do | ||
- render EditableChamp::ChampLabelContentComponent.new champ: @champ, seen_at: @seen_at | ||
= @form.label @champ.main_value_name, id: @champ.labelledby_id, for: @champ.input_id, class: 'fr-label' do | ||
- render EditableChamp::ChampLabelContentComponent.new form: @form, champ: @champ, seen_at: @seen_at | ||
- else | ||
.form-label.mb-4{ id: @champ.labelledby_id } | ||
= render EditableChamp::ChampLabelContentComponent.new champ: @champ, seen_at: @seen_at | ||
.fr-label.mb-4{ id: @champ.labelledby_id } | ||
= render EditableChamp::ChampLabelContentComponent.new form: @form, champ: @champ, seen_at: @seen_at | ||
|
||
|
||
- if @champ.description.present? | ||
.notice{ id: @champ.describedby_id }= render SimpleFormatComponent.new(@champ.description, allow_a: true) |
6 changes: 4 additions & 2 deletions
6
app/components/editable_champ/champ_label_content_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
app/components/editable_champ/date_component/date_component.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
= @form.date_field :value, | ||
id: @champ.input_id, | ||
input_opts(id: @champ.input_id, | ||
aria: { describedby: @champ.describedby_id }, | ||
value: @champ.value, | ||
required: @champ.required?, | ||
placeholder: 'aaaa-mm-jj', | ||
class: "width-33-desktop" | ||
placeholder: 'aaaa-mm-jj', class: "width-33-desktop") |
3 changes: 1 addition & 2 deletions
3
app/components/editable_champ/datetime_component/datetime_component.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
.datetime | ||
= @form.datetime_field(:value, value: formatted_value_for_datetime_locale, id: @champ.input_id, aria: { describedby: @champ.describedby_id }, data: { controller: 'datetime' }) | ||
= @form.datetime_field(:value, input_opts(value: formatted_value_for_datetime_locale, id: @champ.input_id, aria: { describedby: @champ.describedby_id }, data: { controller: 'datetime' })) |
7 changes: 1 addition & 6 deletions
7
app/components/editable_champ/decimal_number_component/decimal_number_component.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
= @form.number_field :value, | ||
id: @champ.input_id, | ||
aria: { describedby: @champ.describedby_id }, | ||
step: :any, | ||
placeholder: "3.14", | ||
required: @champ.required? | ||
= @form.number_field(:value, input_opts(id: @champ.input_id, aria: { describedby: @champ.describedby_id }, step: :any, placeholder: "3.14", required: @champ.required?)) |
Oops, something went wrong.