Class: IronAdmin::Form::SelectComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
Concerns::FormInputBehavior
Defined in:
app/components/iron_admin/form/select_component.rb

Overview

Renders a select dropdown field.

Examples:

Basic select

render IronAdmin::Form::SelectComponent.new(
  name: "record[status]",
  options: %w[pending active completed],
  selected: @record.status
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, options:, selected: nil, include_blank: nil, disabled: false, has_error: false, field: nil, current_user: nil) ⇒ SelectComponent

Returns a new instance of SelectComponent.

Parameters:

  • name (String)

    Select name

  • options (Array)

    Available options

  • selected (String, nil) (defaults to: nil)

    Selected value

  • include_blank (String, nil) (defaults to: nil)

    Blank option text

  • disabled (Boolean) (defaults to: false)

    Disabled state

  • has_error (Boolean) (defaults to: false)

    Error state

  • field (IronAdmin::Field, nil) (defaults to: nil)

    Field config

  • current_user (Object, nil) (defaults to: nil)

    Current user



40
41
42
43
44
45
46
47
48
49
50
# File 'app/components/iron_admin/form/select_component.rb', line 40

def initialize(name:, options:, selected: nil, include_blank: nil,
               disabled: false, has_error: false, field: nil, current_user: nil)
  @name = name
  @options = options
  @selected = selected
  @include_blank = include_blank
  @disabled = disabled
  @has_error = has_error
  @field = field
  @current_user = current_user
end

Instance Attribute Details

#has_errorBoolean (readonly)

Returns Whether input has error state.

Returns:

  • (Boolean)

    Whether input has error state



30
31
32
# File 'app/components/iron_admin/form/select_component.rb', line 30

def has_error
  @has_error
end

#include_blankString? (readonly)

Returns Blank option text.

Returns:

  • (String, nil)

    Blank option text



27
28
29
# File 'app/components/iron_admin/form/select_component.rb', line 27

def include_blank
  @include_blank
end

#nameString (readonly)

Returns Select name attribute.

Returns:

  • (String)

    Select name attribute



18
19
20
# File 'app/components/iron_admin/form/select_component.rb', line 18

def name
  @name
end

#optionsArray (readonly)

Returns Available options.

Returns:

  • (Array)

    Available options



21
22
23
# File 'app/components/iron_admin/form/select_component.rb', line 21

def options
  @options
end

#selectedString? (readonly)

Returns Currently selected value.

Returns:

  • (String, nil)

    Currently selected value



24
25
26
# File 'app/components/iron_admin/form/select_component.rb', line 24

def selected
  @selected
end

Instance Method Details

#chevron_styleString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Inline CSS style for dropdown chevron.

Returns:

  • (String)

    Inline CSS style for dropdown chevron



65
66
67
68
69
70
# File 'app/components/iron_admin/form/select_component.rb', line 65

def chevron_style
  "background-image: url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' " \
    "viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' " \
    "stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e\"); background-position: right 0.5rem center; " \
    "background-repeat: no-repeat; background-size: 1.5em 1.5em; padding-right: 2.5rem;"
end

#select_classesString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns CSS classes for select element.

Returns:

  • (String)

    CSS classes for select element



54
55
56
57
58
59
60
61
# File 'app/components/iron_admin/form/select_component.rb', line 54

def select_classes
  base = "block w-full appearance-none border px-3 py-2 text-sm shadow-sm outline-none " \
         "transition duration-150 ease-in-out #{theme.border_radius} #{theme.input_border} " \
         "#{theme.card_bg} #{theme.body_text} #{theme.input_focus}"
  base += " !border-red-400 !focus:border-red-500 !focus:ring-red-500/20" if has_error
  base += " bg-gray-50 cursor-not-allowed" if effectively_disabled?
  base
end