Skip to content

Commit f882429

Browse files
authored
[Feature] Add NativeSelect component (#344)
1 parent 4e4fd14 commit f882429

6 files changed

Lines changed: 214 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class NativeSelect < Base
5+
def initialize(size: :default, **attrs)
6+
@size = size
7+
super(**attrs)
8+
end
9+
10+
def view_template(&block)
11+
div(
12+
class: "group/native-select relative w-fit has-[select:disabled]:opacity-50"
13+
) do
14+
select(**attrs, &block)
15+
render RubyUI::NativeSelectIcon.new
16+
end
17+
end
18+
19+
private
20+
21+
def default_attrs
22+
{
23+
data: {
24+
ruby_ui__form_field_target: "input",
25+
action: "change->ruby-ui--form-field#onChange invalid->ruby-ui--form-field#onInvalid"
26+
},
27+
class: [
28+
"border-border bg-transparent text-sm w-full min-w-0 appearance-none rounded-md border py-1 pr-8 pl-2.5 shadow-xs transition-[color,box-shadow] outline-none select-none ring-0 ring-ring/0",
29+
"placeholder:text-muted-foreground",
30+
"selection:bg-primary selection:text-primary-foreground",
31+
"focus-visible:outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-2",
32+
"disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50",
33+
"aria-invalid:ring-destructive/20 aria-invalid:border-destructive aria-invalid:ring-2",
34+
(@size == :sm) ? "h-7 rounded-md py-0.5" : "h-9"
35+
]
36+
}
37+
end
38+
end
39+
end
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# frozen_string_literal: true
2+
3+
class Views::Docs::NativeSelect < Views::Base
4+
def view_template
5+
component = "NativeSelect"
6+
7+
div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do
8+
render Docs::Header.new(title: "Native Select", description: "A styled native HTML select element with consistent design system integration.")
9+
10+
Heading(level: 2) { "Usage" }
11+
12+
render Docs::VisualCodeExample.new(title: "Default", context: self) do
13+
<<~RUBY
14+
div(class: "grid w-full max-w-sm items-center gap-1.5") do
15+
NativeSelect do
16+
NativeSelectOption(value: "") { "Select a fruit" }
17+
NativeSelectOption(value: "apple") { "Apple" }
18+
NativeSelectOption(value: "banana") { "Banana" }
19+
NativeSelectOption(value: "blueberry") { "Blueberry" }
20+
NativeSelectOption(value: "pineapple") { "Pineapple" }
21+
end
22+
end
23+
RUBY
24+
end
25+
26+
render Docs::VisualCodeExample.new(title: "Groups", description: "Use NativeSelectGroup to organize options into categories.", context: self) do
27+
<<~RUBY
28+
div(class: "grid w-full max-w-sm items-center gap-1.5") do
29+
NativeSelect do
30+
NativeSelectOption(value: "") { "Select a department" }
31+
NativeSelectGroup(label: "Engineering") do
32+
NativeSelectOption(value: "frontend") { "Frontend" }
33+
NativeSelectOption(value: "backend") { "Backend" }
34+
NativeSelectOption(value: "devops") { "DevOps" }
35+
end
36+
NativeSelectGroup(label: "Sales") do
37+
NativeSelectOption(value: "account_executive") { "Account Executive" }
38+
NativeSelectOption(value: "sales_development") { "Sales Development" }
39+
end
40+
end
41+
end
42+
RUBY
43+
end
44+
45+
render Docs::VisualCodeExample.new(title: "Disabled", description: "Add the disabled attribute to the NativeSelect component to disable the select.", context: self) do
46+
<<~RUBY
47+
div(class: "grid w-full max-w-sm items-center gap-1.5") do
48+
NativeSelect(disabled: true) do
49+
NativeSelectOption(value: "") { "Select a fruit" }
50+
NativeSelectOption(value: "apple") { "Apple" }
51+
NativeSelectOption(value: "banana") { "Banana" }
52+
NativeSelectOption(value: "blueberry") { "Blueberry" }
53+
end
54+
end
55+
RUBY
56+
end
57+
58+
render Docs::VisualCodeExample.new(title: "Invalid", description: "Use aria-invalid to show validation errors.", context: self) do
59+
<<~RUBY
60+
div(class: "grid w-full max-w-sm items-center gap-1.5") do
61+
NativeSelect(aria: {invalid: "true"}) do
62+
NativeSelectOption(value: "") { "Select a fruit" }
63+
NativeSelectOption(value: "apple") { "Apple" }
64+
NativeSelectOption(value: "banana") { "Banana" }
65+
NativeSelectOption(value: "blueberry") { "Blueberry" }
66+
end
67+
end
68+
RUBY
69+
end
70+
71+
Heading(level: 2) { "Native Select vs Select" }
72+
73+
div(class: "space-y-2 text-sm text-muted-foreground") do
74+
p { "NativeSelect: Choose for native browser behavior, superior performance, or mobile-optimized dropdowns." }
75+
p { "Select: Choose for custom styling, animations, or complex interactions." }
76+
end
77+
78+
render Components::ComponentSetup::Tabs.new(component_name: component)
79+
80+
render Docs::ComponentsTable.new(component_files(component))
81+
end
82+
end
83+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class NativeSelectGroup < Base
5+
def view_template(&)
6+
optgroup(**attrs, &)
7+
end
8+
9+
private
10+
11+
def default_attrs
12+
{}
13+
end
14+
end
15+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class NativeSelectIcon < Base
5+
def view_template(&block)
6+
span(**attrs) do
7+
if block
8+
block.call
9+
else
10+
icon
11+
end
12+
end
13+
end
14+
15+
private
16+
17+
def icon
18+
svg(
19+
xmlns: "http://www.w3.org/2000/svg",
20+
viewbox: "0 0 24 24",
21+
fill: "none",
22+
stroke: "currentColor",
23+
stroke_width: "2",
24+
stroke_linecap: "round",
25+
stroke_linejoin: "round",
26+
class: "size-4",
27+
aria_hidden: "true"
28+
) do |s|
29+
s.path(d: "m6 9 6 6 6-6")
30+
end
31+
end
32+
33+
def default_attrs
34+
{
35+
class: "text-muted-foreground pointer-events-none absolute top-1/2 right-2.5 -translate-y-1/2 select-none"
36+
}
37+
end
38+
end
39+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class NativeSelectOption < Base
5+
def view_template(&)
6+
option(**attrs, &)
7+
end
8+
9+
private
10+
11+
def default_attrs
12+
{}
13+
end
14+
end
15+
end

test/ruby_ui/native_select_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class RubyUI::NativeSelectTest < ComponentTest
6+
def test_render_with_all_items
7+
output = phlex do
8+
RubyUI.NativeSelect(name: "department") do
9+
RubyUI.NativeSelectOption(value: "") { "Select a department" }
10+
RubyUI.NativeSelectGroup(label: "Engineering") do
11+
RubyUI.NativeSelectOption(value: "frontend") { "Frontend" }
12+
RubyUI.NativeSelectOption(value: "backend") { "Backend" }
13+
end
14+
RubyUI.NativeSelectGroup(label: "Sales") do
15+
RubyUI.NativeSelectOption(value: "account_executive") { "Account Executive" }
16+
end
17+
end
18+
end
19+
20+
assert_match(/Frontend/, output)
21+
assert_match('name="department"', output)
22+
end
23+
end

0 commit comments

Comments
 (0)