File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -19,6 +19,43 @@ public enum ErrorHideBehaviour {
1919 case onFocusLost
2020}
2121
22+ private class FormStateHandler : ObservableObject {
23+ @Published var fieldStates : [ FieldState ] = . empty
24+ @Published var currentFocusedFieldId : String = . empty
25+ @Published var formValidator = FormValidator ( )
26+
27+ func updateFieldStates( newStates: [ FieldState ] ) {
28+ fieldStates = newStates
29+
30+ let focusedField = newStates. first { $0. isFocused }
31+ currentFocusedFieldId = focusedField? . id ?? . empty
32+
33+ // Замыкание onValidateRun вызывается методом validate() FormValidator'a.
34+ formValidator. onValidateRun = { [ weak self] focusOnFirstFailedField in
35+ guard let self else {
36+ return false
37+ }
38+
39+ let resutls = newStates. map { $0. onValidate ( ) }
40+
41+ // Фокус на первом зафейленом филде.
42+ if let index = resutls. firstIndex ( of: false ) , focusOnFirstFailedField {
43+ currentFocusedFieldId = fieldStates [ index] . id
44+ }
45+
46+ return resutls. allSatisfy { $0 }
47+ }
48+ }
49+
50+ func submit( ) {
51+ currentFocusedFieldId = FocusService . getNextFocusFieldId (
52+ states: fieldStates,
53+ currentFocusField: currentFocusedFieldId
54+ )
55+ }
56+ }
57+
58+
2259public struct FormView < Content: View > : View {
2360 @StateObject private var formStateHandler = FormStateHandler ( )
2461 @ViewBuilder private let content : ( FormValidator ) -> Content
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments