Skip to content

Commit

Permalink
Add type option to component wrapper helper
Browse files Browse the repository at this point in the history
  • Loading branch information
andysellick committed Jan 9, 2025
1 parent 29c5124 commit c360cdb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/component-wrapper-helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ These options can be passed to any component that uses the component wrapper.
- `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 'button', 'submit'.
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 @@ -80,6 +81,7 @@ The component wrapper includes several methods to make managing options easier,
component_helper.set_open(true) # can pass true or false
component_helper.set_tabindex(1)
component_helper.set_dir("rtl")
component_helper.set_type("text")
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 @@ -14,6 +14,7 @@ def initialize(options)
check_hidden_is_valid(@options[:hidden]) if @options.include?(:hidden)
check_tabindex_is_valid(@options[:tabindex]) if @options.include?(:tabindex)
check_dir_is_valid(@options[:dir]) if @options.include?(:dir)
check_type_is_valid(@options[:type]) if @options.include?(:type)
check_margin_bottom_is_valid(@options[:margin_bottom]) if @options.include?(:margin_bottom)
end

Expand All @@ -33,6 +34,7 @@ def all_attributes
attributes[:hidden] = @options[:hidden] unless @options[:hidden].nil?
attributes[:tabindex] = @options[:tabindex] unless @options[:tabindex].blank?
attributes[:dir] = @options[:dir] unless @options[:dir].blank?
attributes[:type] = @options[:type] unless @options[:type].blank?

attributes
end
Expand Down Expand Up @@ -87,6 +89,11 @@ def set_dir(dir_attribute)
@options[:dir] = dir_attribute
end

def set_type(type_attribute)
check_type_is_valid(type_attribute)
@options[:type] = type_attribute
end

def set_margin_bottom(margin_bottom)
check_margin_bottom_is_valid(margin_bottom)
@options[:margin_bottom] = margin_bottom
Expand Down Expand Up @@ -192,6 +199,15 @@ def check_dir_is_valid(dir_attribute)
end
end

def check_type_is_valid(type_attribute)
return if type_attribute.nil?

options = %w[button checkbox file hidden radio reset submit text]
unless options.include? type_attribute
raise(ArgumentError, "type attribute (#{type_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 @@ -14,6 +14,7 @@
open: true,
hidden: "",
tabindex: "0",
type: "submit",
}
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(args)
expected = {
Expand All @@ -29,6 +30,7 @@
open: true,
hidden: "",
tabindex: "0",
type: "submit",
}
expect(component_helper.all_attributes).to eql(expected)
end
Expand Down Expand Up @@ -312,6 +314,26 @@
end
end
end

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

it "accepts valid type value" do
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(type: "submit")
expect(component_helper.all_attributes[:type]).to eql("submit")
end

it "can set a type, overriding a passed value" do
helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(type: "submit")
helper.set_type("button")
expect(helper.all_attributes[:type]).to eql("button")
end
end
end

describe "margins" do
Expand Down

0 comments on commit c360cdb

Please sign in to comment.