@@ -18,19 +18,6 @@ public class FormControl<FieldName> where FieldName: Hashable {
1818 private( set) public var formState : FormState < FieldName >
1919 var instantFormState : FormState < FieldName >
2020
21- var _currentFocusedField : FieldName ?
22- var currentFocusedField : FieldName ? {
23- get {
24- _currentFocusedField ?? options. focusedFieldOption. focusedFieldBindingValue
25- }
26- set {
27- if options. focusedFieldOption. hasFocusedFieldBinder {
28- return
29- }
30- _currentFocusedField = newValue
31- }
32- }
33-
3421 init ( options: FormOption < FieldName > , formState: Binding < FormState < FieldName > > ) {
3522 self . options = options
3623 self . fields = [ : ]
@@ -213,18 +200,9 @@ public class FormControl<FieldName> where FieldName: Hashable {
213200 guard let firstErrorField else {
214201 return
215202 }
216- currentFocusedField = firstErrorField
217203 options. focusedFieldOption. triggerFocus ( on: firstErrorField)
218204 }
219205
220- @MainActor
221- private func focusOnCurrentField( ) {
222- guard let currentFocusedField else {
223- return
224- }
225- options. focusedFieldOption. triggerFocus ( on: currentFocusedField)
226- }
227-
228206 private func postHandleSubmit( isOveralValid: Bool , errors: FormError < FieldName > , isSubmitSuccessful: Bool ) async {
229207 instantFormState. isValid = isOveralValid
230208 instantFormState. submissionState = . submitted
@@ -242,7 +220,7 @@ public class FormControl<FieldName> where FieldName: Hashable {
242220 try await Task . sleep ( nanoseconds: delayErrorInNanoseconds)
243221 self ? . instantFormState. errors = errors
244222 await self ? . syncFormState ( )
245- await self ? . focusOnCurrentField ( )
223+ await self ? . focusError ( with : errors )
246224 }
247225 }
248226 }
@@ -406,7 +384,9 @@ public class FormControl<FieldName> where FieldName: Hashable {
406384 try await Task . sleep ( nanoseconds: delayErrorInNanoseconds)
407385 self ? . instantFormState. errors = errors
408386 await self ? . syncFormState ( )
409- await self ? . focusOnCurrentField ( )
387+ if validationNames. count == 1 , let firstField = validationNames. first {
388+ await self ? . options. focusedFieldOption. triggerFocus ( on: firstField)
389+ }
410390 }
411391 }
412392 return isValid
@@ -442,7 +422,7 @@ extension FormControl {
442422 try await Task . sleep ( nanoseconds: delayErrorInNanoseconds)
443423 self ? . instantFormState. errors = e
444424 await self ? . syncFormState ( )
445- await self ? . focusOnCurrentField ( )
425+ await self ? . focusError ( with : e )
446426 }
447427 }
448428 }
@@ -476,7 +456,7 @@ extension FormControl {
476456 try await Task . sleep ( nanoseconds: delayErrorInNanoseconds)
477457 self ? . instantFormState. errors = errors
478458 await self ? . syncFormState ( )
479- await self ? . focusOnCurrentField ( )
459+ await self ? . focusError ( with : errors )
480460 }
481461 }
482462 instantFormState. isValid = isValid
@@ -547,13 +527,10 @@ private extension FormControl {
547527 guard await self . trigger ( name: name) else {
548528 return
549529 }
550- await MainActor . run {
551- if self . currentFocusedField == name {
552- return
553- }
554- self . currentFocusedField = name
555- self . options. focusedFieldOption. triggerFocus ( on: name)
530+ guard self . options. delayErrorInNanoseconds == 0 else {
531+ return
556532 }
533+ await self . options. focusedFieldOption. triggerFocus ( on: name)
557534 }
558535 }
559536 }
0 commit comments