Class: RubyUIAdmin::UI::Select

Inherits:
Base
  • Object
show all
Defined in:
app/components/ruby_ui_admin/ui/select.rb

Overview

Convenience wrapper around RubyUI's NativeSelect: takes an array of [label, value] options (plus selected:/include_blank:) instead of explicit option components, so call sites stay terse. Renders the styled native select with a custom chevron.

Constant Summary

Constants inherited from Base

Base::TAILWIND_MERGER

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Constructor Details

#initialize(options: [], selected: nil, include_blank: false, **attrs) ⇒ Select

Returns a new instance of Select.



9
10
11
12
13
14
# File 'app/components/ruby_ui_admin/ui/select.rb', line 9

def initialize(options: [], selected: nil, include_blank: false, **attrs)
  @options = options
  @selected = selected
  @include_blank = include_blank
  super(**attrs)
end

Instance Method Details

#view_templateObject



16
17
18
19
20
21
22
23
24
25
26
# File 'app/components/ruby_ui_admin/ui/select.rb', line 16

def view_template
  render NativeSelect.new(**attrs) do
    render(NativeSelectOption.new(value: "")) { "" } if @include_blank

    @options.each do |(label, value)|
      opts = {value: value}
      opts[:selected] = true if value.to_s == @selected.to_s
      render(NativeSelectOption.new(**opts)) { label.to_s }
    end
  end
end