Skip to content

Commit 16eb472

Browse files
author
Nils Henning
committed
do not require attributes hash for custom attributes
1 parent 0943f19 commit 16eb472

File tree

11 files changed

+62
-42
lines changed

11 files changed

+62
-42
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Matestack::Ui::Core::Component
22
class Base < Trailblazer::Cell
33
include Matestack::Ui::Core::Cell
44
include Matestack::Ui::Core::HasViewContext
5+
include Matestack::Ui::Core::HtmlAttributes
56
include Matestack::Ui::Core::Properties
67

78
# probably eed to remove for other tests to be green again
@@ -76,6 +77,7 @@ def initialize(model = nil, options = {})
7677
# TODO: do we realy have to call this every time on initialize or should
7778
# it maybe be called more dynamically like its dynamic_tag_attributes
7879
# equivalent in Dynamic?
80+
extract_html_attributes(options)
7981
set_tag_attributes
8082
setup
8183
validate_options

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,19 @@ const componentDef = {
114114
},
115115
perform: function(){
116116
const self = this
117-
if (self.componentConfig["emit"] != undefined) {
118-
matestackEventHub.$emit(self.componentConfig["emit"]);
119-
}
120-
if (self.componentConfig["delay"] != undefined) {
121-
setTimeout(function () {
117+
if(self.$el.querySelector('form').checkValidity()){
118+
if (self.componentConfig["emit"] != undefined) {
119+
matestackEventHub.$emit(self.componentConfig["emit"]);
120+
}
121+
if (self.componentConfig["delay"] != undefined) {
122+
setTimeout(function () {
123+
self.sendRequest()
124+
}, parseInt(self.componentConfig["delay"]));
125+
} else {
122126
self.sendRequest()
123-
}, parseInt(self.componentConfig["delay"]));
127+
}
124128
} else {
125-
this.sendRequest()
129+
matestackEventHub.$emit('static_form_errors');
126130
}
127131
},
128132
sendRequest: function(){

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
- if label
2-
%label=label
1+
- if input_label
2+
%label=input_label
33

44
- if [:text, :number, :email, :date, :password].include?(type)
5-
%input{ @tag_attributes,
5+
%input{ html_attributes,
66
"v-model#{'.number' if type == :number}": input_key,
77
type: type,
88
"@change": "inputChanged(\"#{attr_key}\")",

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@ module Matestack::Ui::Core::Form::Input
22
class Input < Matestack::Ui::Core::Component::Static
33

44
requires :key, :type
5+
optional :placeholder, :multiple, :init, for: { as: :input_for }, label: { as: :input_label }
56

67
def custom_options_validation
78
raise "included form config is missing, please add ':include' to parent form component" if @included_config.nil?
89
end
910

10-
def label
11-
options[:label]
12-
end
13-
14-
def placeholder
15-
options[:placeholder]
16-
end
17-
1811
def input_key
1912
"data['#{key.to_s}']"
2013
end
@@ -24,30 +17,30 @@ def error_key
2417
end
2518

2619
def input_wrapper
27-
case options[:for]
20+
case input_for
2821
when nil
2922
return nil
3023
when Symbol, String
31-
return options[:for]
24+
return input_for
3225
end
33-
if options[:for].respond_to?(:model_name)
34-
return options[:for].model_name.singular
26+
if input_for.respond_to?(:model_name)
27+
return input_for.model_name.singular
3528
end
3629
end
3730

3831
def attr_key
3932
if input_wrapper.nil?
40-
return "#{key.to_s}#{'[]' if options[:multiple]}"
33+
return "#{key.to_s}#{'[]' if multiple}"
4134
else
42-
return "#{input_wrapper}.#{key.to_s}#{'[]' if options[:multiple]}"
35+
return "#{input_wrapper}.#{key.to_s}#{'[]' if multiple}"
4336
end
4437
end
4538

4639
def init_value
47-
return options[:init] unless options[:init].nil?
40+
return init unless init.nil?
4841

49-
unless options[:for].nil?
50-
value = parse_value(options[:for].send key)
42+
unless input_for.nil?
43+
value = parse_value(input_for.send key)
5144
else
5245
unless @included_config.nil? && @included_config[:for].nil?
5346
if @included_config[:for].respond_to?(key)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Matestack::Ui::Core::HtmlAttributes
2+
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+
)
14+
end
15+
16+
end

lib/matestack/ui/core/components.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def self.require_core_component(name)
2020

2121
require_app_path "helpers/matestack/ui/core/application_helper"
2222
require_app_path "lib/matestack/ui/core/has_view_context"
23+
require_app_path "lib/matestack/ui/core/html_attributes"
2324
require_app_path "lib/matestack/ui/core/properties"
2425

2526
require_app_path "concepts/matestack/ui/core/component/base"

spec/0.8/components/rails_view_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
describe 'Rails View Component', type: :feature, js: true do
55

66
after :all do
7-
Object.send(:remove_const, :ExampleController)
8-
load "#{Rails.root}/app/controllers/example_controller.rb"
7+
class ExampleController < ApplicationController
8+
def page
9+
render ExamplePage
10+
end
11+
end
912
end
1013

1114
it 'renders given view on to page' do

spec/dummy/app/matestack/demo/pages/my_fourth_page.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def response
1111
heading size: 3, text: "Dummy Model Form:"
1212

1313
form my_form_config, :include do
14-
form_input key: :title, type: :text, placeholder: "title"
14+
form_input key: :title, type: :text, placeholder: "title", required: true
15+
form_input key: :description, type: :text, placeholder: "Description", label: 'Description', attributes: { required: true }
1516
br
1617
form_submit do
1718
button text: "Submit me!"

spec/dummy/app/matestack/demo/pages/my_third_page.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ def response
2323
async show_on: "my_action_succeeded", hide_after: 2000 do
2424
plain "action succeeded!"
2525
end
26-
27-
@title = 'Test Title'
28-
rails_view view: '/header'
29-
rails_view partial: '/header'
3026
end
3127
}
3228
end

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

Lines changed: 11 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)