Class: ShadcnPhlexcomponents::Command

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/command.rb

Constant Summary collapse

MODIFIER_KEYS =
[
  :ctrl,
  :alt,
  :shift,
]

Constants inherited from Base

Base::SANITIZER_ALLOWED_ATTRIBUTES, Base::SANITIZER_ALLOWED_TAGS, Base::TAILWIND_MERGER

Instance Method Summary collapse

Methods inherited from Base

#before_template, #convert_collection_hash_to_struct, #find_as_child, #icon, #item_disabled?, #merge_default_attributes, #merged_as_child_attributes, #nokogiri_attributes_to_hash, #overlay, #sanitize_as_child

Constructor Details

#initialize(open: false, modifier_key: nil, shortcut_key: nil, search_path: nil, search_error_text: "Something went wrong, please try again.", search_empty_text: "No results found", search_placeholder_text: "Search...", **attributes) ⇒ Command

Returns a new instance of Command.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/shadcn_phlexcomponents/components/command.rb', line 20

def initialize(
  open: false,
  modifier_key: nil,
  shortcut_key: nil,
  search_path: nil,
  search_error_text: "Something went wrong, please try again.",
  search_empty_text: "No results found",
  search_placeholder_text: "Search...",
  **attributes
)
  if modifier_key && !MODIFIER_KEYS.include?(modifier_key)
    raise ArgumentError, "Expected one of #{MODIFIER_KEYS} for \"modifier_key\", got #{modifier_key}"
  end

  @open = open
  @modifier_key = modifier_key
  @shortcut_key = shortcut_key
  @search_path = search_path
  @search_error_text = search_error_text
  @search_empty_text = search_empty_text
  @search_placeholder_text = search_placeholder_text
  @aria_id = "command-#{SecureRandom.hex(5)}"
  super(**attributes)
end

Instance Method Details

#content(**attributes) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/shadcn_phlexcomponents/components/command.rb', line 49

def content(**attributes, &)
  CommandContent(
    search_error_text: @search_error_text,
    search_empty_text: @search_empty_text,
    search_placeholder_text: @search_placeholder_text,
    aria_id: @aria_id,
    **attributes,
    &
  )
end

#default_attributesObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shadcn_phlexcomponents/components/command.rb', line 72

def default_attributes
  {
    data: {
      controller: "command",
      command_is_open_value: @open.to_s,
      modifier_key: @modifier_key,
      shortcut_key: @shortcut_key,
      search_path: @search_path,
    },
  }
end

#group(**attributes) ⇒ Object



68
69
70
# File 'lib/shadcn_phlexcomponents/components/command.rb', line 68

def group(**attributes, &)
  CommandGroup(aria_id: @aria_id, **attributes, &)
end

#item(**attributes) ⇒ Object



60
61
62
# File 'lib/shadcn_phlexcomponents/components/command.rb', line 60

def item(**attributes, &)
  CommandItem(aria_id: @aria_id, **attributes, &)
end

#label(**attributes) ⇒ Object



64
65
66
# File 'lib/shadcn_phlexcomponents/components/command.rb', line 64

def label(**attributes, &)
  CommandLabel(**attributes, &)
end

#trigger(**attributes) ⇒ Object



45
46
47
# File 'lib/shadcn_phlexcomponents/components/command.rb', line 45

def trigger(**attributes, &)
  CommandTrigger(modifier_key: @modifier_key, shortcut_key: @shortcut_key, aria_id: @aria_id, **attributes, &)
end

#view_templateObject



84
85
86
87
88
89
90
# File 'lib/shadcn_phlexcomponents/components/command.rb', line 84

def view_template(&)
  div(**@attributes) do
    overlay("command")

    yield
  end
end