Sha256: 2a186b58e4a76104e9e527de74f94dcad2896ac8569796cdf2c61bc3cb411f2f

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

module Formstrap
  class SelectView < ViewModel
    include Formstrap::Hintable
    include Formstrap::InputGroupable
    include Formstrap::Labelable
    include Formstrap::Listable
    include Formstrap::Placeholderable
    include Formstrap::Validatable
    include Formstrap::Wrappable

    def input_options
      keys = attributes - %i[append attribute collection float form input_group include_blank label prepend validate selected tags wrapper]
      options = to_h.slice(*keys)
      default_input_options.deep_merge(options)
    end

    def input_group_options
      default_input_group_options
        .deep_merge(label_input_group_options)
        .deep_merge(@input_group || {})
    end

    def wrapper_options
      default_wrapper_options.deep_merge({
        class: ["mb-3", ("form-floating" if float)]
      }).deep_merge(@wrapper || {})
    end

    def select_options
      keys = %i[include_blank selected]
      options = to_h.slice(*keys)
      default_options.deep_merge(options)
    end

    private

    def default_options
      selected = attribute.nil? ? nil : form.object&.send(attribute)
      {
        selected: selected
      }
    end

    def default_input_options
      {
        aria: {describedby: validation_id},
        class: [form_control_class, validation_class],
        data: {
          tags: tags,
          controller: "select",
          "select_selected_value": select_options[:selected]
        },
        multiple: tags,
        placeholder: placeholder
      }
    end

    def form_control_class
      plaintext ? "form-control-plaintext" : "form-select"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
formstrap-0.4.5 app/models/formstrap/select_view.rb
formstrap-0.4.4 app/models/formstrap/select_view.rb