Skip to content

Commit 2ca0a53

Browse files
author
Nils Henning
committed
finalize textarea component, add form_textarea component, add tests
1 parent 6e25557 commit 2ca0a53

File tree

12 files changed

+346
-64
lines changed

12 files changed

+346
-64
lines changed

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

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
require_relative '../utils'
12
module Matestack::Ui::Core::Form::Input
23
class Input < Matestack::Ui::Core::Component::Static
4+
include Matestack::Ui::Core::Form::Utils
35

46
html_attributes :accept, :alt, :autocomplete, :autofocus, :checked, :dirname, :disabled, :form, :formaction,
57
:formenctype, :formmethod, :formnovalidate, :formtarget, :height, :list, :max, :maxlength, :min, :minlength,
@@ -42,50 +44,6 @@ def custom_options_validation
4244
raise "included form config is missing, please add ':include' to parent form component" if @included_config.nil?
4345
end
4446

45-
def input_key
46-
"data['#{key.to_s}']"
47-
end
48-
49-
def error_key
50-
"errors['#{key.to_s}']"
51-
end
52-
53-
def input_wrapper
54-
case input_for
55-
when nil
56-
return nil
57-
when Symbol, String
58-
return input_for
59-
end
60-
if input_for.respond_to?(:model_name)
61-
return input_for.model_name.singular
62-
end
63-
end
64-
65-
def attr_key
66-
if input_wrapper.nil?
67-
return "#{key.to_s}#{'[]' if multiple}"
68-
else
69-
return "#{input_wrapper}.#{key.to_s}#{'[]' if multiple}"
70-
end
71-
end
72-
73-
def init_value
74-
return init unless init.nil?
75-
76-
unless input_for.nil?
77-
value = parse_value(input_for.send key)
78-
else
79-
unless @included_config.nil? && @included_config[:for].nil?
80-
if @included_config[:for].respond_to?(key)
81-
value = parse_value(@included_config[:for].send key)
82-
else
83-
@included_config[:for][key] if @included_config[:for].is_a?(Hash)
84-
end
85-
end
86-
end
87-
end
88-
8947
private
9048

9149
def parse_value(value)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative '../utils'
2+
module Matestack::Ui::Core::Form::Textarea
3+
class Textarea < Matestack::Ui::Core::Textarea::Textarea
4+
include Matestack::Ui::Core::Form::Utils
5+
6+
requires :key
7+
optional :multiple, :init, for: { as: :input_for }, label: { as: :input_label }
8+
9+
def response
10+
label text: input_label if input_label
11+
textarea html_attributes.merge(attributes: vue_attributes)
12+
span class: 'errors', attributes: { 'v-if': error_key } do
13+
span class: 'error', text: '{{ error }}', attributes: { 'v-for': "error in #{error_key}" }
14+
end
15+
end
16+
17+
def vue_attributes
18+
(options[:attributes] || {}).merge({
19+
'v-model': input_key,
20+
'@change': "inputChanged('#{attr_key}')",
21+
"init-value": init_value,
22+
ref: "input.#{attr_key}",
23+
})
24+
end
25+
26+
end
27+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Matestack::Ui::Core::Form::Utils
2+
3+
def input_key
4+
"data['#{key.to_s}']"
5+
end
6+
7+
def error_key
8+
"errors['#{key.to_s}']"
9+
end
10+
11+
def input_wrapper
12+
case input_for
13+
when nil
14+
return nil
15+
when Symbol, String
16+
return input_for
17+
end
18+
if input_for.respond_to?(:model_name)
19+
return input_for.model_name.singular
20+
end
21+
end
22+
23+
def attr_key
24+
if input_wrapper.nil?
25+
return "#{key.to_s}#{'[]' if multiple}"
26+
else
27+
return "#{input_wrapper}.#{key.to_s}#{'[]' if multiple}"
28+
end
29+
end
30+
31+
def init_value
32+
return init unless init.nil?
33+
34+
unless input_for.nil?
35+
value = parse_value(input_for.send key)
36+
else
37+
unless @included_config.nil? && @included_config[:for].nil?
38+
if @included_config[:for].respond_to?(key)
39+
value = parse_value(@included_config[:for].send key)
40+
else
41+
@included_config[:for][key] if @included_config[:for].is_a?(Hash)
42+
end
43+
end
44+
end
45+
end
46+
47+
def parse_value(value)
48+
value
49+
end
50+
51+
end

lib/matestack/ui/core/components.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def self.require_core_component(name)
7070
require_core_component "fieldset"
7171
require_core_component "figure"
7272
require_core_component "footer"
73+
require_core_component "form"
74+
require_core_component "form/input"
75+
require_core_component "form/select"
76+
require_core_component "form/submit"
7377
require_core_component "header"
7478
require_core_component "heading"
7579
require_core_component "hr"
@@ -119,7 +123,8 @@ def self.require_core_component(name)
119123
require_core_component "tbody"
120124
require_core_component "td"
121125
require_core_component "template"
122-
require_core_component "textarea"
126+
require_core_component "textarea" # textarea needs to be required before form/textarea
127+
require_core_component "form/textarea"
123128
require_core_component "tfoot"
124129
require_core_component "th"
125130
require_core_component "thead"
@@ -133,10 +138,6 @@ def self.require_core_component(name)
133138
require_core_component "video"
134139
require_core_component "wbr"
135140
require_core_component "youtube"
136-
require_core_component "form"
137-
require_core_component "form/input"
138-
require_core_component "form/select"
139-
require_core_component "form/submit"
140141
end
141142

142143
Matestack::Ui::Core::Component::Registry.register_components(
@@ -185,6 +186,7 @@ def self.require_core_component(name)
185186
form_input: Matestack::Ui::Core::Form::Input::Input,
186187
form_select: Matestack::Ui::Core::Form::Select::Select,
187188
form_submit: Matestack::Ui::Core::Form::Submit::Submit,
189+
form_textarea: Matestack::Ui::Core::Form::Textarea::Textarea,
188190
header: Matestack::Ui::Core::Header::Header,
189191
heading: Matestack::Ui::Core::Heading::Heading,
190192
hr: Matestack::Ui::Core::Hr::Hr,

spec/0.8/components/dynamic/form/async_request_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class BaseExamplePage < BasePage
7575
class BaseExamplePage < BasePage
7676
def response
7777
super
78-
async show_on: "my_form_success" do
78+
async show_on: "my_form_success", id: 'async-form' do
7979
plain "{{event.data.message}}"
8080
end
8181
end
@@ -95,7 +95,7 @@ def form_config
9595
class BaseExamplePage < BasePage
9696
def response
9797
super
98-
async show_on: "my_form_failure" do
98+
async show_on: "my_form_failure", id: 'async-form' do
9999
plain "{{event.data.message}}"
100100
plain "{{event.data.errors}}"
101101
end

spec/0.8/components/dynamic/form/async_transition_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def response
3737
main do
3838
page_content
3939
end
40-
async show_on: "my_form_success", hide_after: 300 do
40+
async show_on: "my_form_success", hide_after: 300, id: 'async-form-success' do
4141
plain "{{event.data.message}}"
4242
end
43-
async show_on: "my_form_failure", hide_after: 300 do
43+
async show_on: "my_form_failure", hide_after: 300, id: 'async-form-failure' do
4444
plain "{{event.data.message}}"
4545
plain "{{event.data.errors}}"
4646
end

spec/0.8/components/dynamic/form/delay_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def response
3737
end
3838
end
3939
div id: "timestamp" do
40-
async show_on: "form_submitted_successfully" do
40+
async show_on: "form_submitted_successfully", id: 'async-form' do
4141
paragraph id: 'received_timestamp', text: "{{event.data.received_at}}"
4242
end
4343
end

spec/0.8/components/dynamic/form/emit_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def response
3434
button text: 'Submit me!'
3535
end
3636
end
37-
async show_on: "form_submitted" do
37+
async show_on: "form_submitted", id: 'async-form' do
3838
plain "form submitted!"
3939
end
4040
end

spec/0.8/components/dynamic/form/form_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def response
4242
form_input id: 'description', key: :description, type: :text
4343
form_submit { button text: "Save" }
4444
end
45-
async show_on: "form_has_errors", hide_after: 5000 do
45+
async show_on: "form_has_errors", hide_after: 5000, id: 'async-form-errors' do
4646
plain "Form has errors"
4747
end
48-
async show_on: "update_successful", hide_after: 5000 do
48+
async show_on: "update_successful", hide_after: 5000, id: 'async-form-successful' do
4949
plain "Update successful"
5050
end
5151
end
@@ -123,10 +123,10 @@ def response
123123
form_input id: 'query', key: :query, type: :text
124124
form_submit { button text: "Search" }
125125
end
126-
async show_on: "form_has_errors", hide_after: 5000 do
126+
async show_on: "form_has_errors", hide_after: 5000, id: 'async-form-errors' do
127127
plain "Form has errors"
128128
end
129-
async show_on: "submission_successful", hide_after: 5000 do
129+
async show_on: "submission_successful", hide_after: 5000, id: 'async-form-successful' do
130130
plain "Submission successful"
131131
end
132132
end
@@ -182,10 +182,10 @@ def response
182182
form_input id: 'query', key: :query, type: :text
183183
form_submit { button text: "Search" }
184184
end
185-
async show_on: "form_has_errors", hide_after: 5000 do
185+
async show_on: "form_has_errors", hide_after: 5000, id: 'async-form-errors' do
186186
plain "Form has errors"
187187
end
188-
async show_on: "submission_successful", hide_after: 5000 do
188+
async show_on: "submission_successful", hide_after: 5000, id: 'async-form-successful' do
189189
plain "Submission successful"
190190
end
191191
end

spec/0.8/components/dynamic/form/input/file_upload_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def response
7575
button text: 'Submit me!'
7676
end
7777
end
78-
async show_on: "uploaded_successfully" do
78+
async show_on: "uploaded_successfully", id: 'async-form' do
7979
plain "{{event.data.title}}"
8080
plain "{{event.data.file_1.instance}}"
8181
plain "{{event.data.file_1.name}}"
@@ -251,7 +251,7 @@ def response
251251
button text: 'Submit me!'
252252
end
253253
end
254-
async show_on: "uploaded_successfully", hide_on: "form_submitted" do
254+
async show_on: "uploaded_successfully", hide_on: "form_submitted", id: 'async-form' do
255255
plain "{{event.data.file_1.instance}}"
256256
plain "{{event.data.file_1.name}}"
257257
plain "{{event.data.files}}"

0 commit comments

Comments
 (0)