Skip to content

Commit 444f59e

Browse files
Merge pull request #298 from basemate/add_data_component
add data core component & tests & docs
2 parents 39b3598 + 39203a2 commit 444f59e

File tree

5 files changed

+97
-0
lines changed

5 files changed

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

docs/components/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ You can build your [own components](/docs/extend/README.md) as well, both static
1919
- [caption](/docs/components/caption.md)
2020
- [cite](/docs/components/cite.md)
2121
- [code](/docs/components/code.md)
22+
- [data](/docs/components/data.md)
2223
- [dl](/docs/components/dl.md)
2324
- dd
2425
- dt

docs/components/data.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# matestack core component: Data
2+
3+
Show [specs](/spec/usage/components/data_spec.rb)
4+
5+
The HTML `<data>` tag implemented in ruby.
6+
7+
## Parameters
8+
9+
This component can take 2 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 `<data>` should have.
13+
14+
#### # class (optional)
15+
Expects a string with all classes the `<data>` should have.
16+
17+
#### # value (optional)
18+
Expects a string and specifies the machine-readable translation of the content of the `<data>` element.
19+
20+
## Example 1: Yield a given block
21+
22+
```ruby
23+
data id: 'foo', class: 'bar', value: '1300' do
24+
plain 'Data example 1' # optional content
25+
end
26+
```
27+
28+
returns
29+
30+
```html
31+
<data id="foo" class="bar" value="1300">
32+
Data example 1
33+
</data>
34+
```
35+
36+
## Example 2: Render `options[:text]` param
37+
38+
```ruby
39+
data id: 'foo', class: 'bar', value: '1301', text: 'Data example 2'
40+
```
41+
42+
returns
43+
44+
```html
45+
<data id="foo" class="bar" value="1301">
46+
Data example 2
47+
</data>

spec/usage/components/data_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require_relative '../../support/utils'
2+
include Utils
3+
4+
describe 'Data component', type: :feature, js: true do
5+
it 'Renders a simple and enhanced data tag on a page' do
6+
class ExamplePage < Matestack::Ui::Page
7+
def response
8+
components {
9+
# Simple data
10+
data id: 'foo', class: 'bar', value: '1301', text: 'Data example 1'
11+
12+
# Enhanced data
13+
data id: 'foo', class: 'bar', value: '1300' do
14+
plain 'Data example 2' # optional content
15+
end
16+
}
17+
end
18+
end
19+
20+
visit '/example'
21+
22+
static_output = page.html
23+
24+
expected_static_output = <<~HTML
25+
<data id="foo" value="1301" class="bar">
26+
Data example 1
27+
</data>
28+
<data id="foo" value="1300" class="bar">
29+
Data example 2
30+
</data>
31+
HTML
32+
33+
expect(stripped(static_output)).to include(stripped(expected_static_output))
34+
end
35+
end

0 commit comments

Comments
 (0)