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

Fix react-native async onChange prop in ConnectedField #4391 #4392

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 34 additions & 17 deletions src/ConnectedField.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Structure } from './types.js.flow'
import type { Props } from './ConnectedField.types'
import validateComponentProp from './util/validateComponentProp'
import isEvent from './events/isEvent'
import isPromise from 'is-promise'

const propsToNotUpdateFor = ['_reduxForm']

Expand Down Expand Up @@ -45,6 +46,15 @@ const eventDataTransferSetData = (event, key, value) => {
}
}

const getDefaultPreventedResult = defaultPrevented =>
new Promise(resolve => {
if (isPromise(defaultPrevented)) {
return defaultPrevented.then(resolve)
}

return resolve(defaultPrevented)
})

const createConnectedField = (structure: Structure<*, *>) => {
const { deepEqual, getIn } = structure
const getSyncError = (syncErrors: Object, name: string) => {
Expand Down Expand Up @@ -129,15 +139,18 @@ const createConnectedField = (structure: Structure<*, *>) => {
defaultPrevented = onChange(event, newValue, previousValue, name)
}
}
if (!defaultPrevented) {
// dispatch change action
dispatch(_reduxForm.change(name, newValue))

// call post-change callback
if (_reduxForm.asyncValidate) {
_reduxForm.asyncValidate(name, newValue, 'change')
return getDefaultPreventedResult(defaultPrevented).then(result => {
if (!result) {
// dispatch change action
dispatch(_reduxForm.change(name, newValue))

// call post-change callback
if (_reduxForm.asyncValidate) {
_reduxForm.asyncValidate(name, newValue, 'change')
}
}
}
})
}

handleFocus = (event: any) => {
Expand All @@ -161,9 +174,11 @@ const createConnectedField = (structure: Structure<*, *>) => {
}
}

if (!defaultPrevented) {
dispatch(_reduxForm.focus(name))
}
return getDefaultPreventedResult(defaultPrevented).then(result => {
if (!result) {
dispatch(_reduxForm.focus(name))
}
})
}

handleBlur = (event: any) => {
Expand Down Expand Up @@ -205,15 +220,17 @@ const createConnectedField = (structure: Structure<*, *>) => {
}
}

if (!defaultPrevented) {
// dispatch blur action
dispatch(_reduxForm.blur(name, newValue))
return getDefaultPreventedResult(defaultPrevented).then(result => {
if (!result) {
// dispatch blur action
dispatch(_reduxForm.blur(name, newValue))

// call post-blur callback
if (_reduxForm.asyncValidate) {
_reduxForm.asyncValidate(name, newValue, 'blur')
// call post-blur callback
if (_reduxForm.asyncValidate) {
_reduxForm.asyncValidate(name, newValue, 'blur')
}
}
}
})
}

handleDragStart = (event: any) => {
Expand Down