@@ -4,13 +4,13 @@ import utils from './utils';
44
55/* eslint-disable react/default-props-match-prop-types */
66
7- const convertValidationsToObject = ( validations ) => {
7+ const convertValidationsToObject = validations => {
88 if ( typeof validations === 'string' ) {
99 return validations . split ( / , (? ! [ ^ { [ ] * [ } \] ] ) / g) . reduce ( ( validationsAccumulator , validation ) => {
1010 let args = validation . split ( ':' ) ;
1111 const validateMethod = args . shift ( ) ;
1212
13- args = args . map ( ( arg ) => {
13+ args = args . map ( arg => {
1414 try {
1515 return JSON . parse ( arg ) ;
1616 } catch ( e ) {
@@ -19,7 +19,9 @@ const convertValidationsToObject = (validations) => {
1919 } ) ;
2020
2121 if ( args . length > 1 ) {
22- throw new Error ( 'Formsy does not support multiple args on string validations. Use object format of validations instead.' ) ;
22+ throw new Error (
23+ 'Formsy does not support multiple args on string validations. Use object format of validations instead.' ,
24+ ) ;
2325 }
2426
2527 // Avoid parameter reassignment
@@ -35,21 +37,14 @@ const convertValidationsToObject = (validations) => {
3537const propTypes = {
3638 innerRef : PropTypes . func ,
3739 name : PropTypes . string . isRequired ,
38- required : PropTypes . oneOfType ( [
39- PropTypes . bool ,
40- PropTypes . object ,
41- PropTypes . string ,
42- ] ) ,
43- validations : PropTypes . oneOfType ( [
44- PropTypes . object ,
45- PropTypes . string ,
46- ] ) ,
40+ required : PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . object , PropTypes . string ] ) ,
41+ validations : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . string ] ) ,
4742 value : PropTypes . any , // eslint-disable-line react/forbid-prop-types
4843} ;
4944
5045export { propTypes } ;
5146
52- export default ( Component ) => {
47+ export default Component => {
5348 class WrappedComponent extends React . Component {
5449 constructor ( props ) {
5550 super ( props ) ;
@@ -86,12 +81,8 @@ export default (Component) => {
8681 }
8782
8883 shouldComponentUpdate ( nextProps , nextState ) {
89- const isPropsChanged = Object
90- . keys ( this . props )
91- . some ( k => ( this . props [ k ] !== nextProps [ k ] ) ) ;
92- const isStateChanged = Object
93- . keys ( this . state )
94- . some ( k => ( this . state [ k ] !== nextState [ k ] ) ) ;
84+ const isPropsChanged = Object . keys ( this . props ) . some ( k => this . props [ k ] !== nextProps [ k ] ) ;
85+ const isStateChanged = Object . keys ( this . state ) . some ( k => this . state [ k ] !== nextState [ k ] ) ;
9586
9687 return isPropsChanged || isStateChanged ;
9788 }
@@ -104,8 +95,10 @@ export default (Component) => {
10495 }
10596
10697 // If validations or required is changed, run a new validation
107- if ( ! utils . isSame ( this . props . validations , prevProps . validations ) ||
108- ! utils . isSame ( this . props . required , prevProps . required ) ) {
98+ if (
99+ ! utils . isSame ( this . props . validations , prevProps . validations ) ||
100+ ! utils . isSame ( this . props . required , prevProps . required )
101+ ) {
109102 this . context . formsy . validate ( this ) ;
110103 }
111104 }
@@ -118,23 +111,23 @@ export default (Component) => {
118111 getErrorMessage = ( ) => {
119112 const messages = this . getErrorMessages ( ) ;
120113 return messages . length ? messages [ 0 ] : null ;
121- }
114+ } ;
122115
123116 getErrorMessages = ( ) => {
124117 if ( ! this . isValid ( ) || this . showRequired ( ) ) {
125118 return this . state . externalError || this . state . validationError || [ ] ;
126119 }
127120 return [ ] ;
128- }
121+ } ;
129122
130123 getValue = ( ) => this . state . value ;
131124
132125 setValidations = ( validations , required ) => {
133126 // Add validations to the store itself as the props object can not be modified
134127 this . validations = convertValidationsToObject ( validations ) || { } ;
135- this . requiredValidations = required === true ? { isDefaultRequiredValue : true } :
136- convertValidationsToObject ( required ) ;
137- }
128+ this . requiredValidations =
129+ required === true ? { isDefaultRequiredValue : true } : convertValidationsToObject ( required ) ;
130+ } ;
138131
139132 // By default, we validate after the value has been set.
140133 // A user can override this and pass a second parameter of `false` to skip validation.
@@ -144,14 +137,17 @@ export default (Component) => {
144137 value,
145138 } ) ;
146139 } else {
147- this . setState ( {
148- value,
149- isPristine : false ,
150- } , ( ) => {
151- this . context . formsy . validate ( this ) ;
152- } ) ;
140+ this . setState (
141+ {
142+ value,
143+ isPristine : false ,
144+ } ,
145+ ( ) => {
146+ this . context . formsy . validate ( this ) ;
147+ } ,
148+ ) ;
153149 }
154- }
150+ } ;
155151
156152 hasValue = ( ) => this . state . value !== '' ;
157153
@@ -165,17 +161,19 @@ export default (Component) => {
165161
166162 isValid = ( ) => this . state . isValid ;
167163
168- isValidValue = value =>
169- this . context . formsy . isValidValue . call ( null , this , value ) ;
164+ isValidValue = value => this . context . formsy . isValidValue . call ( null , this , value ) ;
170165
171166 resetValue = ( ) => {
172- this . setState ( {
173- value : this . state . pristineValue ,
174- isPristine : true ,
175- } , ( ) => {
176- this . context . formsy . validate ( this ) ;
177- } ) ;
178- }
167+ this . setState (
168+ {
169+ value : this . state . pristineValue ,
170+ isPristine : true ,
171+ } ,
172+ ( ) => {
173+ this . context . formsy . validate ( this ) ;
174+ } ,
175+ ) ;
176+ } ;
179177
180178 showError = ( ) => ! this . showRequired ( ) && ! this . isValid ( ) ;
181179
@@ -211,11 +209,7 @@ export default (Component) => {
211209 }
212210
213211 function getDisplayName ( component ) {
214- return (
215- component . displayName ||
216- component . name ||
217- ( typeof component === 'string' ? component : 'Component' )
218- ) ;
212+ return component . displayName || component . name || ( typeof component === 'string' ? component : 'Component' ) ;
219213 }
220214
221215 WrappedComponent . displayName = `Formsy(${ getDisplayName ( Component ) } )` ;
0 commit comments