@@ -130,7 +130,7 @@ export function formEnhance<T extends AnyZodObject, M>(
130130 // Using this type in the function argument causes a type recursion error.
131131 const errors = errs as SuperForm < T , M > [ 'errors' ] ;
132132
133- async function validateChange (
133+ async function updateCustomValidity (
134134 validityEl : HTMLElement ,
135135 event : 'blur' | 'input' ,
136136 errors : string [ ] | undefined
@@ -161,7 +161,7 @@ export function formEnhance<T extends AnyZodObject, M>(
161161 ) {
162162 if ( options . validationMethod == 'submit-only' ) return ;
163163
164- console . log ( 'htmlInputChange' , change , event , target ) ;
164+ // console.log('htmlInputChange', change, event, target);
165165
166166 const result = await validateField (
167167 change ,
@@ -177,22 +177,16 @@ export function formEnhance<T extends AnyZodObject, M>(
177177 if ( options . customValidity ) {
178178 const name = CSS . escape ( mergePath ( change ) ) ;
179179 const el = formEl . querySelector < HTMLElement > ( `[name="${ name } "]` ) ;
180- if ( el ) validateChange ( el , event , result . errors ) ;
180+ if ( el ) updateCustomValidity ( el , event , result . errors ) ;
181181 }
182182 }
183183
184- const immediateInputTypes = [
185- 'button' ,
186- 'checkbox' ,
187- 'radio' ,
188- 'range' ,
189- 'submit'
190- ] ;
184+ const immediateInputTypes = [ 'checkbox' , 'radio' , 'range' ] ;
191185
192186 /**
193187 * Some input fields have timing issues with the stores, need to wait in that case.
194188 */
195- function immediateInput ( el : EventTarget | null ) {
189+ function isImmediateInput ( el : EventTarget | null ) {
196190 return (
197191 el &&
198192 ( el instanceof HTMLSelectElement ||
@@ -201,7 +195,6 @@ export function formEnhance<T extends AnyZodObject, M>(
201195 ) ;
202196 }
203197
204- // Add blur event, to check tainted
205198 async function checkBlur ( e : Event ) {
206199 if (
207200 options . validationMethod == 'oninput' ||
@@ -210,23 +203,23 @@ export function formEnhance<T extends AnyZodObject, M>(
210203 return ;
211204 }
212205
213- const target = e . target instanceof HTMLElement ? e . target : null ;
214- const immediateUpdate = immediateInput ( target ) ;
206+ // Wait for changes to update
207+ const immediateUpdate = isImmediateInput ( e . target ) ;
208+ if ( immediateUpdate ) await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ;
215209
216- // Immediate inputs has a timing issue and needs to be waited for
217- if ( immediateUpdate ) {
218- await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ;
219- }
210+ const changes = get ( lastChanges ) ;
211+ if ( ! changes . length ) return ;
220212
221- for ( const change of get ( lastChanges ) ) {
213+ const target = e . target instanceof HTMLElement ? e . target : null ;
214+
215+ for ( const change of changes ) {
222216 htmlInputChange ( change , 'blur' , immediateUpdate ? null : target ) ;
223217 }
218+
224219 // Clear last changes after blur (not after input)
225220 lastChanges . set ( [ ] ) ;
226221 }
227- formEl . addEventListener ( 'focusout' , checkBlur ) ;
228222
229- // Add input event, for custom validity
230223 async function checkInput ( e : Event ) {
231224 if (
232225 options . validationMethod == 'onblur' ||
@@ -235,15 +228,16 @@ export function formEnhance<T extends AnyZodObject, M>(
235228 return ;
236229 }
237230
238- const immediateUpdate = immediateInput ( e . target ) ;
231+ // Wait for changes to update
232+ const immediateUpdate = isImmediateInput ( e . target ) ;
233+ if ( immediateUpdate ) await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ;
239234
240- if ( immediateUpdate ) {
241- await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ;
242- }
235+ const changes = get ( lastChanges ) ;
236+ if ( ! changes . length ) return ;
243237
244238 const target = e . target instanceof HTMLElement ? e . target : null ;
245239
246- for ( const change of get ( lastChanges ) ) {
240+ for ( const change of changes ) {
247241 const hadErrors =
248242 // eslint-disable-next-line @typescript-eslint/no-explicit-any
249243 immediateUpdate || traversePath ( get ( errors ) , change as any ) ;
@@ -265,13 +259,16 @@ export function formEnhance<T extends AnyZodObject, M>(
265259 }
266260 }
267261
262+ formEl . addEventListener ( 'focusout' , checkBlur ) ;
268263 formEl . addEventListener ( 'input' , checkInput ) ;
269264
270265 onDestroy ( ( ) => {
271266 formEl . removeEventListener ( 'focusout' , checkBlur ) ;
272267 formEl . removeEventListener ( 'input' , checkInput ) ;
273268 } ) ;
274269
270+ ///// SvelteKit enhance function //////////////////////////////////
271+
275272 const htmlForm = Form ( formEl , { submitting, delayed, timeout } , options ) ;
276273
277274 let currentRequest : AbortController | null ;
0 commit comments