Skip to content

Commit 11d33c3

Browse files
committed
fixed form reset and added specs for form file upload reset
1 parent f9670d3 commit 11d33c3

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,18 @@ const componentDef = {
4242
},
4343
setProps: function (flat, newVal) {
4444
for (var i in flat) {
45-
if (flat[i] instanceof File){
45+
if (flat[i] === null){
46+
flat[i] = newVal;
47+
} else if (flat[i] instanceof File){
4648
flat[i] = newVal;
4749
this.$refs["input."+i].value = newVal
4850
} else if (flat[i] instanceof Array) {
49-
flat[i] = newVal
50-
this.$refs["input."+i].value = newVal
51+
if(flat[i][0] instanceof File){
52+
flat[i] = newVal
53+
this.$refs["input."+i].value = newVal
54+
}
5155
} else if (typeof flat[i] === "object" && !(flat[i] instanceof Array)) {
5256
setProps(flat[i], newVal);
53-
return;
5457
} else {
5558
flat[i] = newVal;
5659
}
@@ -200,6 +203,7 @@ const componentDef = {
200203
return;
201204
}
202205
if (self.shouldResetFormOnSuccessfulSubmit()) {
206+
console.log("reset")
203207
self.setProps(self.data, null);
204208
self.initValues();
205209
}

spec/usage/components/form/input/file_upload_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,72 @@ def form_config
292292

293293
end
294294

295+
it "gets properly resetted when form is successfully submitted" do
296+
297+
class ExamplePage < Matestack::Ui::Page
298+
299+
def response
300+
components {
301+
form form_config, :include do
302+
form_input key: :title, type: :text, placeholder: "title", id: "title-input"
303+
br
304+
form_input key: :file_1, type: :file, id: "file-1-input"
305+
br
306+
form_input key: :files, type: :file, multiple: true, id: "files-input"
307+
br
308+
form_submit do
309+
button text: 'Submit me!'
310+
end
311+
end
312+
async show_on: "uploaded_successfully", hide_on: "form_submitted" do
313+
plain "{{event.data.file_1.instance}}"
314+
plain "{{event.data.file_1.name}}"
315+
plain "{{event.data.files}}"
316+
end
317+
}
318+
end
319+
320+
def form_config
321+
return {
322+
for: :my_form,
323+
method: :post,
324+
multipart: true,
325+
path: :form_input_file_upload_success_form_test_path,
326+
emit: "form_submitted",
327+
success: {
328+
emit: "uploaded_successfully"
329+
}
330+
}
331+
end
332+
333+
end
334+
335+
visit '/example'
336+
337+
fill_in "title-input", with: "bar"
338+
attach_file('file-1-input', "#{File.dirname(__FILE__)}/test_files/matestack-logo.png")
339+
attach_file("files-input", ["#{File.dirname(__FILE__)}/test_files/matestack-logo.png", "#{File.dirname(__FILE__)}/test_files/corgi.mp4"])
340+
341+
click_button "Submit me!"
342+
343+
expect(page).to have_content("file_1 instance: #<ActionDispatch::Http::UploadedFile")
344+
expect(page).to have_content("file_1 with name: matestack-logo.png")
345+
expect(page).to have_content("files[0] instance: #<ActionDispatch::Http::UploadedFile")
346+
expect(page).to have_content("files[1] instance: #<ActionDispatch::Http::UploadedFile")
347+
expect(page).to have_content("files[0] with name: matestack-logo.png")
348+
expect(page).to have_content("files[1] with name: corgi.mp4")
349+
350+
fill_in "title-input", with: "bar"
351+
click_button "Submit me!"
352+
353+
expect(page).to have_content("file_1 instance: nil")
354+
expect(page).to have_content("file_1 with name: nil")
355+
expect(page).to have_content("files[0] instance: nil")
356+
expect(page).to have_content("files[1] instance: nil")
357+
expect(page).to have_content("files[0] with name: nil")
358+
expect(page).to have_content("files[1] with name: nil")
359+
end
360+
295361
end
296362

297363
end

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

Lines changed: 8 additions & 4 deletions
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)