Skip to content

Commit

Permalink
Merge pull request #4554 from alphagov/cwh-options
Browse files Browse the repository at this point in the history
Add more options to the component wrapper helper
  • Loading branch information
andysellick authored Jan 15, 2025
2 parents f6227cb + 6a3cb7e commit 6494163
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +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'.
- `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
Expand Down
4 changes: 4 additions & 0 deletions docs/component-wrapper-helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ 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'
- `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.
Expand Down Expand Up @@ -87,6 +89,8 @@ 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_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 %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -39,6 +40,8 @@ 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[:title] = @options[:title] unless @options[:title].blank?

attributes
end
Expand Down Expand Up @@ -113,6 +116,15 @@ 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_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
Expand Down Expand Up @@ -246,6 +258,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
dir: "rtl",
type: "submit",
draggable: "true",
title: "Hello",
}
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(args)
expected = {
Expand All @@ -35,6 +36,7 @@
dir: "rtl",
type: "submit",
draggable: "true",
title: "Hello",
}
expect(component_helper.all_attributes).to eql(expected)
end
Expand All @@ -53,6 +55,7 @@
dir: nil,
type: nil,
draggable: nil,
title: nil,
)
expect(component_helper.all_attributes).to eql({})
end
Expand All @@ -70,6 +73,7 @@
dir: "",
type: "",
draggable: "",
title: "",
)
expect(component_helper.all_attributes).to eql({})
end
Expand Down Expand Up @@ -432,6 +436,39 @@
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 "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"
Expand Down

0 comments on commit 6494163

Please sign in to comment.