diff --git a/lib/react/Form/Form.jsx b/lib/react/Form/Form.jsx index 8edc4d8..3924001 100755 --- a/lib/react/Form/Form.jsx +++ b/lib/react/Form/Form.jsx @@ -102,6 +102,8 @@ export default memo(Form); export function onSubmit(event, initValues, cb) { event.preventDefault(); + if (event.target.hasAttribute('pristine')) return; + if (!event.target.reportValidity()) return; event.stopPropagation(); diff --git a/lib/react/Form/Form.spec.js b/lib/react/Form/Form.spec.js index 507a74b..2817703 100644 --- a/lib/react/Form/Form.spec.js +++ b/lib/react/Form/Form.spec.js @@ -14,6 +14,7 @@ describe('Form', () => { let initValues; function generateFakeSubmissionEvent({ + attrs = { pristine: false }, valid = true } = {}) { const spies = { @@ -28,6 +29,7 @@ describe('Form', () => { stopPropagation() { spies.stopPropagationCalled = true }, target: { elements: [], + hasAttribute(name) { return attrs[name] }, reportValidity() { spies.reportValidityCalled = true; return valid; @@ -65,8 +67,17 @@ describe('Form', () => { expect(spies.stopPropagationCalled).to.be.true; }); + it('should abort when form is pristine', () => { + let cbCalled = false; + const { event } = generateFakeSubmissionEvent({ attrs: { pristine: true } }); + + onSubmit(event, initValues, () => cbCalled = true); + + expect(cbCalled).to.be.false; + }); + it('should abort when validation fails', () => { - const { event, spies } = generateFakeSubmissionEvent({ valid: false}); + const { event, spies } = generateFakeSubmissionEvent({ valid: false }); onSubmit(event, initValues, noop);