summaryrefslogtreecommitdiff
path: root/app/components/pajamas/alert_component.rb
blob: c1b2132da29199961471b0a23297fc7ac4497467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

# Renders a GlAlert root element
module Pajamas
  class AlertComponent < Pajamas::Component
    # @param [String] title
    # @param [Symbol] variant
    # @param [Boolean] dismissible
    # @param [Boolean] show_icon
    # @param [String] alert_class
    # @param [Hash] alert_data
    # @param [String] close_button_class
    # @param [Hash] close_button_data
    def initialize(
      title: nil, variant: :info, dismissible: true, show_icon: true,
      alert_class: nil, alert_data: {}, close_button_class: nil, close_button_data: {})
      @title = title
      @variant = variant
      @dismissible = dismissible
      @show_icon = show_icon
      @alert_class = alert_class
      @alert_data = alert_data
      @close_button_class = close_button_class
      @close_button_data = close_button_data
    end

    def base_class
      classes = ["gl-alert-#{@variant}"]
      classes.push('gl-alert-not-dismissible') unless @dismissible
      classes.push('gl-alert-no-icon') unless @show_icon

      classes.join(' ')
    end

    private

    delegate :sprite_icon, to: :helpers

    renders_one :body
    renders_one :actions

    ICONS = {
      info: 'information-o',
      warning: 'warning',
      success: 'check-circle',
      danger: 'error',
      tip: 'bulb'
    }.freeze

    def icon
      ICONS[@variant]
    end

    def icon_classes
      "gl-alert-icon#{' gl-alert-icon-no-title' if @title.nil?}"
    end
  end
end