2018-08-18 07:19:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-07 10:37:35 -05:00
|
|
|
module DropdownsHelper
|
2022-09-01 17:10:22 -04:00
|
|
|
# rubocop:disable Metrics/CyclomaticComplexity
|
2016-03-09 05:19:41 -05:00
|
|
|
def dropdown_tag(toggle_text, options: {}, &block)
|
2017-06-02 13:11:26 -04:00
|
|
|
content_tag :div, class: "dropdown #{options[:wrapper_class] if options.key?(:wrapper_class)}" do
|
2016-03-09 05:19:41 -05:00
|
|
|
data_attr = { toggle: "dropdown" }
|
2016-03-07 12:17:11 -05:00
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if options.key?(:data)
|
2016-03-09 05:19:41 -05:00
|
|
|
data_attr = options[:data].merge(data_attr)
|
2016-03-07 10:37:35 -05:00
|
|
|
end
|
|
|
|
|
2016-03-09 05:19:41 -05:00
|
|
|
dropdown_output = dropdown_toggle(toggle_text, data_attr, options)
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2018-12-06 11:08:12 -05:00
|
|
|
if options.key?(:toggle_link)
|
|
|
|
dropdown_output = dropdown_toggle_link(toggle_text, data_attr, options)
|
|
|
|
end
|
|
|
|
|
2020-07-03 08:08:53 -04:00
|
|
|
content_tag_options = { class: "dropdown-menu dropdown-select #{options[:dropdown_class] if options.key?(:dropdown_class)}" }
|
2022-09-01 17:10:22 -04:00
|
|
|
content_tag_options[:data] = options[:dropdown_qa_selector] ? { qa_selector: "#{options[:dropdown_qa_selector]}" } : {}
|
|
|
|
content_tag_options[:data][:testid] = "#{options[:dropdown_testid]}" if options[:dropdown_testid]
|
2020-07-03 08:08:53 -04:00
|
|
|
|
|
|
|
dropdown_output << content_tag(:div, content_tag_options) do
|
2018-08-18 07:19:57 -04:00
|
|
|
output = []
|
2016-03-07 10:37:35 -05:00
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if options.key?(:title)
|
2016-03-09 05:19:41 -05:00
|
|
|
output << dropdown_title(options[:title])
|
2016-03-07 10:37:35 -05:00
|
|
|
end
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if options.key?(:filter)
|
2016-03-09 05:19:41 -05:00
|
|
|
output << dropdown_filter(options[:placeholder])
|
2016-03-07 10:37:35 -05:00
|
|
|
end
|
|
|
|
|
2021-03-16 02:09:57 -04:00
|
|
|
output << content_tag(:div, data: { qa_selector: "dropdown_list_content" }, class: "dropdown-content #{options[:content_class] if options.key?(:content_class)}") do
|
2017-06-02 13:11:26 -04:00
|
|
|
capture(&block) if block && !options.key?(:footer_content)
|
2016-03-07 12:28:37 -05:00
|
|
|
end
|
|
|
|
|
2016-03-17 05:09:06 -04:00
|
|
|
if block && options[:footer_content]
|
2016-03-09 05:19:41 -05:00
|
|
|
output << content_tag(:div, class: "dropdown-footer") do
|
2016-03-07 12:28:37 -05:00
|
|
|
capture(&block)
|
|
|
|
end
|
2016-03-07 10:37:35 -05:00
|
|
|
end
|
|
|
|
|
2016-03-09 05:19:41 -05:00
|
|
|
output << dropdown_loading
|
2018-08-18 07:19:57 -04:00
|
|
|
output.join.html_safe
|
2016-03-07 10:37:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
dropdown_output.html_safe
|
|
|
|
end
|
|
|
|
end
|
2022-09-01 17:10:22 -04:00
|
|
|
# rubocop:enable Metrics/CyclomaticComplexity
|
2016-03-09 05:19:41 -05:00
|
|
|
|
2016-05-26 18:55:49 -04:00
|
|
|
def dropdown_toggle(toggle_text, data_attr, options = {})
|
2016-07-19 05:05:38 -04:00
|
|
|
default_label = data_attr[:default_label]
|
2019-07-16 12:03:29 -04:00
|
|
|
content_tag(:button, disabled: options[:disabled], class: "dropdown-menu-toggle #{options[:toggle_class] if options.key?(:toggle_class)}", id: (options[:id] if options.key?(:id)), type: "button", data: data_attr) do
|
2016-07-16 04:03:19 -04:00
|
|
|
output = content_tag(:span, toggle_text, class: "dropdown-toggle-text #{'is-default' if toggle_text == default_label}")
|
2020-11-18 13:09:08 -05:00
|
|
|
output << sprite_icon('chevron-down', css_class: "dropdown-menu-toggle-icon gl-top-3")
|
2016-03-09 05:19:41 -05:00
|
|
|
output.html_safe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-06 11:08:12 -05:00
|
|
|
def dropdown_toggle_link(toggle_text, data_attr, options = {})
|
|
|
|
output = content_tag(:a, toggle_text, class: "dropdown-toggle-text #{options[:toggle_class] if options.key?(:toggle_class)}", id: (options[:id] if options.key?(:id)), data: data_attr)
|
|
|
|
output.html_safe
|
|
|
|
end
|
|
|
|
|
2017-07-28 13:36:07 -04:00
|
|
|
def dropdown_title(title, options: {})
|
2020-09-16 20:09:34 -04:00
|
|
|
has_back = options.fetch(:back, false)
|
|
|
|
has_close = options.fetch(:close, true)
|
|
|
|
|
|
|
|
container_class = %w[dropdown-title gl-display-flex]
|
|
|
|
margin_class = []
|
|
|
|
|
|
|
|
if has_back && has_close
|
|
|
|
container_class << 'gl-justify-content-space-between'
|
|
|
|
elsif has_back
|
|
|
|
margin_class << 'gl-mr-auto'
|
|
|
|
elsif has_close
|
|
|
|
margin_class << 'gl-ml-auto'
|
|
|
|
end
|
|
|
|
|
|
|
|
container_class = container_class.join(' ')
|
|
|
|
margin_class = margin_class.join(' ')
|
|
|
|
|
|
|
|
content_tag :div, class: container_class do
|
2018-08-18 07:19:57 -04:00
|
|
|
title_output = []
|
2016-03-09 05:19:41 -05:00
|
|
|
|
2020-09-16 20:09:34 -04:00
|
|
|
if has_back
|
|
|
|
title_output << content_tag(:button, class: "dropdown-title-button dropdown-menu-back " + margin_class, aria: { label: "Go back" }, type: "button") do
|
2020-09-07 11:09:04 -04:00
|
|
|
sprite_icon('arrow-left')
|
2016-03-09 05:19:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-16 20:09:34 -04:00
|
|
|
title_output << content_tag(:span, title, class: margin_class)
|
2016-03-09 05:19:41 -05:00
|
|
|
|
2020-09-16 20:09:34 -04:00
|
|
|
if has_close
|
|
|
|
title_output << content_tag(:button, class: "dropdown-title-button dropdown-menu-close " + margin_class, aria: { label: "Close" }, type: "button") do
|
|
|
|
sprite_icon('close', size: 16, css_class: 'dropdown-menu-close-icon')
|
2017-07-28 13:36:07 -04:00
|
|
|
end
|
2016-03-09 05:19:41 -05:00
|
|
|
end
|
|
|
|
|
2018-08-18 07:19:57 -04:00
|
|
|
title_output.join.html_safe
|
2016-03-09 05:19:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-09 05:44:17 -04:00
|
|
|
def dropdown_filter(placeholder, search_id: nil)
|
2016-03-09 05:19:41 -05:00
|
|
|
content_tag :div, class: "dropdown-input" do
|
2021-06-01 20:09:56 -04:00
|
|
|
filter_output = search_field_tag search_id, nil, data: { qa_selector: "dropdown_input_field" }, id: nil, class: "dropdown-input-field", placeholder: placeholder, autocomplete: 'off'
|
2020-10-07 17:08:21 -04:00
|
|
|
filter_output << sprite_icon('search', css_class: 'dropdown-input-search')
|
2020-09-16 20:09:34 -04:00
|
|
|
filter_output << sprite_icon('close', size: 16, css_class: 'dropdown-input-clear js-dropdown-input-clear')
|
2016-03-09 05:19:41 -05:00
|
|
|
|
|
|
|
filter_output.html_safe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def dropdown_content(&block)
|
|
|
|
content_tag(:div, class: "dropdown-content") do
|
|
|
|
if block
|
|
|
|
capture(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-14 03:26:19 -04:00
|
|
|
def dropdown_footer(add_content_class: false, &block)
|
2016-03-09 05:19:41 -05:00
|
|
|
content_tag(:div, class: "dropdown-footer") do
|
2017-08-14 03:26:19 -04:00
|
|
|
if add_content_class
|
|
|
|
content_tag(:div, capture(&block), class: "dropdown-footer-content")
|
|
|
|
else
|
2016-03-09 05:19:41 -05:00
|
|
|
capture(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def dropdown_loading
|
2022-03-04 07:13:07 -05:00
|
|
|
spinner = gl_loading_icon(size: "md", css_class: "gl-mt-7")
|
2020-10-21 23:08:25 -04:00
|
|
|
content_tag(:div, spinner, class: "dropdown-loading")
|
2016-03-09 05:19:41 -05:00
|
|
|
end
|
2016-03-07 10:37:35 -05:00
|
|
|
end
|