Class: DsfrComponent::AlertComponent

Inherits:
Base
  • Object
show all
Defined in:
app/components/dsfr_component/alert_component.rb

Constant Summary collapse

TYPES =
%i[error success info warning].freeze
SIZES =
%i[sm md].freeze

Constants inherited from Base

Base::HEADING_LEVELS

Instance Attribute Summary

Attributes inherited from Base

#html_attributes

Instance Method Summary collapse

Constructor Details

#initialize(type:, title: nil, size: :md, close_button: false, classes: [], html_attributes: {}) ⇒ AlertComponent

Note:

in size MD the title is required but the content is optional. In size SM there should be not title but the content is required

Returns a new instance of AlertComponent.

Parameters:

  • type (Symbol)

    alert type (and matching color) ‘:success`, `:info`, `:warning` ou `:error`

  • title (String) (defaults to: nil)

    alert title. cannot be set in size ‘:sm`

  • size (Symbol) (defaults to: :md)

    alert size : ‘:md` (default) or `:sm`

  • close_button (Boolean) (defaults to: false)

    display a close button to remove the alert



10
11
12
13
14
15
16
17
# File 'app/components/dsfr_component/alert_component.rb', line 10

def initialize(type:, title: nil, size: :md, close_button: false, classes: [], html_attributes: {})
  @title = title
  @type = type
  @size = size
  @close_button = close_button

  super(classes: classes, html_attributes: html_attributes)
end

Instance Method Details

#callObject

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
# File 'app/components/dsfr_component/alert_component.rb', line 19

def call
  raise ArgumentError, "SM alerts cannot have titles but must have a content" if @size == :sm && (@title.present? || content.blank?)
  raise ArgumentError, "MD Alerts must have a title" if @size == :md && @title.blank?

  tag.div(**html_attributes) do
    safe_join([title_tag, , close_button_tag])
  end
end