Class: Shadcn::Select::Component

Inherits:
ApplicationViewComponent show all
Includes:
Concerns::SubmitsValue
Defined in:
app/components/shadcn/select/component.rb

Overview

Port of registry/new-york-v4/ui/select.tsx

A role="combobox" trigger driving a role="listbox" layer. When name: is given a hidden input mirrors the selection, the way Radix's BubbleSelect does, so the control submits with the form.

Constant Summary

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods included from Concerns::SubmitsValue

included

Methods inherited from ApplicationViewComponent

#css_classes, default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #style_variants, #tag_name

Constructor Details

#initialize(value: nil, name: nil, placeholder: nil, open: false, side: :bottom, align: :center, side_offset: 4, searchable: false, **attributes) ⇒ Component

Returns a new instance of Component.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/components/shadcn/select/component.rb', line 26

def initialize(value: nil, name: nil, placeholder: nil, open: false,
               side: :bottom, align: :center, side_offset: 4, searchable: false,
               **attributes)
  @searchable = searchable
  @value = value.to_s
  @name = name
  @placeholder = placeholder.to_s
  @open = open
  @side = side&.to_sym || :bottom
  @align = align&.to_sym || :center
  @side_offset = side_offset
  super(**attributes)
end

Instance Attribute Details

#alignObject (readonly)

Returns the value of attribute align.



24
25
26
# File 'app/components/shadcn/select/component.rb', line 24

def align
  @align
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'app/components/shadcn/select/component.rb', line 24

def name
  @name
end

#openObject (readonly)

Returns the value of attribute open.



24
25
26
# File 'app/components/shadcn/select/component.rb', line 24

def open
  @open
end

#placeholderObject (readonly)

Returns the value of attribute placeholder.



24
25
26
# File 'app/components/shadcn/select/component.rb', line 24

def placeholder
  @placeholder
end

#searchableObject (readonly)

Returns the value of attribute searchable.



24
25
26
# File 'app/components/shadcn/select/component.rb', line 24

def searchable
  @searchable
end

#sideObject (readonly)

Returns the value of attribute side.



24
25
26
# File 'app/components/shadcn/select/component.rb', line 24

def side
  @side
end

#side_offsetObject (readonly)

Returns the value of attribute side_offset.



24
25
26
# File 'app/components/shadcn/select/component.rb', line 24

def side_offset
  @side_offset
end

#valueObject (readonly)

Returns the value of attribute value.



24
25
26
# File 'app/components/shadcn/select/component.rb', line 24

def value
  @value
end

Instance Method Details

#callObject



54
55
56
# File 'app/components/shadcn/select/component.rb', line 54

def call
  render_element(body: safe_join([ trigger, select_content, content, hidden_input ].compact))
end

#element_attributes(**defaults) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/components/shadcn/select/component.rb', line 40

def element_attributes(**defaults)
  super(**{
    style: merged_style(CONTENTS_STYLE),
    "data-controller" => "shadcn--select",
    "data-shadcn--select-open-value" => open,
    "data-shadcn--select-value-value" => value,
    "data-shadcn--select-placeholder-value" => placeholder,
    "data-shadcn--select-side-value" => side,
    "data-shadcn--select-align-value" => align,
    "data-shadcn--select-side-offset-value" => side_offset,
    "data-shadcn--select-searchable-value" => searchable
  }.merge(defaults))
end