Skip to content

Commit

Permalink
Add default onSubmit function
Browse files Browse the repository at this point in the history
  • Loading branch information
dpikt committed Dec 12, 2017
1 parent 95c7a15 commit d799288
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A wrapper around the `reduxForm` HOC exported from

1. Makes extra options available for configuring the form
2. Wraps every rejected `onSubmit` in a `SubmissionError`. If the thrown error has an `errors` property, its value will be passed to `SubmissionError`.
3. Provides a default `onSubmit` function that resolves successfully and logs a warning.

The extra options that can be provided to `lpForm` are as follows:

Expand Down
15 changes: 13 additions & 2 deletions src/lp-form.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React from 'react'
import { reduxForm } from 'redux-form'
import { createFilterFunction, wrapSubmissionPromise, wrapDisplayName } from './utils'
import {
createFilterFunction,
wrapSubmissionPromise,
wrapDisplayName,
} from './utils'
import validate from './validate'

/**
Expand All @@ -9,6 +13,7 @@ import validate from './validate'
*
* 1. Makes extra options available for configuring the form
* 2. Wraps every rejected `onSubmit` in a `SubmissionError`. If the thrown error has an `errors` property, its value will be passed to `SubmissionError`.
* 3. Provides a default `onSubmit` function that resolves successfully and logs a warning.
*
* The extra options that can be provided to `lpForm` are as follows:
*
Expand Down Expand Up @@ -45,6 +50,12 @@ import validate from './validate'
*
*/

function defaultOnSubmit (...args) {
// eslint-disable-next-line no-console
console.warn('WARNING: no onSubmit function specified. Form will submit successfully by default.')
return Promise.resolve(...args)
}

function lpForm (options={}) {
return Wrapped => {
const WrappedWithForm = reduxForm()(Wrapped)
Expand All @@ -53,7 +64,7 @@ function lpForm (options={}) {
const {
name,
initialValues,
onSubmit,
onSubmit=defaultOnSubmit,
submitFilters,
initialValuesFilters,
constraints={},
Expand Down
12 changes: 12 additions & 0 deletions test/lp-form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ test('lpForm: wraps rejected promises in a SubmissionError', () => {
})
})

test('lpForm: provides a default onSubmit that submits successfully', () => {
expect.assertions(1)
const Wrapped = () => <div> Hi </div>
const Form = lpForm()(Wrapped)
const wrapper = mount(<Form />)
const formConfig = wrapper.find(Wrapped).props()

return formConfig.onSubmit(INITIAL_VALUES).then(values => {
expect(values).toEqual(INITIAL_VALUES)
})
})

test('lpForm: creates validation function with constraints', () => {
const constraints = { 'foo': { presence: true } }
const Wrapped = () => <div> Hi </div>
Expand Down

0 comments on commit d799288

Please sign in to comment.