From c360cdb2c00d3b94b452f05c2f2c0a8fa7b04efb Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Thu, 9 Jan 2025 14:54:17 +0000 Subject: [PATCH] Add type option to component wrapper helper --- docs/component-wrapper-helper.md | 2 ++ .../presenters/component_wrapper_helper.rb | 16 ++++++++++++++ .../component_wrapper_helper_spec.rb | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/docs/component-wrapper-helper.md b/docs/component-wrapper-helper.md index 9c22bc4838..67f0ba1b3f 100644 --- a/docs/component-wrapper-helper.md +++ b/docs/component-wrapper-helper.md @@ -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. @@ -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 %> diff --git a/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb b/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb index 727c0c7b43..33e8c2a0c9 100644 --- a/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb +++ b/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb @@ -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 @@ -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 @@ -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 @@ -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 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 183e6da5f8..cbfc400fd7 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 @@ -14,6 +14,7 @@ open: true, hidden: "", tabindex: "0", + type: "submit", } component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(args) expected = { @@ -29,6 +30,7 @@ open: true, hidden: "", tabindex: "0", + type: "submit", } expect(component_helper.all_attributes).to eql(expected) end @@ -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