Skip to content

Commit a7d9014

Browse files
committed
make form reset on success configurable
#314 (comment)
1 parent cb5a9c7 commit a7d9014

File tree

4 files changed

+160
-4
lines changed

4 files changed

+160
-4
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ const componentDef = {
8888
}
8989
self.data = data;
9090
},
91+
shouldResetFormOnSuccessfulSubmit() {
92+
const self = this
93+
if (self.componentConfig['success'] != undefined && self.componentConfig['success']['reset'] != undefined) {
94+
return self.componentConfig['success']['reset']
95+
} else {
96+
return self.shouldResetFormOnSuccessfulSubmitByDefault()
97+
}
98+
},
99+
shouldResetFormOnSuccessfulSubmitByDefault() {
100+
const self = this
101+
if (self.componentConfig["method"] == "put") {
102+
return false
103+
} else {
104+
return true
105+
}
106+
},
91107
perform: function(){
92108
const self = this
93109
let payload = {}
@@ -126,7 +142,7 @@ const componentDef = {
126142
self.$store.dispatch('navigateTo', {url: path, backwards: false})
127143
return;
128144
}
129-
if (self.componentConfig["method"] == "post")
145+
if (self.shouldResetFormOnSuccessfulSubmit())
130146
{
131147
self.setProps(self.data, null);
132148
self.initValues();

spec/usage/components/form_spec.rb

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def page4
486486

487487
end
488488

489-
describe "Example 6 - Async submit update request with success, which does not rest the input fields" do
489+
describe "Example 6 - Async submit update request with success, which does not reset the input fields" do
490490
# https://github.com/basemate/matestack-ui-core/issues/304
491491

492492
# This example uses the `TestModel` with attributes `title` and `description`
@@ -575,6 +575,130 @@ def test_model_params
575575
end
576576
end
577577

578+
describe "Example 6.1 - Async submit with success configured to reset the input fields" do
579+
# https://github.com/matestack/matestack-ui-core/pull/314#discussion_r362826471
580+
581+
before do
582+
class Pages::SearchPage < Matestack::Ui::Page
583+
def response
584+
components {
585+
form form_config, :include do
586+
form_input id: 'query', key: :query, type: :text
587+
form_submit { button text: "Search" }
588+
end
589+
async show_on: "form_has_errors", hide_after: 5000 do
590+
plain "Form has errors"
591+
end
592+
async show_on: "submission_successful", hide_after: 5000 do
593+
plain "Submission successful"
594+
end
595+
}
596+
end
597+
598+
private
599+
600+
def form_config
601+
{
602+
for: :search,
603+
method: :get,
604+
path: "#",
605+
success: { emit: "submission_successful", reset: true },
606+
failure: { emit: "form_has_errors" }
607+
}
608+
end
609+
end
610+
611+
class SearchesController < ApplicationController
612+
include Matestack::Ui::Core::ApplicationHelper
613+
614+
def index
615+
responder_for Pages::SearchPage
616+
end
617+
end
618+
619+
Rails.application.routes.draw do
620+
get 'search', to: 'searches#index'
621+
end
622+
end
623+
624+
after do
625+
Rails.application.reload_routes!
626+
end
627+
628+
specify do
629+
visit "/search"
630+
expect(find_field(:query).value).to eq ""
631+
632+
fill_in :query, with: "Bar"
633+
click_on "Search"
634+
635+
expect(page).to have_text "Submission successful"
636+
expect(find_field(:query).value).to eq ""
637+
end
638+
end
639+
640+
describe "Example 6.2 - Async submit with success configured not to reset the input fields" do
641+
# https://github.com/matestack/matestack-ui-core/pull/314#discussion_r362826471
642+
643+
before do
644+
class Pages::SearchPage < Matestack::Ui::Page
645+
def response
646+
components {
647+
form form_config, :include do
648+
form_input id: 'query', key: :query, type: :text
649+
form_submit { button text: "Search" }
650+
end
651+
async show_on: "form_has_errors", hide_after: 5000 do
652+
plain "Form has errors"
653+
end
654+
async show_on: "submission_successful", hide_after: 5000 do
655+
plain "Submission successful"
656+
end
657+
}
658+
end
659+
660+
private
661+
662+
def form_config
663+
{
664+
for: :search,
665+
method: :get,
666+
path: "#",
667+
success: { emit: "submission_successful", reset: false },
668+
failure: { emit: "form_has_errors" }
669+
}
670+
end
671+
end
672+
673+
class SearchesController < ApplicationController
674+
include Matestack::Ui::Core::ApplicationHelper
675+
676+
def index
677+
responder_for Pages::SearchPage
678+
end
679+
end
680+
681+
Rails.application.routes.draw do
682+
get 'search', to: 'searches#index'
683+
end
684+
end
685+
686+
after do
687+
Rails.application.reload_routes!
688+
end
689+
690+
specify do
691+
visit "/search"
692+
expect(find_field(:query).value).to eq ""
693+
694+
fill_in :query, with: "Bar"
695+
click_on "Search"
696+
697+
expect(page).to have_text "Submission successful"
698+
expect(find_field(:query).value).to eq "Bar"
699+
end
700+
end
701+
578702
describe "Form Input Component" do
579703

580704
it "Example 1 - Supports 'text', 'password', 'number', 'email', 'textarea' type" do

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

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

vendor/assets/javascripts/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)