Skip to content

Commit 4577e72

Browse files
authored
Merge pull request #61 from alphagov/cm-481-combine-govspeak-and-regular-textarea-components
CM-481: Combine TextareaComponent and GovspeakEnabledTextareaComponent
2 parents fe3c6ca + 1310ba4 commit 4577e72

File tree

7 files changed

+89
-89
lines changed

7 files changed

+89
-89
lines changed

app/components/edition/details/fields/bsl_guidance_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="app-c-content-block-manager-bsl-guidance-component">
22
<% value_input = capture do %>
33
<%= render(
4-
Edition::Details::Fields::GovspeakEnabledTextareaComponent.new(
4+
Edition::Details::Fields::TextareaComponent.new(
55
edition: edition,
66
value: value_for_field(value_field),
77
nested_object_key: field.name,

app/components/edition/details/fields/govspeak_enabled_textarea_component.html.erb

Lines changed: 0 additions & 21 deletions
This file was deleted.

app/components/edition/details/fields/govspeak_enabled_textarea_component.rb

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
<%= render "govuk_publishing_components/components/textarea", {
2-
label: {
3-
text: label,
4-
},
5-
name:,
6-
textarea_id: id,
7-
value:,
8-
error_items:,
9-
hint:,
10-
} %>
1+
<div class="app-c-content-block-manager-textarea-component">
2+
<% if govspeak_enabled? %>
3+
<%= render "components/govspeak_editor", {
4+
name: name_attribute,
5+
id: id_attribute,
6+
label: { text: label_element, hint_text: hint_text, hint_id: aria_described_by },
7+
value:,
8+
error_items: error_items,
9+
hint_id: aria_described_by,
10+
rows: 5,
11+
} %>
12+
<% else %>
13+
<%= render "govuk_publishing_components/components/textarea", {
14+
label: { text: label_element },
15+
name: name_attribute,
16+
textarea_id: id_attribute,
17+
value:,
18+
error_items: error_items,
19+
} %>
20+
<% end %>
21+
</div>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,65 @@
11
class Edition::Details::Fields::TextareaComponent < Edition::Details::Fields::BaseComponent
2+
def initialize(nested_object_key: nil, **args)
3+
@nested_object_key = nested_object_key
4+
5+
super(**args)
6+
end
7+
8+
attr_reader :edition, :nested_object_key, :field, :subschema, :errors
9+
10+
def govspeak_enabled?
11+
return false unless nested_object_key
12+
13+
subschema.govspeak_enabled?(nested_object_key: nested_object_key, field_name: field.name)
14+
end
15+
16+
def name_attribute
17+
return name if nested_object_key.blank?
18+
19+
"edition[details][#{subschema_block_type}][#{nested_object_key}][#{field.name}]"
20+
end
21+
22+
def id_attribute
23+
return id if nested_object_key.blank?
24+
25+
"#{PARENT_CLASS}_details_#{subschema_block_type}_#{nested_object_key}_#{field.name}"
26+
end
27+
28+
def label_element
29+
return nested_object_label if nested_object_key
30+
31+
label
32+
end
33+
34+
def hint_text
35+
return nil unless govspeak_enabled?
36+
37+
"Govspeak supported"
38+
end
39+
40+
def aria_described_by
41+
"#{id_attribute}-hint"
42+
end
43+
44+
def error_items
45+
return error_items_for_nested_object if nested_object_key
46+
47+
errors_for(edition.errors, "details_#{id_suffix}".to_sym)
48+
end
49+
50+
private
51+
52+
def nested_object_label
53+
helpers.humanized_label(
54+
relative_key: field.name,
55+
root_object: "#{subschema_block_type}.#{nested_object_key}",
56+
)
57+
end
58+
59+
def error_items_for_nested_object
60+
errors_for(
61+
edition.errors,
62+
"details_#{subschema_block_type}_#{nested_object_key}_#{field.name}".to_sym,
63+
)
64+
end
265
end

app/components/edition/details/fields/video_relay_service_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<% video_relay_service_inputs = capture do %>
44
<%= render(
5-
Edition::Details::Fields::GovspeakEnabledTextareaComponent.new(
5+
Edition::Details::Fields::TextareaComponent.new(
66
edition: edition,
77
value: value_for_field(prefix) ,
88
nested_object_key: field.name,
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require "test_helper"
22

3-
class Edition::Details::Fields::GovspeakEnabledTextareaComponentTest < ViewComponent::TestCase
3+
class Edition::Details::Fields::TextareaComponentTest < ViewComponent::TestCase
44
extend Minitest::Spec::DSL
55

6-
COMPONENT_CLASS = ".app-c-content-block-manager-govspeak-enabled-textarea-component".freeze
6+
COMPONENT_CLASS = ".app-c-content-block-manager-textarea-component".freeze
77

88
let(:edition) { build(:edition, :contact) }
99

@@ -58,7 +58,7 @@ class Edition::Details::Fields::GovspeakEnabledTextareaComponentTest < ViewCompo
5858
let(:field_value) { nil }
5959

6060
let(:component) do
61-
Edition::Details::Fields::GovspeakEnabledTextareaComponent.new(
61+
Edition::Details::Fields::TextareaComponent.new(
6262
edition: edition,
6363
field: field,
6464
value: field_value,

0 commit comments

Comments
 (0)