Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/form change event #281

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/FormulateForm.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<form
:class="classes"
@change="formChanged"
@submit.prevent="formSubmitted"
>
<FormulateSchema
Expand Down Expand Up @@ -165,6 +166,10 @@ export default {
this.errorComponents.push(component)
}
},
formChanged () {
const submission = new FormSubmission(this)
this.$emit('change', submission)
},
formSubmitted () {
if (this.submissionPromise) {
return this.submissionPromise
Expand Down
24 changes: 23 additions & 1 deletion test/unit/FormulateForm.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue'
import { mount, shallowMount } from '@vue/test-utils'
import { mount } from '@vue/test-utils'
import flushPromises from 'flush-promises'
import Formulate from '../../src/Formulate.js'
import FormSubmission from '../../src/FormSubmission.js'
Expand Down Expand Up @@ -253,6 +253,28 @@ describe('FormulateForm', () => {
expect(wrapper.emitted('submit-raw')[0][0]).toBeInstanceOf(FormSubmission)
})

it('emits an instance of FormSubmission on change', async () => {
const wrapper = mount(FormulateForm, {
slots: { default: `<FormulateInput type="text" validation="required|in:bar" name="testinput" />` }
})
const input = wrapper.find('input[type="text"]')
input.setValue('foo')
input.trigger('change')
await flushPromises()
expect(wrapper.emitted('change')[0][0]).toBeInstanceOf(FormSubmission)
})

it('emits a single event on Form input', async () => {
const wrapper = mount(FormulateForm, {
slots: { default: `<FormulateInput type="text" validation="required|in:bar" name="testinput" />` }
})
const input = wrapper.find('input[type="text"]')
input.setValue('foo')
input.trigger('input')
await flushPromises()
expect(wrapper.emitted('input').length).toBe(1)
})

it('resolves hasValidationErrors to true', async () => {
const wrapper = mount(FormulateForm, {
slots: { default: '<FormulateInput type="text" validation="required" name="testinput" />' }
Expand Down