Class: NitroKit::Combobox

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/combobox.rb

Constant Summary collapse

PLACEMENTS =
%i[bottom_start bottom_end top_start top_end].freeze

Constants inherited from Component

NitroKit::Component::ADDITIVE_DATA_ATTRIBUTES, NitroKit::Component::COMPONENT_OWNED_DATA_ATTRIBUTES, NitroKit::Component::FORBIDDEN_ATTRIBUTES, NitroKit::Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, label:, options:, value: nil, placeholder: nil, include_blank: true, placement: :bottom_start, required: false, disabled: false, autocomplete: "off", html: {}, aria: {}, data: {}, control_aria: {}, desperately_need_a_class: nil) ⇒ Combobox

Returns a new instance of Combobox.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/components/nitro_kit/combobox.rb', line 7

def initialize(
  id:,
  name:,
  label:,
  options:,
  value: nil,
  placeholder: nil,
  include_blank: true,
  placement: :bottom_start,
  required: false,
  disabled: false,
  autocomplete: "off",
  html: {},
  aria: {},
  data: {},
  control_aria: {},
  desperately_need_a_class: nil
)
  @identifier = component_id(id)
  @name = form_name(name)
  @label = visible_label(label)
  @options = typed_options(options)
  @value = value
  @selected_option = selected_option(value)
  @placeholder = validate_optional_text!(:placeholder, placeholder)
  @include_blank = validate_blank_option!(include_blank)
  @placement = validate_choice!(:placement, placement, PLACEMENTS)
  @required = validate_boolean!(:required, required)
  @disabled = validate_boolean!(:disabled, disabled)
  @autocomplete = autocomplete
  @control_aria = attribute_hash(control_aria, name: "control_aria")

  if !value.nil? && !@selected_option
    raise ArgumentError, "Combobox value #{value.inspect} does not match a declared option"
  end
  if @label == false && !named_by_control_aria?
    raise ArgumentError,
      "a Combobox without label: requires control_aria: { label: } or control_aria: { labelledby: }"
  end

  super(
    component: :combobox,
    attributes: {
      id: @identifier,
      data: {
        controller: "nk--combobox",
        placement: placement_value,
        state: "closed",
        action: "click@window->nk--combobox#closeFromOutside",
        nk__combobox_open_value: "false",
        nk__combobox_required_value: @required,
        nk__combobox_invalid_selection_value: I18n.t("nitro_kit.combobox.invalid_selection"),
        nk__combobox_no_results_value: I18n.t("nitro_kit.combobox.no_results"),
        nk__combobox_results_one_value: I18n.t("nitro_kit.combobox.results.one"),
        nk__combobox_results_other_value: I18n.t("nitro_kit.combobox.results.other")
      }
    },
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



71
72
73
# File 'app/components/nitro_kit/combobox.rb', line 71

def identifier
  @identifier
end

#labelObject (readonly)

Returns the value of attribute label.



71
72
73
# File 'app/components/nitro_kit/combobox.rb', line 71

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



71
72
73
# File 'app/components/nitro_kit/combobox.rb', line 71

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



71
72
73
# File 'app/components/nitro_kit/combobox.rb', line 71

def options
  @options
end

#placementObject (readonly)

Returns the value of attribute placement.



71
72
73
# File 'app/components/nitro_kit/combobox.rb', line 71

def placement
  @placement
end

#valueObject (readonly)

Returns the value of attribute value.



71
72
73
# File 'app/components/nitro_kit/combobox.rb', line 71

def value
  @value
end

Instance Method Details

#view_templateObject



73
74
75
76
77
78
79
80
81
# File 'app/components/nitro_kit/combobox.rb', line 73

def view_template
  div(**root_attributes) do
    render_label
    render_control
    render_native_select
    render_listbox
    render_status
  end
end