Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add open attribute to component wrapper #4074

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 open attribute to component wrapper ([PR #4074](https://github.com/alphagov/govuk_publishing_components/pull/4074))
* Add guard to accordion component init method ([PR #4069](https://github.com/alphagov/govuk_publishing_components/pull/4069))
* Prevent screen readers from announcing document list child/parts items dashes ([PR #4066](https://github.com/alphagov/govuk_publishing_components/pull/4066))
* Align checkboxes component more toward Design System ([PR #4061](https://github.com/alphagov/govuk_publishing_components/pull/4061))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def self.description
- `classes` - accepts a space separated string of classes, these should not be used for styling and must be prefixed with `js-`
- `role` - accepts a space separated string of roles
- `lang` - accepts a language attribute value
- `open` - accepts an open attribute value (true or false)
"
end
end
Expand Down
2 changes: 2 additions & 0 deletions docs/component-wrapper-helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ These options can be passed to any component that uses the component wrapper.
- `classes` - accepts a space separated string of classes, these should not be used for styling and must be prefixed with `js-`
- `role` - accepts a space separated string of roles
- `lang` - accepts a language attribute value
- `open` - accepts an open 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-`, `app-c-` or `govuk-` are also permitted, but should only be used within the component and not passed to it.

Expand Down Expand Up @@ -71,6 +72,7 @@ The component wrapper includes several methods to make managing options easier,
component_helper.add_aria_attribute({ label: "my-label" }) # works like 'add_data_attribute'
component_helper.add_role("button") # works like 'add_class'
component_helper.set_lang("en") # works like 'set_id' (can only have one lang)
component_helper.set_open(true) # can pass true or false
%>
<%= tag.div(**component_helper.all_attributes) do %>
component content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(options)
check_aria_is_valid(@options[:aria]) if @options.include?(:aria)
check_role_is_valid(@options[:role]) if @options.include?(:role)
check_lang_is_valid(@options[:lang]) if @options.include?(:lang)
check_open_is_valid(@options[:open]) if @options.include?(:open)
end

def all_attributes
Expand All @@ -21,6 +22,7 @@ def all_attributes
attributes[:class] = @options[:classes] unless @options[:classes].blank?
attributes[:role] = @options[:role] unless @options[:role].blank?
attributes[:lang] = @options[:lang] unless @options[:lang].blank?
attributes[:open] = @options[:open] unless @options[:open].blank?

attributes
end
Expand Down Expand Up @@ -55,6 +57,11 @@ def set_lang(lang)
@options[:lang] = lang
end

def set_open(open_attribute)
check_open_is_valid(open_attribute)
@options[:open] = open_attribute
end

private

def check_id_is_valid(id)
Expand Down Expand Up @@ -114,6 +121,15 @@ def check_lang_is_valid(lang)
end
end

def check_open_is_valid(open_attribute)
return if open_attribute.blank?

options = [true, false]
unless options.include? open_attribute
raise(ArgumentError, "open attribute (#{open_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 @@ -11,6 +11,7 @@
},
role: "navigation",
lang: "en",
open: true,
}
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(args)
expected = {
Expand All @@ -23,6 +24,7 @@
},
role: "navigation",
lang: "en",
open: true,
}
expect(component_helper.all_attributes).to eql(expected)
end
Expand Down Expand Up @@ -167,5 +169,31 @@
GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(lang: "klingon")
}.to raise_error(ArgumentError, error)
end

it "accepts valid open attribute value" do
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(open: true)
expected = {
open: true,
}
expect(component_helper.all_attributes).to eql(expected)
end

it "can set an open attribute, overriding a passed value" do
helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(open: true)
helper.set_open(false)
expect(helper.all_attributes[:open]).to eql(nil)
end

it "does not include an open attribute if the option is false" do
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(open: false)
expect(component_helper.all_attributes).to eql({})
end

it "does not accept an invalid open value" do
error = "open attribute (false) is not recognised"
expect {
GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(open: "false")
}.to raise_error(ArgumentError, error)
end
end
end