Skip to content

Commit 0b5bac8

Browse files
author
Nils Henning
committed
update html_attributes method, refactor input component
1 parent 16eb472 commit 0b5bac8

File tree

10 files changed

+92
-24
lines changed

10 files changed

+92
-24
lines changed

.byebug_history

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
continue
22
save_screenshot
3+
page.html
4+
continue
5+
save_screenshot
36
continue
47
context
58
continue
@@ -251,6 +254,3 @@ prop.is_a? Hash
251254
self.class.requires_properties
252255
continue
253256
self.class.requires_properties
254-
continue
255-
Matestack::Ui::Core::Form::Form.instance_methods.include? :method
256-
Matestack::Ui::Core::Form::Form.instance_methods.contains? :method

app/concepts/matestack/ui/core/component/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class Base < Trailblazer::Cell
55
include Matestack::Ui::Core::HtmlAttributes
66
include Matestack::Ui::Core::Properties
77

8+
# define html global attributes
9+
html_attributes *HTML_GLOBAL_ATTRIBUTES, *HTML_EVENT_ATTRIBUTES
10+
811
# probably eed to remove for other tests to be green again
912
include Matestack::Ui::Core::DSL
1013

@@ -77,7 +80,6 @@ def initialize(model = nil, options = {})
7780
# TODO: do we realy have to call this every time on initialize or should
7881
# it maybe be called more dynamically like its dynamic_tag_attributes
7982
# equivalent in Dynamic?
80-
extract_html_attributes(options)
8183
set_tag_attributes
8284
setup
8385
validate_options

app/concepts/matestack/ui/core/form/form.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ const componentDef = {
114114
},
115115
perform: function(){
116116
const self = this
117-
if(self.$el.querySelector('form').checkValidity()){
117+
var form = self.$el.tagName == 'FORM' ? self.$el : self.$el.querySelector('form');
118+
if(form.checkValidity()){
118119
if (self.componentConfig["emit"] != undefined) {
119120
matestackEventHub.$emit(self.componentConfig["emit"]);
120121
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
11
module Matestack::Ui::Core::Form::Input
22
class Input < Matestack::Ui::Core::Component::Static
33

4+
html_attributes :accept, :alt, :autocomplete, :autofocus, :checked, :dirname, :disabled, :form, :formaction,
5+
:formenctype, :formmethod, :formnovalidate, :formtarget, :height, :list, :max, :maxlength, :min, :minlength,
6+
:multiple, :name, :pattern, :placeholder, :readonly, :required, :size, :src, :step, :type, :value, :width
7+
48
requires :key, :type
5-
optional :placeholder, :multiple, :init, for: { as: :input_for }, label: { as: :input_label }
9+
optional :multiple, :init, for: { as: :input_for }, label: { as: :input_label }
10+
11+
def response
12+
label text: input_label if input_label
13+
input html_attributes.merge(attributes: vue_attributes)
14+
span class: 'errors', attributes: { 'v-if': error_key } do
15+
span class: 'error', text: '{{ error }}', attributes: { 'v-for': "error in #{error_key}" }
16+
end
17+
end
18+
19+
def vue_attributes
20+
(options[:attributes] || {}).merge({
21+
"@change": change_event,
22+
ref: "input.#{attr_key}",
23+
'init-value': init_value
24+
}).merge(
25+
type != :file ? { "#{v_model_type}": input_key } : {}
26+
) # file inputs are readonly, no v-model possible
27+
end
28+
29+
def v_model_type
30+
if type == :number || init_value.is_a?(Integer)
31+
'v-model.number'
32+
else
33+
'v-model'
34+
end
35+
end
36+
37+
def change_event
38+
"inputChanged('#{attr_key}'); #{ "filesAdded('#{attr_key}');" if type == :file }".strip
39+
end
640

741
def custom_options_validation
842
raise "included form config is missing, please add ':include' to parent form component" if @included_config.nil?
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
%input{ @tag_attributes,
1+
%input{ html_attributes,
22
type: type }

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

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
3+
4+
html_attributes :accept, :alt, :autocomplete, :autofocus, :checked, :dirname, :disabled, :form, :formaction,
5+
:formenctype, :formmethod, :formnovalidate, :formtarget, :height, :list, :max, :maxlength, :min, :minlength,
6+
:multiple, :name, :pattern, :placeholder, :readonly, :required, :size, :src, :step, :type, :value, :width
37

48
def type
59
options[:type]
Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
module Matestack::Ui::Core::HtmlAttributes
22

3-
# contains all passed options
4-
attr_accessor :html_attributes
5-
6-
def extract_html_attributes(options)
7-
self.html_attributes = options.except(
8-
:context,
9-
:included_config,
10-
:matestack_context,
11-
:children,
12-
:url_params
13-
)
3+
HTML_GLOBAL_ATTRIBUTES = [
4+
:accesskey, :class, :contenteditable, :data, :dir, :draggable,
5+
:hidden, :id, :lang, :spellcheck, :style, :tabindex, :title, :translate
6+
]
7+
8+
HTML_EVENT_ATTRIBUTES = [
9+
:onafterprint, :onbeforeprint, :onbeforeunload, :onerror, :onhashchange, :onload, :onmessage,
10+
:onoffline, :ononline, :onpagehide, :onpageshow, :onpopstate, :onresize, :onstorage, :onunload
11+
]
12+
13+
# prepend the initializer and add class methods
14+
def self.included(base)
15+
base.class_eval do
16+
extend ClassMethods
17+
end
18+
end
19+
20+
module ClassMethods
21+
22+
def inherited(subclass)
23+
subclass.html_attributes *self.allowed_html_attributes
24+
end
25+
26+
def html_attributes(*attributes)
27+
attributes.each do |attribute|
28+
allowed_html_attributes.push(attribute)
29+
end
30+
end
31+
32+
def allowed_html_attributes
33+
@allowed_html_attributes.flatten!&.uniq! if defined? @allowed_html_attributes
34+
@allowed_html_attributes ||= []
35+
end
36+
37+
end
38+
39+
def html_attributes
40+
options.slice(*self.class.allowed_html_attributes).merge((options[:attributes] || {}))
1441
end
1542

1643
end

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def form_config
6464
end
6565

6666
visit '/example'
67-
sleep 1
6867
fill_in "my-test-input", with: "bar"
6968
expect_any_instance_of(FormTestController).to receive(:expect_params)
7069
.with(hash_including(my_object: { bar: nil, foo: "bar" }))
@@ -85,7 +84,7 @@ def response
8584
form_input id: "email-input", key: :email_input, type: :email
8685
form_input id: "password-input", key: :password_input, type: :password
8786
form_input id: "number-input", key: :number_input, type: :number
88-
form_input id: "textarea-input", key: :textarea_input, type: :textarea
87+
# form_input id: "textarea-input", key: :textarea_input, type: :textarea # TODO textarea will be moved
8988
form_input id: "range-input", key: :range_input, type: :range
9089
form_submit do
9190
button text: "Submit me!"
@@ -110,15 +109,15 @@ def form_config
110109
fill_in "email-input", with: "[email protected]"
111110
fill_in "password-input", with: "secret"
112111
fill_in "number-input", with: 123
113-
fill_in "textarea-input", with: "Hello \n World!"
112+
# fill_in "textarea-input", with: "Hello \n World!"
114113
fill_in "range-input", with: 10
115114
expect_any_instance_of(FormTestController).to receive(:expect_params).with(hash_including(
116115
my_object: {
117116
text_input: "text",
118117
email_input: "[email protected]",
119118
password_input: "secret",
120119
number_input: 123,
121-
textarea_input: "Hello \n World!",
120+
# textarea_input: "Hello \n World!",
122121
range_input: "10"
123122
}
124123
))

vendor/assets/javascripts/dist/matestack-ui-core.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/assets/javascripts/dist/matestack-ui-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)