Skip to content

Commit 272b915

Browse files
committed
added form loading state and form classes, fixed error rendering bug on checkboxes
1 parent bcac875 commit 272b915

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ def response
1515
if checkbox_options
1616
checkbox_options.to_a.each do |item|
1717
input html_attributes.merge(
18-
attributes: vue_attributes,
18+
attributes: vue_attributes,
1919
type: :checkbox,
2020
id: "#{id_for_item(item_value(item))}",
2121
name: item_name(item),
22-
value: item_value(item),
22+
value: item_value(item)
2323
)
2424
label text: item_name(item), for: id_for_item(item_value(item))
2525
end
2626
# checked/unchecked checkbox
27-
else
28-
form_input type: :hidden, key: key, value: (false_value || 0)
29-
form_input type: :checkbox, key: key, value: checked_value, id: id_for_item(value)
27+
else
28+
form_input type: :hidden, key: key, value: (false_value || 0), errors: false
29+
form_input type: :checkbox, key: key, value: checked_value, id: id_for_item(value), errors: false
3030
label text: input_label, for: id_for_item(value)
3131
end
3232
render_errors

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const componentDef = {
1111
return {
1212
data: {},
1313
errors: {},
14+
loading: false
1415
};
1516
},
1617
methods: {
@@ -23,6 +24,15 @@ const componentDef = {
2324
updateFormValue: function (key, value) {
2425
this.data[key] = value;
2526
},
27+
hasErrors: function(){
28+
//https://stackoverflow.com/a/27709663/13886137
29+
for (var key in this.errors) {
30+
if (this.errors[key] !== null && this.errors[key] != ""){
31+
return true;
32+
}
33+
}
34+
return false;
35+
},
2636
resetErrors: function (key) {
2737
if (this.errors[key]) {
2838
this.errors[key] = null;
@@ -116,6 +126,7 @@ const componentDef = {
116126
const self = this
117127
var form = self.$el.tagName == 'FORM' ? self.$el : self.$el.querySelector('form');
118128
if(form.checkValidity()){
129+
self.loading = true;
119130
if (self.componentConfig["emit"] != undefined) {
120131
matestackEventHub.$emit(self.componentConfig["emit"]);
121132
}
@@ -171,6 +182,7 @@ const componentDef = {
171182
}
172183
axios(axios_config)
173184
.then(function (response) {
185+
self.loading = false;
174186
if (self.componentConfig["success"] != undefined && self.componentConfig["success"]["emit"] != undefined) {
175187
matestackEventHub.$emit(self.componentConfig["success"]["emit"], response.data);
176188
}
@@ -228,6 +240,7 @@ const componentDef = {
228240
}
229241
})
230242
.catch(function (error) {
243+
self.loading = false;
231244
if (error.response && error.response.data && error.response.data.errors) {
232245
self.errors = error.response.data.errors;
233246
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ def setup
2828
@component_config[:failure][:redirect][:path] = redirect_path options[:failure]
2929
end
3030
end
31-
@tag_attributes.merge!({"@submit.prevent": true})
31+
@tag_attributes.merge!({
32+
"@submit.prevent": true,
33+
"class": "matestack-form #{options[:class]}",
34+
"v-bind:class": "{ 'has-errors': hasErrors(), 'loading': loading }"
35+
})
3236
rescue => e
3337
raise "Form component could not be setted up. Reason: #{e}"
3438
end

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module Matestack::Ui::Core::Form::Submit
22
class Submit < Matestack::Ui::Core::Component::Static
33

44
def setup
5-
@tag_attributes.merge!({ "@click.prevent": "perform" })
5+
@tag_attributes.merge!({
6+
"@click.prevent": "perform",
7+
"v-bind:class": "{ 'loading': loading }"
8+
})
69
end
710

811
end

0 commit comments

Comments
 (0)