Skip to content

Commit eb93f81

Browse files
authored
Merge pull request #397 from matestack/20200310_82_henning_add-range-input
Add datalist component, update input and form_input to handle type range
2 parents bd93f6c + a9d5725 commit eb93f81

File tree

12 files changed

+276
-55
lines changed

12 files changed

+276
-55
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%datalist{@tag_attributes}
2+
- if block_given?
3+
= yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Matestack::Ui::Core::Datalist
2+
class Datalist < Matestack::Ui::Core::Component::Static
3+
4+
end
5+
end
Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
1-
- if options[:label]
2-
%label=options[:label]
1+
- if label
2+
%label=label
33

4-
- if [:text, :number, :email, :date, :password].include?(options[:type])
5-
%input{@tag_attributes,
6-
"v-model#{'.number' if options[:type] == :number}": input_key,
7-
type: options[:type],
4+
- if [:text, :number, :email, :date, :password].include?(type)
5+
%input{ @tag_attributes,
6+
"v-model#{'.number' if type == :number}": input_key,
7+
type: type,
88
"@change": "inputChanged(\"#{attr_key}\")",
99
ref: "input.#{attr_key}",
10-
placeholder: options[:placeholder],
11-
"init-value": init_value}
12-
%span{class: "errors", "v-if": error_key }
13-
%span{class: "error", "v-for": "error in #{error_key}"}
14-
{{ error }}
10+
placeholder: placeholder,
11+
"init-value": init_value }
1512

16-
- if options[:type] == :textarea
17-
%textarea{@tag_attributes,
13+
- if type == :textarea
14+
%textarea{ @tag_attributes,
1815
"v-model": input_key,
1916
"@change": "inputChanged(\"#{attr_key}\")",
2017
ref: "input.#{attr_key}",
21-
placeholder: options[:placeholder],
18+
placeholder: placeholder,
2219
"init-value": init_value,
2320
rows: options[:rows],
24-
cols: options[:cols]}
25-
%span{class: "errors", "v-if": error_key }
26-
%span{class: "error", "v-for": "error in #{error_key}"}
27-
{{ error }}
21+
cols: options[:cols] }
22+
23+
- if type == :range
24+
%input{ @tag_attributes,
25+
"v-model": input_key,
26+
type: :range,
27+
"@change": "inputChanged(\"#{attr_key}\")",
28+
ref: "input.#{attr_key}",
29+
placeholder: placeholder,
30+
"init-value": init_value,
31+
min: options[:min],
32+
max: options[:max],
33+
step: options[:step],
34+
list: options[:list] }
35+
36+
%span{ class: "errors", "v-if": error_key }
37+
%span{ class: "error", "v-for": "error in #{error_key}" }
38+
{{ error }}

app/concepts/matestack/ui/core/form/input/input.rb

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,35 @@ def custom_options_validation
77
raise "included form config is missing, please add ':include' to parent form component" if @included_config.nil?
88
end
99

10+
def key
11+
options[:key]
12+
end
13+
14+
def label
15+
options[:label]
16+
end
17+
18+
def type
19+
options[:type]
20+
end
21+
22+
def placeholder
23+
options[:placeholder]
24+
end
25+
1026
def input_key
11-
'data["' + options[:key].to_s + '"]'
27+
"data['#{key.to_s}']"
1228
end
1329

1430
def error_key
15-
'errors["' + options[:key].to_s + '"]'
31+
"errors['#{key.to_s}']"
1632
end
1733

1834
def input_wrapper
1935
case options[:for]
2036
when nil
2137
return nil
22-
when Symbol
23-
return options[:for]
24-
when String
38+
when Symbol, String
2539
return options[:for]
2640
end
2741
if options[:for].respond_to?(:model_name)
@@ -31,44 +45,37 @@ def input_wrapper
3145

3246
def attr_key
3347
if input_wrapper.nil?
34-
return options[:key].to_s
48+
return key.to_s
3549
else
36-
return "#{input_wrapper}.#{options[:key].to_s}"
50+
return "#{input_wrapper}.#{key.to_s}"
3751
end
3852
end
3953

4054
def init_value
41-
unless options[:init].nil?
42-
return options[:init]
43-
end
55+
return options[:init] unless options[:init].nil?
4456

4557
unless options[:for].nil?
46-
value = options[:for].send(options[:key])
47-
if [true, false].include? value
48-
value ? 1 : 0
49-
else
50-
return value
51-
end
58+
value = parse_value(options[:for].send key)
5259
else
5360
unless @included_config.nil? && @included_config[:for].nil?
54-
if @included_config[:for].respond_to?(options[:key])
55-
value = @included_config[:for].send(options[:key])
56-
if [true, false].include? value
57-
value ? 1 : 0
58-
else
59-
return value
60-
end
61+
if @included_config[:for].respond_to?(key)
62+
value = parse_value(@included_config[:for].send key)
6163
else
62-
if @included_config[:for].is_a?(Symbol) || @included_config[:for].is_a?(String)
63-
return nil
64-
end
65-
if @included_config[:for].is_a?(Hash)
66-
return @included_config[:for][options[:key]]
67-
end
64+
@included_config[:for][key] if @included_config[:for].is_a?(Hash)
6865
end
6966
end
7067
end
7168
end
7269

70+
private
71+
72+
def parse_value(value)
73+
if [true, false].include? value
74+
value ? 1 : 0
75+
else
76+
return value
77+
end
78+
end
79+
7380
end
7481
end
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
%input{@tag_attributes}
2-
- if block_given?
3-
= yield
1+
%input{ @tag_attributes,
2+
type: type }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module Matestack::Ui::Core::Input
22
class Input < Matestack::Ui::Core::Component::Static
33

4+
def type
5+
options[:type]
6+
end
7+
48
end
59
end

docs/components/datalist.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# matestack core component: Datalist
2+
3+
Show [specs](/spec/usage/components/datalist_spec.rb)
4+
5+
The HTML `<datalist>` tag implemented in Ruby.
6+
7+
## Parameters
8+
9+
This component can take 2 optional configuration params and yield content.
10+
11+
#### # id (optional)
12+
Expects a string with all ids the datalist should have.
13+
14+
#### # class (optional)
15+
Expects a string with all classes the datalist should have.
16+
17+
18+
## Example 1
19+
Adding an optional id
20+
21+
```ruby
22+
datalist id: 'foo', class: 'bar' do
23+
plain 'Example Text'
24+
end
25+
```
26+
27+
returns
28+
29+
```html
30+
<datalist id="foo" class="bar">Example Text</datalist>
31+
```

docs/components/form.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ end
578578

579579
### Example 4: Multiple input fields of different types
580580

581-
Of course, our input core component accepts not only 'text', but very different input types: In this example, we will introduce 'password', 'number', 'email', 'textarea' types!
581+
Of course, our input core component accepts not only 'text', but very different input types: In this example, we will introduce 'password', 'number', 'email', 'range', 'textarea' types!
582582

583583
On our example page, we define the input fields, together with a `type: X` configuration:
584584

@@ -592,6 +592,7 @@ class ExamplePage < Matestack::Ui::Page
592592
form_input id: 'email-input', key: :email_input, type: :email
593593
form_input id: 'password-input', key: :password_input, type: :password
594594
form_input id: 'number-input', key: :number_input, type: :number
595+
form_input id: 'range-input', key: :range_input, type: :range
595596
form_input id: 'textarea-input', key: :textarea_input, type: :textarea
596597
form_submit do
597598
button text: 'Submit me!'
@@ -1326,3 +1327,25 @@ end
13261327
```
13271328

13281329
Again, we can visit our example page on `localhost:3000/example` and are welcome by the preselected status of our `TestModel` instance. Changing the value of the status by toggling the radio button and filling in a description plus submitting it works just as before!
1330+
1331+
#### Example 10.4: The Range Input
1332+
1333+
Whilst working similiar to the 'text' input, the range input accepts a few more parameters. It accepts also 'min', 'max', 'step', 'list' as optional parameters.
1334+
1335+
##### Example 10.4.1: Range input with max, min, step set
1336+
1337+
```ruby
1338+
form_input id: 'range-input', type: :range, min: 0, max: 100, step: 1
1339+
```
1340+
1341+
#### Example 10.4.2: Range input with corresponding datalist
1342+
1343+
To use a datalist for the range input specify the 'list' parameter with the id of the provided datalist
1344+
1345+
```ruby
1346+
form_input id: 'range-input', type: :range, list: 'datalist-id'
1347+
datalist id: 'datalist-id' do
1348+
option value: 10
1349+
option value: 20
1350+
end
1351+
```

docs/components/input.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# matestack core component: Input
2+
3+
Show [specs](/spec/usage/components/input_spec.rb)
4+
5+
The HTML input tag implemented in ruby.
6+
7+
## Parameters
8+
9+
This component can take 3 optional configuration params and optional content.
10+
11+
#### # id (optional)
12+
Expects a string with all ids the main should have.
13+
14+
#### # class (optional)
15+
Expects a string with all classes the main should have.
16+
17+
#### # type (optional)
18+
Expects a symbol with that specifies the input type.
19+
20+
## Example
21+
22+
```ruby
23+
input type: :text, id: "foo", class: "bar"
24+
```
25+
26+
returns
27+
28+
```html
29+
<input type="text" id="foo" class="bar" />
30+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require_relative "../../support/utils"
2+
include Utils
3+
4+
describe 'Datalist Component', type: :feature, js: true do
5+
6+
it 'Example 1 - yield' do
7+
8+
class ExamplePage < Matestack::Ui::Page
9+
10+
def response
11+
components {
12+
datalist id: 'foo', class: 'bar' do
13+
plain 'Example Text'
14+
end
15+
}
16+
end
17+
18+
end
19+
20+
visit '/example'
21+
22+
static_output = page.html
23+
24+
expected_static_output = <<~HTML
25+
<datalist id="foo" class="bar">Example Text</datalist>
26+
HTML
27+
28+
expect(stripped(static_output)).to include(stripped(expected_static_output))
29+
end
30+
31+
end

0 commit comments

Comments
 (0)