OnSubmitModifier.swift 933 B

12345678910111213141516171819202122232425
  1. extension View {
  2. /// Adds an action to perform when the user submits a text field within this
  3. /// view (generally via pressing the Enter/Return key). Outer `onSubmit`
  4. /// handlers get called before inner `onSubmit` handlers. To prevent
  5. /// submissions from propagating upwards, use ``View/submitScope()`` after
  6. /// adding the handler.
  7. ///
  8. /// - Parameter action: The action to perform.
  9. public func onSubmit(perform action: @escaping () -> Void) -> some View {
  10. EnvironmentModifier(self) { environment in
  11. environment.with(\.onSubmit) {
  12. environment.onSubmit?()
  13. action()
  14. }
  15. }
  16. }
  17. /// Prevents text field submissions from propagating to this view's
  18. /// ancestors.
  19. public func submitScope() -> some View {
  20. EnvironmentModifier(self) { environment in
  21. environment.with(\.onSubmit, nil)
  22. }
  23. }
  24. }