Skip to content

Commit f8344bb

Browse files
committed
Update Example
1 parent 2567f99 commit f8344bb

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

Example/FormHookExample/ContentView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ struct ContentView: View {
3535

3636
@FocusState var focusField: FormFieldName?
3737

38+
// func onFocusField(_ field: FormFieldName) {
39+
// focusField = field
40+
// }
41+
3842
@ViewBuilder
3943
var body: some View {
44+
// Pass an onFocusField closure
45+
// ContextualForm(onFocusField: onFocusField(_:)) {}
4046
ContextualForm(focusedFieldBinder: $focusField) { form in
4147
Form {
4248
Section("Name") {

Sources/FormHook/Controller.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public struct FieldOption<FieldName, Value> {
2222
public typealias ControllerRenderOption<FieldName, Value> = (field: FieldOption<FieldName, Value>, fieldState: FieldState, formState: FormState<FieldName>) where FieldName: Hashable
2323

2424
public struct Controller<Content, FieldName, Value>: View where Content: View, FieldName: Hashable {
25+
let form: FormControl<FieldName>?
2526
let name: FieldName
2627
let defaultValue: Value
2728
let rules: any Validator<Value>
@@ -31,6 +32,7 @@ public struct Controller<Content, FieldName, Value>: View where Content: View, F
3132
let render: (ControllerRenderOption<FieldName, Value>) -> Content
3233

3334
public init(
35+
form: FormControl<FieldName>? = nil,
3436
name: FieldName,
3537
defaultValue: Value,
3638
rules: any Validator<Value> = NoopValidator(),
@@ -39,6 +41,7 @@ public struct Controller<Content, FieldName, Value>: View where Content: View, F
3941
fieldOrdinal: Int? = nil,
4042
@ViewBuilder render: @escaping (ControllerRenderOption<FieldName, Value>) -> Content
4143
) {
44+
self.form = form
4245
self.name = name
4346
self.defaultValue = defaultValue
4447
self.rules = rules
@@ -50,7 +53,7 @@ public struct Controller<Content, FieldName, Value>: View where Content: View, F
5053

5154
public var body: some View {
5255
HookScope {
53-
let renderOption = useController(name: name, defaultValue: defaultValue, rules: rules, shouldUnregister: shouldUnregister, unregisterOption: unregisterOption, fieldOrdinal: fieldOrdinal)
56+
let renderOption = useController(form: form, name: name, defaultValue: defaultValue, rules: rules, shouldUnregister: shouldUnregister, unregisterOption: unregisterOption, fieldOrdinal: fieldOrdinal)
5457
render(renderOption)
5558
}
5659
}

Sources/FormHook/Hook/UseController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import Foundation
99
import Hooks
1010

1111
public func useController<FieldName, Value>(
12+
form: FormControl<FieldName>? = nil,
1213
name: FieldName,
1314
defaultValue: Value,
1415
rules: any Validator<Value>,
1516
shouldUnregister: Bool = true,
1617
unregisterOption: UnregisterOption = [],
1718
fieldOrdinal: Int? = nil
1819
) -> ControllerRenderOption<FieldName, Value> where FieldName: Hashable {
19-
let form = useContext(Context<FormControl<FieldName>>.self)
20+
let form = form ?? useContext(Context<FormControl<FieldName>>.self)
2021
let registration = form.register(name: name, options: RegisterOption(fieldOrdinal: fieldOrdinal, rules: rules, defaultValue: defaultValue, shouldUnregister: shouldUnregister))
2122

2223
let preservedChangedArray = [

Sources/FormHook/Hook/UseForm.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public func useForm<FieldName>(
1515
resolver: Resolver<FieldName>? = nil,
1616
context: Any? = nil,
1717
shouldUnregister: Bool = true,
18-
shouldFocusError: Bool,
18+
shouldFocusError: Bool = true,
1919
delayErrorInNanoseconds: UInt64 = 0,
2020
@_implicitSelfCapture onFocusField: @escaping (FieldName) -> Void
2121
) -> FormControl<FieldName> where FieldName: Hashable {

0 commit comments

Comments
 (0)