Module: Shoelace::SlFormHelper
- Defined in:
- app/helpers/shoelace/sl_form_helper.rb
Constant Summary collapse
- ShoelaceFormBuilder =
SlFormBuilder
Instance Method Summary collapse
-
#grouped_sl_options_for_select(grouped_options, options) ⇒ Object
Returns a string of <sl-option> tags, like
options_for_select
, but prepends a <small> tag to each group. - #sl_form_for(*args, **options, &block) ⇒ Object
- #sl_form_with(**args, &block) ⇒ Object
- #sl_option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil) ⇒ Object
-
#sl_options_for_select(enumerable, options = nil) ⇒ Object
Accepts an enumerable (hash, array, enumerable, your type) and returns a string of
sl-option
tags. -
#sl_options_from_collection_for_select(collection, value_method, text_method, selected = nil) ⇒ Object
Returns a string of <sl-option> tags compiled by iterating over the collection and assigning the result of a call to the
value_method
as the option value and thetext_method
as the option text. -
#sl_radio_button(object_name, method, tag_value, options = {}, &block) ⇒ Object
Returns a <sl-radio> tag for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object).
-
#sl_submit_tag(value = 'Save changes', **options) ⇒ Object
Creates a submit button with the text value as the caption, with the
submit
attribute. -
#sl_text_field_tag(name, value = nil, **options, &block) ⇒ Object
Creates a shoelace text field; use these text fields to input smaller chunks of text like a username or a search query.
Instance Method Details
#grouped_sl_options_for_select(grouped_options, options) ⇒ Object
Returns a string of <sl-option> tags, like options_for_select
, but prepends a <small> tag to each group.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 44 def (, ) body = "".html_safe .each_with_index do |container, index| label, values = container body.safe_concat(DIVIDER_TAG) if index > 0 body.safe_concat(content_tag("small", label)) if label.present? body.safe_concat((values, )) end body end |
#sl_form_for(*args, **options, &block) ⇒ Object
15 16 17 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 15 def sl_form_for(*args, **, &block) form_for(*args, **DEFAULT_FORM_PARAMETERS, **, &block) end |
#sl_form_with(**args, &block) ⇒ Object
19 20 21 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 19 def sl_form_with(**args, &block) form_with(**args, **DEFAULT_FORM_PARAMETERS, &block) end |
#sl_option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 78 def sl_option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil) body = "".html_safe collection.each_with_index do |group, index| = (value_for_collection(group, group_method), option_key_method, option_value_method, selected_key) body.safe_concat(DIVIDER_TAG) if index > 0 body.safe_concat(content_tag("small", value_for_collection(group, group_label_method))) body.safe_concat() end body end |
#sl_options_for_select(enumerable, options = nil) ⇒ Object
Accepts an enumerable (hash, array, enumerable, your type) and returns a string of sl-option
tags. Given an enumerable where the elements respond to first
and last
(such as a two-element array), the “lasts” serve as option values and the “firsts” as option text.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 61 def (enumerable, = nil) return enumerable if String === enumerable selected, disabled = extract_selected_and_disabled().map { |r| Array(r).map(&:to_s) } enumerable.map do |element| html_attributes = option_html_attributes(element) text, value = option_text_and_value(element).map(&:to_s) html_attributes[:checked] ||= selected.include?(value) html_attributes[:disabled] ||= disabled.include?(value) html_attributes[:value] = value tag_builder.content_tag_string('sl-option', text, html_attributes) end.join("\n").html_safe end |
#sl_options_from_collection_for_select(collection, value_method, text_method, selected = nil) ⇒ Object
Returns a string of <sl-option> tags compiled by iterating over the collection and assigning the result of a call to the value_method
as the option value and the text_method
as the option text.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 94 def (collection, value_method, text_method, selected = nil) = collection.map do |element| [value_for_collection(element, text_method), value_for_collection(element, value_method), option_html_attributes(element)] end selected, disabled = extract_selected_and_disabled(selected) select_deselect = { selected: extract_values_from_collection(collection, value_method, selected), disabled: extract_values_from_collection(collection, value_method, disabled) } (, select_deselect) end |
#sl_radio_button(object_name, method, tag_value, options = {}, &block) ⇒ Object
Returns a <sl-radio> tag for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). If the current value of method is tag_value
the radio button will be checked.
To force the radio button to be checked pass checked: true in the options hash. You may pass HTML options there as well.
115 116 117 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 115 def (object_name, method, tag_value, = {}, &block) Components::SlRadioButton.new(object_name, method, self, tag_value, ).render(&block) end |
#sl_submit_tag(value = 'Save changes', **options) ⇒ Object
Creates a submit button with the text value as the caption, with the submit
attribute.
24 25 26 27 28 29 30 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 24 def sl_submit_tag(value = 'Save changes', **) = .deep_stringify_keys = { "type" => "submit", "variant" => "primary" }.update() set_default_disable_with(value, ) content_tag('sl-button', value, ) end |
#sl_text_field_tag(name, value = nil, **options, &block) ⇒ Object
Creates a shoelace text field; use these text fields to input smaller chunks of text like a username or a search query.
For the properties available on this tag, please refer to the official documentation:
https://shoelace.style/components/input?id=properties
38 39 40 |
# File 'app/helpers/shoelace/sl_form_helper.rb', line 38 def sl_text_field_tag(name, value = nil, **, &block) content_tag('sl-input', '', { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(.stringify_keys), &block) end |