Skip to content

Commit b7a2077

Browse files
committed
Adds docs and tests for onclick component
1 parent 43e7324 commit b7a2077

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

docs/components/onclick.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
# matestack core component: Action
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 on click. This is a nice component for use cases where you would like to wrap multiple components into one click function. The function called by the onclick component requires two fields. These two fields are `emit` and `data`. These two fields are then emitted as a event in javascript when the component is clicked
6+
7+
```ruby
8+
class Pages::MyPage::Home < Matestack::Ui::Page
9+
def response
10+
components{
11+
onclick run_this_function do
12+
plain "Hello world"
13+
end
14+
}
15+
end
16+
17+
def run_this_function
18+
return {
19+
emit: "abc",
20+
data: {
21+
id: 1,
22+
name: "John",
23+
detail: "Clicked the button"
24+
}
25+
}
26+
end
27+
end
28+
```

spec/usage/components/onclick_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require_relative "../../support/utils"
2+
include Utils
3+
4+
describe "Nav 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+
data: {
27+
message: "This is a cool message"
28+
}
29+
}
30+
end
31+
end
32+
33+
visit "/example"
34+
35+
static_output = page.html
36+
37+
expected_static_output = <<~HTML
38+
<div><div id="click_this">Click me</div></div>
39+
HTML
40+
41+
expect(stripped(static_output)).to include(stripped(expected_static_output))
42+
find('div', id: 'click_this').click
43+
44+
new_expected_static_output = <<~HTML
45+
<div id="the_message">This is a cool message</div>
46+
<div>Click me!</div>
47+
HTML
48+
end
49+
50+
end

0 commit comments

Comments
 (0)