Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab_ui_form_builder.rb « form_builders « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8e87a864ccb84e388cb08d0d4b72450f28127c1 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

module Gitlab
  module FormBuilders
    class GitlabUiFormBuilder < ActionView::Helpers::FormBuilder
      def gitlab_ui_checkbox_component(
        method,
        label,
        help_text: nil,
        checkbox_options: {},
        checked_value: '1',
        unchecked_value: '0',
        label_options: {}
      )
        @template.content_tag(
          :div,
          class: 'gl-form-checkbox custom-control custom-checkbox'
        ) do
          value = checkbox_options[:multiple] ? checked_value : nil

          @template.check_box(
            @object_name,
            method,
            format_options(checkbox_options, ['custom-control-input']),
            checked_value,
            unchecked_value
          ) + generic_label(method, label, label_options, help_text: help_text, value: value)
        end
      end

      def gitlab_ui_radio_component(
        method,
        value,
        label,
        help_text: nil,
        radio_options: {},
        label_options: {}
      )
        @template.content_tag(
          :div,
          class: 'gl-form-radio custom-control custom-radio'
        ) do
          @template.radio_button(
            @object_name,
            method,
            value,
            format_options(radio_options, ['custom-control-input'])
          ) + generic_label(method, label, label_options, help_text: help_text, value: value)
        end
      end

      private

      def generic_label(method, label, label_options, help_text: nil, value: nil)
        @template.label(
          @object_name, method, format_options(label_options.merge({ value: value }), ['custom-control-label'])
        ) do
          if help_text
            @template.content_tag(
              :span,
              label
            ) +
            @template.content_tag(
              :p,
              help_text,
              class: 'help-text'
            )
          else
            label
          end
        end
      end

      def format_options(options, classes)
        classes << options[:class]

        objectify_options(options.merge({ class: classes.flatten.compact }))
      end
    end
  end
end