@@ -8,7 +8,21 @@ function isCheckbox(element) {
88 return element . getAttribute && element . getAttribute ( 'type' ) === 'checkbox' ;
99}
1010
11- export const createForm = config => {
11+ function isFileInput ( element ) {
12+ return element . getAttribute && element . getAttribute ( 'type' ) === 'file' ;
13+ }
14+
15+ function resolveValue ( element ) {
16+ if ( isFileInput ( element ) ) {
17+ return element . files ;
18+ } else if ( isCheckbox ( element ) ) {
19+ return element . checked ;
20+ } else {
21+ return element . value ;
22+ }
23+ }
24+
25+ export const createForm = ( config ) => {
1226 let initialValues = config . initialValues || { } ;
1327
1428 const validationSchema = config . validationSchema ;
@@ -31,12 +45,14 @@ export const createForm = config => {
3145 const isSubmitting = writable ( false ) ;
3246 const isValidating = writable ( false ) ;
3347
34- const isValid = derived ( errors , $errors => {
35- const noErrors = util . getValues ( $errors ) . every ( field => field === NO_ERROR ) ;
48+ const isValid = derived ( errors , ( $errors ) => {
49+ const noErrors = util
50+ . getValues ( $errors )
51+ . every ( ( field ) => field === NO_ERROR ) ;
3652 return noErrors ;
3753 } ) ;
3854
39- const modified = derived ( form , $form => {
55+ const modified = derived ( form , ( $form ) => {
4056 const object = util . assignDeep ( $form , false ) ;
4157
4258 for ( let key in $form ) {
@@ -46,14 +62,14 @@ export const createForm = config => {
4662 return object ;
4763 } ) ;
4864
49- const isModified = derived ( modified , $modified => {
65+ const isModified = derived ( modified , ( $modified ) => {
5066 return util . getValues ( $modified ) . includes ( true ) ;
5167 } ) ;
5268
5369 function validateField ( field ) {
5470 return util
5571 . subscribeOnce ( form )
56- . then ( values => validateFieldValue ( field , values [ field ] ) ) ;
72+ . then ( ( values ) => validateFieldValue ( field , values [ field ] ) ) ;
5773 }
5874
5975 function validateFieldValue ( field , value ) {
@@ -65,7 +81,7 @@ export const createForm = config => {
6581 return validationSchema
6682 . validateAt ( field , get ( form ) )
6783 . then ( ( ) => util . update ( errors , field , '' ) )
68- . catch ( error => util . update ( errors , field , error . message ) )
84+ . catch ( ( error ) => util . update ( errors , field , error . message ) )
6985 . finally ( ( ) => {
7086 isValidating . set ( false ) ;
7187 } ) ;
@@ -75,7 +91,7 @@ export const createForm = config => {
7591 isValidating . set ( true ) ;
7692 return Promise . resolve ( )
7793 . then ( ( ) => validateFunction ( { [ field ] : value } ) )
78- . then ( errs =>
94+ . then ( ( errs ) =>
7995 util . update ( errors , field , ! util . isNullish ( errs ) ? errs [ field ] : '' ) ,
8096 )
8197 . finally ( ( ) => {
@@ -94,7 +110,7 @@ export const createForm = config => {
94110 function handleChange ( event ) {
95111 const element = event . target ;
96112 const field = element . name || element . id ;
97- const value = isCheckbox ( element ) ? element . checked : element . value ;
113+ const value = resolveValue ( element ) ;
98114
99115 return updateValidateField ( field , value ) ;
100116 }
@@ -106,13 +122,13 @@ export const createForm = config => {
106122
107123 isSubmitting . set ( true ) ;
108124
109- return util . subscribeOnce ( form ) . then ( values => {
125+ return util . subscribeOnce ( form ) . then ( ( values ) => {
110126 if ( typeof validateFunction === 'function' ) {
111127 isValidating . set ( true ) ;
112128
113129 return Promise . resolve ( )
114130 . then ( ( ) => validateFunction ( values ) )
115- . then ( error => {
131+ . then ( ( error ) => {
116132 if ( util . isNullish ( error ) || util . getValues ( error ) . length === 0 ) {
117133 clearErrorsAndSubmit ( values ) ;
118134 } else {
@@ -131,11 +147,11 @@ export const createForm = config => {
131147 . validate ( values , { abortEarly : false } )
132148 . then ( ( ) => clearErrorsAndSubmit ( values ) )
133149 // eslint-disable-next-line unicorn/catch-error-name
134- . catch ( yupErrors => {
150+ . catch ( ( yupErrors ) => {
135151 if ( yupErrors && yupErrors . inner ) {
136152 const updatedErrors = getInitial . errors ( ) ;
137153
138- yupErrors . inner . map ( error =>
154+ yupErrors . inner . map ( ( error ) =>
139155 util . set ( updatedErrors , error . path , error . message ) ,
140156 ) ;
141157
0 commit comments