Skip to content

Commit

Permalink
Formatted all code with Prettier (redux-form#2905)
Browse files Browse the repository at this point in the history
* Prettier

* Added npm script
  • Loading branch information
erikras authored May 9, 2017
1 parent b67edf1 commit ba30caf
Show file tree
Hide file tree
Showing 190 changed files with 10,573 additions and 7,959 deletions.
2 changes: 1 addition & 1 deletion babel-lodash-es.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function () {
module.exports = function() {
return {
visitor: {
ImportDeclaration(path) {
Expand Down
2 changes: 1 addition & 1 deletion immutable.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/immutable');
module.exports = require('./lib/immutable')
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"example:submitValidation": "form=submitValidation npm run example",
"example:syncValidation": "form=syncValidation npm run example",
"example:wizard": "form=wizard npm run example",
"format": "prettier-eslint \"src/**/*.js\" --write --prettier.semi=false",
"prepublish": "npm run test && npm run clean && npm run build",
"test": "mocha --compilers js:babel-register --recursive --recursive \"src/**/__tests__/*\" --require src/__tests__/setup.js",
"test:watch": "npm test -- --watch",
Expand Down
144 changes: 91 additions & 53 deletions src/ConnectedField.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Component, createElement } from 'react';
import PropTypes from 'prop-types';
import { Component, createElement } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import createFieldProps from './createFieldProps'
import onChangeValue from './events/onChangeValue'
import { dataKey } from './util/eventConsts'
import plain from './structure/plain'

const propsToNotUpdateFor = [
'_reduxForm'
]
const propsToNotUpdateFor = ['_reduxForm']

const createConnectedField = ({ deepEqual, getIn, toJS }) => {

const getSyncError = (syncErrors, name) => {
const error = plain.getIn(syncErrors, name)
// Because the error for this field might not be at a level in the error structure where
Expand Down Expand Up @@ -40,9 +37,15 @@ const createConnectedField = ({ deepEqual, getIn, toJS }) => {
shouldComponentUpdate(nextProps) {
const nextPropsKeys = Object.keys(nextProps)
const thisPropsKeys = Object.keys(this.props)
return nextPropsKeys.length !== thisPropsKeys.length || nextPropsKeys.some(prop => {
return !~propsToNotUpdateFor.indexOf(prop) && !deepEqual(this.props[ prop ], nextProps[ prop ])
})
return (
nextPropsKeys.length !== thisPropsKeys.length ||
nextPropsKeys.some(prop => {
return (
!~propsToNotUpdateFor.indexOf(prop) &&
!deepEqual(this.props[prop], nextProps[prop])
)
})
)
}

isPristine() {
Expand All @@ -58,18 +61,30 @@ const createConnectedField = ({ deepEqual, getIn, toJS }) => {
}

handleChange(event) {
const { name, dispatch, parse, normalize, onChange, _reduxForm, value: previousValue } = this.props
const {
name,
dispatch,
parse,
normalize,
onChange,
_reduxForm,
value: previousValue
} = this.props
const newValue = onChangeValue(event, { name, parse, normalize })

let defaultPrevented = false
if (onChange) {
onChange({
...event,
preventDefault: () => {
defaultPrevented = true
return event.preventDefault()
}
}, newValue, previousValue)
onChange(
{
...event,
preventDefault: () => {
defaultPrevented = true
return event.preventDefault()
}
},
newValue,
previousValue
)
}
if (!defaultPrevented) {
// dispatch change action
Expand Down Expand Up @@ -97,7 +112,16 @@ const createConnectedField = ({ deepEqual, getIn, toJS }) => {
}

handleBlur(event) {
const { name, dispatch, parse, normalize, onBlur, _reduxForm, _value, value: previousValue } = this.props
const {
name,
dispatch,
parse,
normalize,
onBlur,
_reduxForm,
_value,
value: previousValue
} = this.props
let newValue = onChangeValue(event, { name, parse, normalize })

// for checkbox and radio, if the value property of checkbox or radio equals
Expand All @@ -108,13 +132,17 @@ const createConnectedField = ({ deepEqual, getIn, toJS }) => {

let defaultPrevented = false
if (onBlur) {
onBlur({
...event,
preventDefault: () => {
defaultPrevented = true
return event.preventDefault()
}
}, newValue, previousValue)
onBlur(
{
...event,
preventDefault: () => {
defaultPrevented = true
return event.preventDefault()
}
},
newValue,
previousValue
)
}

if (!defaultPrevented) {
Expand All @@ -138,18 +166,28 @@ const createConnectedField = ({ deepEqual, getIn, toJS }) => {
}

handleDrop(event) {
const { name, dispatch, onDrop, _reduxForm, value: previousValue } = this.props
const {
name,
dispatch,
onDrop,
_reduxForm,
value: previousValue
} = this.props
const newValue = event.dataTransfer.getData(dataKey)

let defaultPrevented = false
if (onDrop) {
onDrop({
...event,
preventDefault: () => {
defaultPrevented = true
return event.preventDefault()
}
}, newValue, previousValue)
onDrop(
{
...event,
preventDefault: () => {
defaultPrevented = true
return event.preventDefault()
}
},
newValue,
previousValue
)
}

if (!defaultPrevented) {
Expand All @@ -165,27 +203,24 @@ const createConnectedField = ({ deepEqual, getIn, toJS }) => {
withRef,
name,
// remove props that are part of redux internals:
_reduxForm, // eslint-disable-line no-unused-vars
normalize, // eslint-disable-line no-unused-vars
onBlur, // eslint-disable-line no-unused-vars
onChange, // eslint-disable-line no-unused-vars
onFocus, // eslint-disable-line no-unused-vars
_reduxForm, // eslint-disable-line no-unused-vars
normalize, // eslint-disable-line no-unused-vars
onBlur, // eslint-disable-line no-unused-vars
onChange, // eslint-disable-line no-unused-vars
onFocus, // eslint-disable-line no-unused-vars
onDragStart, // eslint-disable-line no-unused-vars
onDrop, // eslint-disable-line no-unused-vars
onDrop, // eslint-disable-line no-unused-vars
...rest
} = this.props
const { custom, ...props } = createFieldProps({ getIn, toJS },
name,
{
...rest,
form: _reduxForm.form,
onBlur: this.handleBlur,
onChange: this.handleChange,
onDrop: this.handleDrop,
onDragStart: this.handleDragStart,
onFocus: this.handleFocus
}
)
const { custom, ...props } = createFieldProps({ getIn, toJS }, name, {
...rest,
form: _reduxForm.form,
onBlur: this.handleBlur,
onChange: this.handleChange,
onDrop: this.handleDrop,
onDragStart: this.handleDragStart,
onFocus: this.handleFocus
})
if (withRef) {
custom.ref = 'renderedComponent'
}
Expand All @@ -200,7 +235,8 @@ const createConnectedField = ({ deepEqual, getIn, toJS }) => {
}

ConnectedField.propTypes = {
component: PropTypes.oneOfType([ PropTypes.func, PropTypes.string ]).isRequired,
component: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
.isRequired,
props: PropTypes.object
}

Expand All @@ -209,7 +245,9 @@ const createConnectedField = ({ deepEqual, getIn, toJS }) => {
const { name, _reduxForm: { initialValues, getFormState } } = ownProps
const formState = getFormState(state)
const initialState = getIn(formState, `initial.${name}`)
const initial = initialState !== undefined ? initialState : (initialValues && getIn(initialValues, name))
const initial = initialState !== undefined
? initialState
: initialValues && getIn(initialValues, name)
const value = getIn(formState, `values.${name}`)
const submitting = getIn(formState, 'submitting')
const syncError = getSyncError(getIn(formState, 'syncErrors'), name)
Expand Down
70 changes: 41 additions & 29 deletions src/ConnectedFieldArray.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { Component, createElement } from 'react';
import PropTypes from 'prop-types';
import { Component, createElement } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import createFieldArrayProps from './createFieldArrayProps'
import { mapValues } from 'lodash'
import plain from './structure/plain'

const propsToNotUpdateFor = [
'_reduxForm',
'value'
]
const propsToNotUpdateFor = ['_reduxForm', 'value']

const createConnectedFieldArray = ({ deepEqual, getIn, size }) => {

const getSyncError = (syncErrors, name) => {
// For an array, the error can _ONLY_ be under _error.
// This is why this getSyncError is not the same as the
Expand All @@ -39,20 +35,29 @@ const createConnectedFieldArray = ({ deepEqual, getIn, size }) => {
const nextValue = nextProps.value

if (thisValue && nextValue) {
if (thisValue.length !== nextValue.length || thisValue.every(val => nextValue.some(next => deepEqual(val, next)))) {
if (
thisValue.length !== nextValue.length ||
thisValue.every(val => nextValue.some(next => deepEqual(val, next)))
) {
return true
}
}

const nextPropsKeys = Object.keys(nextProps)
const thisPropsKeys = Object.keys(this.props)
return nextPropsKeys.length !== thisPropsKeys.length || nextPropsKeys.some(prop => {
// useful to debug rerenders
// if (!plain.deepEqual(this.props[ prop ], nextProps[ prop ])) {
// console.info(prop, 'changed', this.props[ prop ], '==>', nextProps[ prop ])
// }
return !~propsToNotUpdateFor.indexOf(prop) && !deepEqual(this.props[ prop ], nextProps[ prop ])
})
return (
nextPropsKeys.length !== thisPropsKeys.length ||
nextPropsKeys.some(prop => {
// useful to debug rerenders
// if (!plain.deepEqual(this.props[ prop ], nextProps[ prop ])) {
// console.info(prop, 'changed', this.props[ prop ], '==>', nextProps[ prop ])
// }
return (
!~propsToNotUpdateFor.indexOf(prop) &&
!deepEqual(this.props[prop], nextProps[prop])
)
})
)
}

get dirty() {
Expand Down Expand Up @@ -101,7 +106,8 @@ const createConnectedFieldArray = ({ deepEqual, getIn, size }) => {
}

ConnectedFieldArray.propTypes = {
component: PropTypes.oneOfType([ PropTypes.func, PropTypes.string ]).isRequired,
component: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
.isRequired,
props: PropTypes.object
}

Expand All @@ -113,7 +119,9 @@ const createConnectedFieldArray = ({ deepEqual, getIn, size }) => {
(state, ownProps) => {
const { name, _reduxForm: { initialValues, getFormState } } = ownProps
const formState = getFormState(state)
const initial = getIn(formState, `initial.${name}`) || (initialValues && getIn(initialValues, name))
const initial =
getIn(formState, `initial.${name}`) ||
(initialValues && getIn(initialValues, name))
const value = getIn(formState, `values.${name}`)
const submitting = getIn(formState, 'submitting')
const syncError = getSyncError(getIn(formState, 'syncErrors'), name)
Expand Down Expand Up @@ -147,18 +155,22 @@ const createConnectedFieldArray = ({ deepEqual, getIn, size }) => {
arraySwap,
arrayUnshift
} = _reduxForm
return mapValues({
arrayInsert,
arrayMove,
arrayPop,
arrayPush,
arrayRemove,
arrayRemoveAll,
arrayShift,
arraySplice,
arraySwap,
arrayUnshift
}, actionCreator => bindActionCreators(actionCreator.bind(null, name), dispatch))
return mapValues(
{
arrayInsert,
arrayMove,
arrayPop,
arrayPush,
arrayRemove,
arrayRemoveAll,
arrayShift,
arraySplice,
arraySwap,
arrayUnshift
},
actionCreator =>
bindActionCreators(actionCreator.bind(null, name), dispatch)
)
},
undefined,
{ withRef: true }
Expand Down
Loading

0 comments on commit ba30caf

Please sign in to comment.