Declaration
compose().onSubmit({
of?: SwiftUI.SubmitTriggers,
action: () => void,
})nonisolated func onSubmit(
of triggers: SubmitTriggers = .text,
_ action: @escaping () -> Void
) -> some ViewParameters
options.ofSwiftUI.SubmitTriggersoptions.action*() => voidThe action to perform on submission of a value.
Discussion
Different views may have different triggers for the provided action.
A TextField, or SecureField will trigger this action when the
user hits the hardware or software return key. This modifier may also
bind this action to a default action keyboard shortcut. You may set this
action on an individual view or an entire view hierarchy.
TextField("Username", text: $username)
.onSubmit {
guard viewModel.validate() else { return }
viewModel.login()
}You can use the View.submitScope(_:) modifier to stop a submit
trigger from a control from propagating higher up in the view hierarchy
to higher View.onSubmit(of:_:) modifiers.
Form {
TextField("Username", text: $viewModel.userName)
SecureField("Password", text: $viewModel.password)
TextField("Tags", text: $viewModel.tags)
.submitScope()
}
.onSubmit {
guard viewModel.validate() else { return }
viewModel.login()
}You can use different submit triggers to filter the types of triggers
that should invoke the provided submission action. For example, you
may provide a value of SubmitTriggers.search to only hear
submission triggers that originate from search fields vended by
searchable modifiers.
@StateObject private var viewModel = ViewModel()
NavigationView {
SidebarView()
DetailView()
}
.searchable(
text: $viewModel.searchText,
placement: .sidebar
) {
SuggestionsView()
}
.onSubmit(of: .search) {
viewModel.submitCurrentSearchQuery()
}See also
More in Events and lifecycle.
- onAppearAdds an action to perform before this view appears.
- onChangeAdds a modifier for this view that fires an action when a specific value changes.
- onContinueUserActivityRegisters a handler to invoke in response to a user activity that your app receives.
- onContinuousHoverAdds an action to perform when the pointer enters, moves within, and exits the view's bounds.
- onDisappearAdds an action to perform after this view disappears.
- onDragActivates this view as the source of a drag and drop operation.
- onDragSessionUpdatedSpecifies an action to perform on each update of an ongoing dragging operation activated by draggable(:) or anther drag modifiers.
- onDropDefines the destination of a drag and drop operation that handles the dropped content with a closure that you specify.
- onDropSessionUpdatedSpecifies an action to perform on each update of an ongoing drop operation activated by dropDestination(:) or other drop modifiers.
- onGeometryChangeAdds an action to be performed when a value, created from a geometry proxy, changes.
- onHingeChangeAdds an action to perform when the hinge context of the view hierarchy changes.
- onHoverAdds an action to perform when the user moves the pointer over or away from the view's frame.