Skip to content

Commit 6b740b0

Browse files
committed
add optgroup matestack core component
Closes #202
1 parent 1bb4424 commit 6b740b0

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%optgroup{@tag_attributes}
2+
- if block_given?
3+
= yield
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Matestack::Ui::Core::Optgroup
2+
class Optgroup < Matestack::Ui::Core::Component::Static
3+
def setup
4+
@tag_attributes.merge!(
5+
disabled: options[:disabled] ||= nil,
6+
label: options[:label]
7+
)
8+
end
9+
end
10+
end

docs/components/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ You can build your [own components](/docs/extend/README.md) as well, both static
5050
- [nav](/docs/components/nav.md)
5151
- [noscript](/docs/components/noscript.md)
5252
- [object](/docs/components/object.md)
53+
- [optgroup](/docs/components/optgroup.md)
5354
- [option](/docs/components/option.md)
5455
- [output](/docs/components/output.md)
5556
- [paragraph](/docs/components/paragraph.md)

docs/components/optgroup.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# matestack core component: Optgroup
2+
3+
Show [specs](/spec/usage/components/optgroup_spec.rb)
4+
5+
The HTML `<optgroup>` tag implemented in ruby.
6+
7+
## Parameters
8+
9+
This component can take 4 optional configuration params and yield the passed content.
10+
11+
#### # id (optional)
12+
Expects a string with all ids the `<optgroup>` should have.
13+
14+
#### # class (optional)
15+
Expects a string with all classes the `<optgroup>` should have.
16+
17+
#### # disabled (optional)
18+
Specifies that the `<optgroup>` should be disabled.
19+
20+
#### # label (optional)
21+
Specifies a label for the `<optgroup>`.
22+
23+
## Example: Yield a given block
24+
25+
```ruby
26+
optgroup label: 'Swedish Cars' do
27+
option text: 'Volvo', value: 'volvo'
28+
option text: 'Saab', value: 'saab'
29+
end
30+
```
31+
32+
returns
33+
34+
```html
35+
<optgroup label="Swedish Cars">
36+
<option value="volvo">Volvo</option>
37+
<option value="saab">Saab</option>
38+
</optgroup>
39+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
require_relative '../../support/utils'
2+
include Utils
3+
4+
describe 'Optgroup component', type: :feature, js: true do
5+
it 'Renders a optgroup tag on a page' do
6+
class ExamplePage < Matestack::Ui::Page
7+
def response
8+
components {
9+
# Just the tag
10+
optgroup
11+
12+
# The tag with a label
13+
optgroup label: 'Optgroup'
14+
15+
# Optgroup tag without label but sub-components
16+
optgroup do
17+
option text: 'Option A'
18+
option text: 'Option B'
19+
option text: 'Option C'
20+
end
21+
22+
# Simple optgroup
23+
optgroup label: 'Simple Group' do
24+
option text: 'Option D'
25+
option text: 'Option E'
26+
option text: 'Option F'
27+
end
28+
29+
# Enhanced optgroups
30+
optgroup label: 'Enabled Group 1', class: 'enabled' do
31+
option text: 'Option G'
32+
option text: 'Option H'
33+
option text: 'Option I'
34+
end
35+
36+
optgroup label: 'Disabled Group 2', disabled: true, id: 'disabled' do
37+
option text: 'Option J'
38+
option text: 'Option K'
39+
option text: 'Option L'
40+
end
41+
42+
optgroup label: 'Not Disabled Group 3', disabled: false, class: 'group', id: 'not-disabled' do
43+
option text: 'Option M'
44+
option text: 'Option N'
45+
option text: 'Option O'
46+
end
47+
}
48+
end
49+
end
50+
51+
visit '/example'
52+
53+
static_output = page.html
54+
55+
File.write("static.html", static_output)
56+
57+
expected_static_output = <<~HTML
58+
<optgroup></optgroup>
59+
<optgroup label="Optgroup"></optgroup>
60+
<optgroup>
61+
<option>Option A</option>
62+
<option>Option B</option>
63+
<option>Option C</option>
64+
</optgroup>
65+
<optgroup label="Simple Group">
66+
<option>Option D</option>
67+
<option>Option E</option>
68+
<option>Option F</option>
69+
</optgroup>
70+
<optgroup label="Enabled Group 1" class="enabled">
71+
<option>Option G</option>
72+
<option>Option H</option>
73+
<option>Option I</option>
74+
</optgroup>
75+
<optgroup disabled="disabled" id="disabled" label="Disabled Group 2">
76+
<option>Option J</option>
77+
<option>Option K</option>
78+
<option>Option L</option>
79+
</optgroup>
80+
<optgroup id="not-disabled" label="Not Disabled Group 3" class="group">
81+
<option>Option M</option>
82+
<option>Option N</option>
83+
<option>Option O</option>
84+
</optgroup>
85+
HTML
86+
87+
expect(stripped(static_output)).to include(stripped(expected_static_output))
88+
end
89+
end

0 commit comments

Comments
 (0)