Skip to content

Commit 0d8d26c

Browse files
committed
Update field focus
1 parent afeaa29 commit 0d8d26c

File tree

2 files changed

+11
-48
lines changed

2 files changed

+11
-48
lines changed

Sources/FormHook/Form.swift

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Sources/FormHook/Hook/UseForm.swift

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,6 @@ public struct FormOption<FieldName> where FieldName: Hashable {
118118
private let anyFocusedFieldBinder: Any?
119119
private let onFocusField: ((FieldName) -> Void)?
120120

121-
var hasFocusedFieldBinder: Bool {
122-
anyFocusedFieldBinder != nil
123-
}
124-
125-
var focusedFieldBindingValue: FieldName? {
126-
if #available(macOS 12.0, iOS 15.0, tvOS 15.0, *) {
127-
return focusedFieldBinder?.wrappedValue
128-
}
129-
return nil
130-
}
131-
132-
@available(macOS 12.0, iOS 15.0, tvOS 15.0, *)
133-
var focusedFieldBinder: FocusState<FieldName?>.Binding? {
134-
anyFocusedFieldBinder as? FocusState<FieldName?>.Binding
135-
}
136-
137121
@available(macOS 12.0, iOS 15.0, tvOS 15.0, *)
138122
init(_ focusedFieldBinder: FocusState<FieldName?>.Binding) {
139123
self.anyFocusedFieldBinder = focusedFieldBinder
@@ -145,11 +129,13 @@ public struct FormOption<FieldName> where FieldName: Hashable {
145129
self.onFocusField = onFocusField
146130
}
147131

132+
@MainActor
148133
func triggerFocus(on field: FieldName) {
149134
if let onFocusField {
150135
return onFocusField(field)
151136
}
152137
if #available(macOS 12.0, iOS 15.0, tvOS 15.0, *) {
138+
let focusedFieldBinder = anyFocusedFieldBinder as? FocusState<FieldName?>.Binding
153139
focusedFieldBinder?.wrappedValue = field
154140
}
155141
}

0 commit comments

Comments
 (0)