Module: Shoelace::Rails::Ui::FormHelper

Defined in:
app/helpers/shoelace/rails/ui/form_helper.rb

Defined Under Namespace

Classes: ShoelaceCheckBox, ShoelaceCollectionRadioButtons, ShoelaceCollectionSelect, ShoelaceColorPicker, ShoelaceFormBuilder, ShoelaceInputField, ShoelaceRadioButton, ShoelaceRange, ShoelaceSelect, ShoelaceSwitch, ShoelaceTextArea

Constant Summary collapse

DEFAULT_TURBO_FORM_PARAMETERS =
{
  builder: ShoelaceFormBuilder,
}

Instance Method Summary collapse

Instance Method Details

#grouped_sl_options_for_select(grouped_options, options) ⇒ Object

Returns a string of <sl-menu-item> tags, like options_for_select, but prepends a <sl-menu-label> tag to each group.



359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 359

def grouped_sl_options_for_select(grouped_options, options)
  body = "".html_safe

  grouped_options.each_with_index do |container, index|
    label, values = container

    body.safe_concat(DIVIDER_TAG) if index > 0
    body.safe_concat(("sl-menu-label", label)) if label.present?
    body.safe_concat(sl_options_for_select(values, options))
  end

  body
end

#sl_button_tag(**attrs, &block) ⇒ Object

Creates a generic <sl-button> element.



318
319
320
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 318

def sl_button_tag(**attrs, &block)
  ("sl-button", **attrs, &block)
end

#sl_form_for(*args, **options, &block) ⇒ Object



275
276
277
278
279
280
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 275

def sl_form_for(*args, **options, &block)
  content = form_for(*args, **DEFAULT_FORM_PARAMETERS.deep_merge(options), &block)
  content[0, 5]  = OPENING_SL_FORM_TAG
  content[-7, 7] = CLOSING_SL_FORM_TAG
  content
end

#sl_form_tag(url_for_options = {}, options = {}, &block) ⇒ Object



289
290
291
292
293
294
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 289

def sl_form_tag(url_for_options = {}, options = {}, &block)
  content = form_tag(url_for_options, options.with_defaults(DEFAULT_FORM_PARAMETERS.except(:builder)), &block)
  content[0, 5]  = OPENING_SL_FORM_TAG
  content[-7, 7] = CLOSING_SL_FORM_TAG
  content
end

#sl_form_with(**args, &block) ⇒ Object



282
283
284
285
286
287
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 282

def sl_form_with(**args, &block)
  content = form_with(**args, **DEFAULT_FORM_PARAMETERS.except(:data), &block)
  content[0, 5]  = OPENING_SL_FORM_TAG
  content[-7, 7] = CLOSING_SL_FORM_TAG
  content
end

#sl_options_for_select(enumerable, options = nil) ⇒ Object

Accepts an enumerable (hash, array, enumerable, your type) and returns a string of sl-menu-item 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.



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 376

def sl_options_for_select(enumerable, options = nil)
  return enumerable if String === enumerable

  selected, disabled = extract_selected_and_disabled(options).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.('sl-menu-item', 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-menu-item> 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.



395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 395

def sl_options_from_collection_for_select(collection, value_method, text_method, selected = nil)
  options = 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)
  }

  sl_options_for_select(options, 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.



416
417
418
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 416

def sl_radio_button(object_name, method, tag_value, options = {}, &block)
  ShoelaceRadioButton.new(object_name, method, self, tag_value, options).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.



339
340
341
342
343
344
345
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 339

def sl_submit_tag(value = 'Save changes', **options)
  options = options.deep_stringify_keys
  tag_options = { "submit" => true, "type" => "primary" }.update(options)
  set_default_disable_with(value, tag_options)

  ('sl-button', value, tag_options)
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


353
354
355
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 353

def sl_text_field_tag(name, value = nil, **options, &block)
  ('sl-input', '', { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys), &block)
end

#sl_turbo_form_for(*args, **options, &block) ⇒ Object



296
297
298
299
300
301
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 296

def sl_turbo_form_for(*args, **options, &block)
  content = form_for(*args, **DEFAULT_TURBO_FORM_PARAMETERS.deep_merge(options), &block)
  content[0, 5]  = OPENING_SL_TURBO_FORM_TAG
  content[-7, 7] = CLOSING_SL_TURBO_FORM_TAG
  content
end

#sl_turbo_form_tag(url_for_options = {}, options = {}, &block) ⇒ Object



310
311
312
313
314
315
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 310

def sl_turbo_form_tag(url_for_options = {}, options = {}, &block)
  content = form_tag(url_for_options, options.with_defaults(DEFAULT_TURBO_FORM_PARAMETERS.except(:builder)), &block)
  content[0, 5]  = OPENING_SL_TURBO_FORM_TAG
  content[-7, 7] = CLOSING_SL_TURBO_FORM_TAG
  content
end

#sl_turbo_form_with(**args, &block) ⇒ Object



303
304
305
306
307
308
# File 'app/helpers/shoelace/rails/ui/form_helper.rb', line 303

def sl_turbo_form_with(**args, &block)
  content = form_with(**args, **DEFAULT_TURBO_FORM_PARAMETERS.except(:data), &block)
  content[0, 5]  = OPENING_SL_TURBO_FORM_TAG
  content[-7, 7] = CLOSING_SL_TURBO_FORM_TAG
  content
end