From a5d086246f22c5a64b3db344654e4e8419ecb7bc Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 15 Jan 2025 12:25:13 +0000 Subject: [PATCH 1/3] Add target to component wrapper --- .../component_wrapper_helper_options.rb | 1 + docs/component-wrapper-helper.md | 2 ++ .../presenters/component_wrapper_helper.rb | 16 +++++++++++++++ .../component_wrapper_helper_spec.rb | 20 +++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/app/models/govuk_publishing_components/component_wrapper_helper_options.rb b/app/models/govuk_publishing_components/component_wrapper_helper_options.rb index 4c2786dba6..a22e078289 100644 --- a/app/models/govuk_publishing_components/component_wrapper_helper_options.rb +++ b/app/models/govuk_publishing_components/component_wrapper_helper_options.rb @@ -17,6 +17,7 @@ def self.description - `dir` - accepts 'rtl', 'ltr', or 'auto'. - `type` - accepts any valid type attribute e.g. 'button', 'submit', 'text'. - `rel` - accepts any valid rel attribute e.g. 'nofollow'. +- `target` - accepts a valid target attribute e.g. '_blank'. - `draggable` - accepts a draggable attribute value (\"true\" or \"false\") " end diff --git a/docs/component-wrapper-helper.md b/docs/component-wrapper-helper.md index 28891cdb16..50a5a20785 100644 --- a/docs/component-wrapper-helper.md +++ b/docs/component-wrapper-helper.md @@ -38,6 +38,7 @@ These options can be passed to any component that uses the component wrapper. - `dir` - accepts 'rtl', 'ltr', or 'auto'. - `type` - accepts any valid type attribute e.g. 'button', 'submit', 'text' - `rel` - accepts any valid rel attribute e.g. 'nofollow' +- `target` - accepts a valid target attribute e.g. '_blank' - `draggable` - accepts a draggable attribute value ("true" or "false") To prevent breaking [component isolation](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/component_principles.md#a-component-is-isolated-when), passed classes should only be used for JavaScript hooks and not styling. All component styling should be included only in the component itself. Any passed classes should be prefixed with `js-`. To allow for extending this option, classes prefixed with `gem-c-`, `govuk-`, `app-c-`, `brand--`, or `brand__` are also permitted, as well as an exact match of `direction-rtl`, but these classes should only be used within the component and not passed to it. @@ -87,6 +88,7 @@ The component wrapper includes several methods to make managing options easier, component_helper.set_draggable("true") component_helper.set_rel("nofollow") # overrides any existing rel component_helper.add_rel("noopener") # adds to existing rel + component_helper.set_target("_blank") component_helper.set_margin_bottom(3) # can pass any number from 1 to 9 %> <%= tag.div(**component_helper.all_attributes) do %> diff --git a/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb b/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb index 620b8aaaa2..0b611b0338 100644 --- a/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb +++ b/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb @@ -17,6 +17,7 @@ def initialize(options) check_type_is_valid(@options[:type]) if @options.include?(:type) check_draggable_is_valid(@options[:draggable]) if @options.include?(:draggable) check_rel_is_valid(@options[:rel]) if @options.include?(:rel) + check_target_is_valid(@options[:target]) if @options.include?(:target) check_margin_bottom_is_valid(@options[:margin_bottom]) if @options.include?(:margin_bottom) end @@ -39,6 +40,7 @@ def all_attributes attributes[:type] = @options[:type] unless @options[:type].blank? attributes[:draggable] = @options[:draggable] unless @options[:draggable].blank? attributes[:rel] = @options[:rel] unless @options[:rel].blank? + attributes[:target] = @options[:target] unless @options[:target].blank? attributes end @@ -113,6 +115,11 @@ def set_rel(rel_attribute) @options[:rel] = rel_attribute end + def set_target(target_attribute) + check_target_is_valid(target_attribute) + @options[:target] = target_attribute + end + def set_margin_bottom(margin_bottom) check_margin_bottom_is_valid(margin_bottom) @options[:margin_bottom] = margin_bottom @@ -246,6 +253,15 @@ def check_rel_is_valid(rel_attribute) end end + def check_target_is_valid(target_attribute) + return if target_attribute.blank? + + options = %w[_self _blank _parent _top _unfencedTop] + unless options.include? target_attribute + raise(ArgumentError, "target attribute (#{target_attribute}) is not recognised") + end + end + def extend_string(option, string) ((@options[option] ||= "") << " #{string}").strip! end diff --git a/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb b/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb index 490a7c2f77..55177febde 100644 --- a/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb +++ b/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb @@ -432,6 +432,26 @@ end end + describe "target" do + it "does not accept an invalid target value" do + error = "target attribute (zelda) is not recognised" + expect { + GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(target: "zelda") + }.to raise_error(ArgumentError, error) + end + + it "accepts a valid target value" do + component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(target: "_blank") + expect(component_helper.all_attributes[:target]).to eql("_blank") + end + + it "can set a target, overriding a passed value" do + helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(target: "_blank") + helper.set_target("_self") + expect(helper.all_attributes[:target]).to eql("_self") + end + end + describe "margins" do it "complains about an invalid margin" do error = "margin_bottom option (15) is not recognised" From 195e369627a7648de1df9dd0b50e708f4d94b3a2 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 15 Jan 2025 13:29:27 +0000 Subject: [PATCH 2/3] Add title to component wrapper - no real limits to what a title should be, so no error checking needed --- .../component_wrapper_helper_options.rb | 11 ++++++----- docs/component-wrapper-helper.md | 2 ++ .../presenters/component_wrapper_helper.rb | 5 +++++ .../components/component_wrapper_helper_spec.rb | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/models/govuk_publishing_components/component_wrapper_helper_options.rb b/app/models/govuk_publishing_components/component_wrapper_helper_options.rb index a22e078289..0886c545cc 100644 --- a/app/models/govuk_publishing_components/component_wrapper_helper_options.rb +++ b/app/models/govuk_publishing_components/component_wrapper_helper_options.rb @@ -13,11 +13,12 @@ def self.description - `lang` - accepts a language attribute value - `open` - accepts an open attribute value (true or false) - `hidden` - accepts an empty string, 'hidden', or 'until-found' -- `tabindex` - accepts an integer. The integer can also be passed as a string. -- `dir` - accepts 'rtl', 'ltr', or 'auto'. -- `type` - accepts any valid type attribute e.g. 'button', 'submit', 'text'. -- `rel` - accepts any valid rel attribute e.g. 'nofollow'. -- `target` - accepts a valid target attribute e.g. '_blank'. +- `tabindex` - accepts an integer. The integer can also be passed as a string +- `dir` - accepts 'rtl', 'ltr', or 'auto' +- `type` - accepts any valid type attribute e.g. 'button', 'submit', 'text' +- `rel` - accepts any valid rel attribute e.g. 'nofollow' +- `target` - accepts a valid target attribute e.g. '_blank' +- `title` - accepts any string - `draggable` - accepts a draggable attribute value (\"true\" or \"false\") " end diff --git a/docs/component-wrapper-helper.md b/docs/component-wrapper-helper.md index 50a5a20785..8fe0154501 100644 --- a/docs/component-wrapper-helper.md +++ b/docs/component-wrapper-helper.md @@ -39,6 +39,7 @@ These options can be passed to any component that uses the component wrapper. - `type` - accepts any valid type attribute e.g. 'button', 'submit', 'text' - `rel` - accepts any valid rel attribute e.g. 'nofollow' - `target` - accepts a valid target attribute e.g. '_blank' +- `title` - accepts any string - `draggable` - accepts a draggable attribute value ("true" or "false") To prevent breaking [component isolation](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/component_principles.md#a-component-is-isolated-when), passed classes should only be used for JavaScript hooks and not styling. All component styling should be included only in the component itself. Any passed classes should be prefixed with `js-`. To allow for extending this option, classes prefixed with `gem-c-`, `govuk-`, `app-c-`, `brand--`, or `brand__` are also permitted, as well as an exact match of `direction-rtl`, but these classes should only be used within the component and not passed to it. @@ -89,6 +90,7 @@ The component wrapper includes several methods to make managing options easier, component_helper.set_rel("nofollow") # overrides any existing rel component_helper.add_rel("noopener") # adds to existing rel component_helper.set_target("_blank") + component_helper.set_title("this is a title") component_helper.set_margin_bottom(3) # can pass any number from 1 to 9 %> <%= tag.div(**component_helper.all_attributes) do %> diff --git a/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb b/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb index 0b611b0338..41f3958d04 100644 --- a/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb +++ b/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb @@ -41,6 +41,7 @@ def all_attributes attributes[:draggable] = @options[:draggable] unless @options[:draggable].blank? attributes[:rel] = @options[:rel] unless @options[:rel].blank? attributes[:target] = @options[:target] unless @options[:target].blank? + attributes[:title] = @options[:title] unless @options[:title].blank? attributes end @@ -120,6 +121,10 @@ def set_target(target_attribute) @options[:target] = target_attribute end + def set_title(title_attribute) + @options[:title] = title_attribute + end + def set_margin_bottom(margin_bottom) check_margin_bottom_is_valid(margin_bottom) @options[:margin_bottom] = margin_bottom diff --git a/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb b/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb index 55177febde..d029f8f382 100644 --- a/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb +++ b/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb @@ -17,6 +17,7 @@ dir: "rtl", type: "submit", draggable: "true", + title: "Hello", } component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(args) expected = { @@ -35,6 +36,7 @@ dir: "rtl", type: "submit", draggable: "true", + title: "Hello", } expect(component_helper.all_attributes).to eql(expected) end @@ -53,6 +55,7 @@ dir: nil, type: nil, draggable: nil, + title: nil, ) expect(component_helper.all_attributes).to eql({}) end @@ -70,6 +73,7 @@ dir: "", type: "", draggable: "", + title: "", ) expect(component_helper.all_attributes).to eql({}) end @@ -452,6 +456,19 @@ end end + describe "target" do + it "accepts a valid title value" do + component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(title: "this is a title") + expect(component_helper.all_attributes[:title]).to eql("this is a title") + end + + it "can set a title, overriding a passed value" do + helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(title: "this is a title") + helper.set_title("this is a different title") + expect(helper.all_attributes[:title]).to eql("this is a different title") + end + end + describe "margins" do it "complains about an invalid margin" do error = "margin_bottom option (15) is not recognised" From 6a3cb7e1f12074d5f1366b63fc951913bfcd94c2 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 15 Jan 2025 13:33:28 +0000 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17e019f594..e74d0d8ac0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## Unreleased +* Add more options to the component wrapper helper ([PR #4554](https://github.com/alphagov/govuk_publishing_components/pull/4554)) * **BREAKING** Use component wrapper on big number component ([PR #4550](https://github.com/alphagov/govuk_publishing_components/pull/4550)) * **BREAKING** Use component wrapper on attachment component ([PR #4545](https://github.com/alphagov/govuk_publishing_components/pull/4545)) * **BREAKING** Use component wrapper on attachment link component ([PR #4549](https://github.com/alphagov/govuk_publishing_components/pull/4549))