Sha256: 99d1b69bccb9d3f4bf30869f277c7f826340c64075f122e4d3c993eb8bbf33e9

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

require 'action_view'

module GovukPublishingComponents
  module Presenters
    class ButtonHelper
      attr_reader :href, :text, :title, :info_text, :rel, :data_attributes,
        :margin_bottom, :inline_layout, :target, :type, :start, :secondary, :secondary_quiet, :destructive

      def initialize(local_assigns)
        @href = local_assigns[:href]
        @text = local_assigns[:text]
        @title = local_assigns[:title]
        @info_text = local_assigns[:info_text]
        @rel = local_assigns[:rel]
        @data_attributes = local_assigns[:data_attributes]
        @margin_bottom = local_assigns[:margin_bottom]
        @inline_layout = local_assigns[:inline_layout]
        @target = local_assigns[:target]
        @type = local_assigns[:type]
        @start = local_assigns[:start]
        @secondary = local_assigns[:secondary]
        @secondary_quiet = local_assigns[:secondary_quiet]
        @destructive = local_assigns[:destructive]
      end

      def link?
        href.present?
      end

      def html_options
        options = { class: css_classes }
        options[:role] = "button" if link?
        options[:type] = button_type
        options[:rel] = rel if rel
        options[:data] = data_attributes if data_attributes
        options[:title] = title if title
        options[:target] = target if target
        options
      end

      def button_type
        type || "submit" unless link?
      end

    private

      def css_classes
        classes = %w(gem-c-button govuk-button)
        classes << "govuk-button--start" if start
        classes << "gem-c-button--secondary" if secondary
        classes << "gem-c-button--secondary-quiet" if secondary_quiet
        classes << "govuk-button--warning" if destructive
        classes << "gem-c-button--bottom-margin" if margin_bottom
        classes << "gem-c-button--inline" if inline_layout
        classes.join(" ")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govuk_publishing_components-16.22.0 lib/govuk_publishing_components/presenters/button_helper.rb
govuk_publishing_components-16.21.0 lib/govuk_publishing_components/presenters/button_helper.rb
govuk_publishing_components-16.20.1 lib/govuk_publishing_components/presenters/button_helper.rb