Class: Proscenium::UI::Form::Fields::Select

Inherits:
Base
  • Object
show all
Defined in:
lib/proscenium/ui/form/fields/select.rb

Overview

Render a <select> input for the given model attribute. It will attempt to automatically build an appropriate set of <option>‘s, and supports ActiveRecord associations and enums. It will also detect if multiple options should be enabled or not.

## Supported options

  • options [Array] a list of options to use for the <select>. If this is given, the automatic

    detection of options will be disabled. A flat array of strings will be used for both the
    label and value. While an Array of nested two-level arrays will be used.
    
  • include_blank [Boolean, String] if true, will add an empty <option> to the <select>. If a

    String is given, it will be used as the label for the empty <option>.
    
  • label [String] the label to use for the field (default: humanized attribute name).

  • hint [String] the hint to use for the field (optional).

  • required [Boolean] if true, will add the ‘required` attribute to the <select> tag (default:

    false). Also supported as a bang attribute (e.g. `:required!`). See
    `Hue::Utils.merge_bang_attributes!`.
    
  • typeahead [Boolean] if true, will enable typeahead support by replacing with a SmartSelect

    React component (default: false). Also supported as a bang attribute (e.g. `:typeahead!`).
    See `Hue::Utils.merge_bang_attributes!`.
    

Instance Attribute Summary

Attributes inherited from Base

#attribute, #attributes, #form, #model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Proscenium::UI::Form::Fields::Base

Class Method Details

.css_module_pathObject

Ensure both the form and select field are side loaded.



32
33
34
# File 'lib/proscenium/ui/form/fields/select.rb', line 32

def self.css_module_path
  source_path.sub_ext('.module.css')
end

.sideload(_options) ⇒ Object



26
27
28
29
# File 'lib/proscenium/ui/form/fields/select.rb', line 26

def self.sideload(_options)
  # Proscenium::Importer.import Hue::Phlex::ReactComponent.manager
  Proscenium::Importer.sideload source_path, lazy: true
end

Instance Method Details

#before_templateObject



36
37
38
39
40
41
42
43
# File 'lib/proscenium/ui/form/fields/select.rb', line 36

def before_template
  # Defined here to ensure they are deleted from `attributes` before `final_attributes` is
  # called.
  @options_from_attributes = attributes.delete(:options)
  @include_blank = attributes.delete(:include_blank)

  super
end

#options_templateObject



68
69
70
71
72
# File 'lib/proscenium/ui/form/fields/select.rb', line 68

def options_template
  options.each do |value, opts|
    option(value:, selected: opts[:selected]) { opts[:label] }
  end
end

#view_template(&options_block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/proscenium/ui/form/fields/select.rb', line 45

def view_template(&options_block)
  field class: :@field do
    if !options_block && typeahead?
      @component_props = attributes.delete(:component_props) || {}

      # SmartSelect - or most likely React - does not like being wrapped in a label.
      div class: :@typeahead do
        label
        div(**final_attributes)
        hint
      end
    else
      label do
        multiple? && input(name: field_name, type: :hidden, value: '')
        select(name: field_name, **final_attributes) do
          options_block ? yield_content(&options_block) : options_template
        end
        hint
      end
    end
  end
end