Skip to content

Commit 082b669

Browse files
authored
Merge pull request #96 from basemate/add-onlick-docs-and-tests
Adds docs and tests for onclick component
2 parents 6f42c9e + e9c9a5d commit 082b669

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

docs/components/onclick.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
# matestack core component: Action
1+
# matestack core component: Onclick
22

3-
Show [specs](../../spec/usage/components/action_spec.rb)
3+
Show [specs](../../spec/usage/components/onclick_spec.rb)
4+
5+
The `onclick` component renders a div that runs a function when the user clicks on it. This is a simple component that can be used to wrap components with a onclick function that will emit a event. The event that must be emitted onclick can defined by passing a hash into the `onclick` component that has the key `emit`. See example below for more details.
6+
7+
```ruby
8+
class Pages::MyPage::Home < Matestack::Ui::Page
9+
def response
10+
components{
11+
onclick(emit: "abc") do
12+
plain "Hello world"
13+
end
14+
}
15+
async rerender_on: "abc" do
16+
plain "Render this text when the 'abc' event is emitted"
17+
end
18+
end
19+
end
20+
```

spec/usage/components/onclick_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require_relative "../../support/utils"
2+
include Utils
3+
4+
describe "Onclick Component", type: :feature, js: true do
5+
6+
it "Example 1" do
7+
class ExamplePage < Matestack::Ui::Page
8+
def response
9+
components {
10+
async show_on: 'show_message' do
11+
div id: 'the_message' do
12+
plain "{{event.data.message}}"
13+
end
14+
end
15+
onclick click_function do
16+
div id: 'click_this' do
17+
plain "Click me"
18+
end
19+
end
20+
}
21+
end
22+
23+
def click_function
24+
return {
25+
emit: 'show_message'
26+
}
27+
end
28+
end
29+
30+
visit "/example"
31+
32+
static_output = page.html
33+
34+
expected_static_output = <<~HTML
35+
<div><div id="click_this">Click me</div></div>
36+
HTML
37+
38+
expect(stripped(static_output)).to include(stripped(expected_static_output))
39+
find('div', id: 'click_this').click
40+
41+
new_expected_static_output = <<~HTML
42+
<div id="the_message">This is a cool message</div>
43+
<div>Click me!</div>
44+
HTML
45+
end
46+
end

0 commit comments

Comments
 (0)