Skip to content

Commit 1bb4424

Browse files
committed
add option matestack core component
Closes #203
1 parent feb02c6 commit 1bb4424

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%option{@tag_attributes}
2+
- if options[:text].blank? && block_given?
3+
= yield
4+
- else
5+
= options[:text]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Matestack::Ui::Core::Option
2+
class Option < Matestack::Ui::Core::Component::Static
3+
def setup
4+
@tag_attributes.merge!(
5+
disabled: options[:disabled] ||= nil,
6+
selected: options[:selected] ||= nil,
7+
label: options[:label],
8+
value: options[:value]
9+
)
10+
end
11+
end
12+
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+
- [option](/docs/components/option.md)
5354
- [output](/docs/components/output.md)
5455
- [paragraph](/docs/components/paragraph.md)
5556
- [param](/docs/components/param.md)

docs/components/option.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# matestack core component: Option
2+
3+
Show [specs](/spec/usage/components/option_spec.rb)
4+
5+
The HTML `<option>` tag implemented in ruby.
6+
7+
## Parameters
8+
9+
This component can take 7 optional configuration params and either yield content or display what gets passed to the `text` configuration param.
10+
11+
#### # id (optional)
12+
Expects a string with all ids the `<option>` should have.
13+
14+
#### # class (optional)
15+
Expects a string with all classes the `<option>` should have.
16+
17+
#### # disabled (optional)
18+
Specifies that the `<option>` should be disabled.
19+
20+
#### # label (optional)
21+
Specifies a shorter label for the `<option>`.
22+
23+
#### # text (optional)
24+
Specifies the text the `<option>` should contain.
25+
26+
#### # selected (optional)
27+
Specifies that the `<option>` should be pre-selected when the page loads.
28+
29+
#### # value (optional)
30+
Specifies the value to be sent to a server.
31+
32+
33+
## Example 1: Render `options[:text]` param
34+
35+
```ruby
36+
option text: 'Option 1'
37+
```
38+
39+
returns
40+
41+
```html
42+
<option>Option 1</option>
43+
```
44+
45+
## Example 2: Render `options[:label]` param
46+
47+
```ruby
48+
option label: 'Option 2'
49+
```
50+
51+
returns
52+
53+
```html
54+
<option label="Option 2"></option>
55+
```
56+
57+
## Example 3: Render with more attributes
58+
59+
```ruby
60+
option disabled: true, label: 'Option 3', selected: true, value: '3'
61+
```
62+
63+
returns
64+
65+
```html
66+
<option disabled="disabled" label="Option 3" selected="selected" value="3"></option>
67+
```
68+
69+
## Example 4: Yield a given block
70+
71+
```ruby
72+
option id: 'foo', class: 'bar' do
73+
plain 'Option 4'
74+
end
75+
```
76+
77+
returns
78+
79+
```html
80+
<option id="foo" class="bar">
81+
Option 4
82+
</option>
83+
```

spec/usage/components/option_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require_relative '../../support/utils'
2+
include Utils
3+
4+
describe 'Option component', type: :feature, js: true do
5+
it 'Renders a option tag on a page' do
6+
class ExamplePage < Matestack::Ui::Page
7+
def response
8+
components {
9+
option text: 'TEXT text'
10+
option label: 'TEXT label'
11+
option text: 'TEXT text', label: 'TEXT label'
12+
13+
option id: 'my-id', class: 'my-class' do
14+
plain 'TEXT plain'
15+
end
16+
17+
option text: 'TEXT text' do
18+
plain 'TEXT plain'
19+
end
20+
21+
option text: 'TEXT text', label: 'TEXT label' do
22+
plain 'TEXT plain'
23+
end
24+
25+
option disabled: true, label: 'TEXT label', selected: true, value: 'value'
26+
option disabled: false, label: 'TEXT label', selected: true, value: 'value'
27+
option disabled: true, label: 'TEXT label', selected: false, value: 'value'
28+
option disabled: false, label: 'TEXT label', selected: false, value: 'value'
29+
}
30+
end
31+
end
32+
33+
visit '/example'
34+
35+
static_output = page.html
36+
37+
expected_static_output = <<~HTML
38+
<option>TEXT text</option>
39+
<option label="TEXT label"></option>
40+
<option label="TEXT label">TEXT text</option>
41+
<option id="my-id" class="my-class">TEXT plain</option>
42+
<option>TEXT text</option>
43+
<option label="TEXT label">TEXT text</option>
44+
<option disabled="disabled" label="TEXT label" selected="selected" value="value"></option>
45+
<option label="TEXT label" selected="selected" value="value"></option>
46+
<option disabled="disabled" label="TEXT label" value="value"></option>
47+
<option label="TEXT label" value="value"></option>
48+
HTML
49+
50+
expect(stripped(static_output)).to include(stripped(expected_static_output))
51+
end
52+
end

0 commit comments

Comments
 (0)