Skip to content

Commit dbb41ef

Browse files
Merge pull request #243 from basemate/document_link_param_option
Document link param option, fixes #73
2 parents 002ca67 + 0dd8e02 commit dbb41ef

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

app/concepts/matestack/ui/core/link/link.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def setup
99
"id": component_id,
1010
"method": options[:method],
1111
"target": options[:target] ||= nil,
12-
"href": link_path
12+
"href": link_path,
13+
"title": options[:title]
1314
})
1415
end
1516

docs/components/link.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ The HTTP request method the link should implement.
2828
#### # target (optional, default is nil)
2929
Specify where to open the linked document.
3030

31+
#### # title (optional)
32+
Specify a title for the link (shown on mouseover).
33+
3134
## Example 1
3235
This example renders a simple link within a `<div`-tag
3336

@@ -50,7 +53,7 @@ This example renders a link without a specific link-text, so it wraps the rest o
5053

5154
```ruby
5255
div id: "foo", class: "bar" do
53-
link path: "https://matestack.org" do
56+
link path: "https://matestack.org", title: "The matestack website" do
5457
plain "Here"
5558
end
5659
end
@@ -59,7 +62,7 @@ end
5962
returns
6063

6164
```html
62-
<div id="foo" class="bar">
65+
<div id="foo" class="bar" title="The matestack website">
6366
<a href="https://matestack.org">Here</a>
6467
</div>
6568
```
@@ -107,3 +110,37 @@ returns
107110
</a>
108111
</div>
109112
```
113+
114+
## Example 5 - path from symbol
115+
This example renders a link with a get request to any within your Rails application. In case you want to switch between pages within one specific matestack app, using the `transition` component probably is a better idea though!
116+
117+
```ruby
118+
div id: "foo", class: "bar" do
119+
link path: :inline_edit_path, text: 'Click to edit'
120+
end
121+
```
122+
123+
returns
124+
125+
```html
126+
<div id="foo" class="bar">
127+
<a href="/inline_edit">Click to edit</a>
128+
</div>
129+
```
130+
131+
## Example 6 - path from symbol with params
132+
You can also dynamically create `paths` from symbols and params, as displayed below:
133+
134+
```ruby
135+
div id: "foo", class: "bar" do
136+
link path: :single_endpoint_path, params: {number: 1}, text: 'Call API endpoint 1'
137+
end
138+
```
139+
140+
returns
141+
142+
```html
143+
<div id="foo" class="bar">
144+
<a href="/api/single_endpoint/1">Call API endpoint 1</a>
145+
</div>
146+
```

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
scope :api do
4040
get 'data', to: 'api#data'
41+
get 'data/:number', to: 'api#single_endpoint', as: "single_endpoint"
4142
end
4243

4344
end

spec/usage/components/link_spec.rb

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ExamplePage < Matestack::Ui::Page
3737
def response
3838
components {
3939
div id: "foo", class: "bar" do
40-
link path: "https://matestack.org" do
40+
link path: "https://matestack.org", title: "The matestack website" do
4141
plain 'here'
4242
end
4343
end
@@ -53,7 +53,7 @@ def response
5353

5454
expected_static_output = <<~HTML
5555
<div id="foo" class="bar">
56-
<a href="https://matestack.org">here</a>
56+
<a href="https://matestack.org" title="The matestack website">here</a>
5757
</div>
5858
HTML
5959

@@ -125,4 +125,58 @@ def response
125125
expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
126126
end
127127

128+
it 'Example 5 - Rails Routing using symbols' do
129+
130+
class ExamplePage < Matestack::Ui::Page
131+
132+
def response
133+
components {
134+
div id: "foo", class: "bar" do
135+
link text: 'Click', path: :inline_edit_path
136+
end
137+
}
138+
end
139+
140+
end
141+
142+
visit "/example"
143+
144+
static_output = page.html
145+
146+
expected_static_output = <<~HTML
147+
<div id="foo" class="bar">
148+
<a href="/my_app/inline_edit">Click</a>
149+
</div>
150+
HTML
151+
152+
expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
153+
end
154+
155+
it 'Example 6 - Rails Routing using symbols with params' do
156+
157+
class ExamplePage < Matestack::Ui::Page
158+
159+
def response
160+
components {
161+
div id: "foo", class: "bar" do
162+
link path: :single_endpoint_path, params: {number: 1}, text: 'Call API endpoint 1'
163+
end
164+
165+
}
166+
end
167+
168+
end
169+
visit "/example"
170+
171+
static_output = page.html
172+
173+
expected_static_output = <<~HTML
174+
<div id="foo" class="bar">
175+
<a href="/api/data/1">Call API endpoint 1</a>
176+
</div>
177+
HTML
178+
179+
expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
180+
end
181+
128182
end

0 commit comments

Comments
 (0)