Class: Daisy::DataInput::SelectComponent::SelectOptionComponent

Inherits:
LocoMotion::BasicComponent show all
Defined in:
app/components/daisy/data_input/select_component.rb

Overview

Inner component for rendering individual select options as <option> elements.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LocoMotion::BasicComponent

name

Constructor Details

#initialize(value:, label:, selected: nil, disabled: false, css: "", html: {}, **kws) ⇒ SelectOptionComponent

Initialize a new select option component.

rubocop:disable Metrics/ParameterLists

Parameters:

  • value (String, Symbol, Integer)

    The value of the option.

  • label (String)

    The label to display for the option.

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

    Whether the option is selected. Defaults to nil, which means "inherit" — the parent select marks the option selected when its value matches the select's value:. Pass true or false to override that and force the option's selected state.

  • disabled (Boolean) (defaults to: false)

    Whether the option is disabled. Defaults to false.

  • css (String) (defaults to: "")

    CSS classes to apply to the option.

  • html (Hash) (defaults to: {})

    HTML attributes to apply to the option.



81
82
83
84
85
86
87
88
89
# File 'app/components/daisy/data_input/select_component.rb', line 81

def initialize(value:, label:, selected: nil, disabled: false, css: "", html: {}, **kws)
  @value = value
  @label = label
  @selected = selected
  @disabled = disabled
  @css = css
  @html = html
  super(**kws)
end

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



55
56
57
# File 'app/components/daisy/data_input/select_component.rb', line 55

def disabled
  @disabled
end

#labelObject (readonly)

Returns the value of attribute label.



55
56
57
# File 'app/components/daisy/data_input/select_component.rb', line 55

def label
  @label
end

#selectedObject

Writable so the parent Daisy::DataInput::SelectComponent can default it from its value: when the caller didn't set it explicitly (see Daisy::DataInput::SelectComponent#render_select_options).



60
61
62
# File 'app/components/daisy/data_input/select_component.rb', line 60

def selected
  @selected
end

#valueObject (readonly)

Returns the value of attribute value.



55
56
57
# File 'app/components/daisy/data_input/select_component.rb', line 55

def value
  @value
end

Instance Method Details

#callString

Renders the option element with the appropriate attributes.

Returns:

  • (String)

    The rendered HTML for the option element.



97
98
99
100
101
102
103
104
# File 'app/components/daisy/data_input/select_component.rb', line 97

def call
  (:option, label, {
    value: value,
    selected: selected,
    disabled: disabled,
    class: @css
  }.merge(@html))
end