Files
todos/App/Sources/Views/NewTask/NewTaskContent.swift
T

88 lines
2.6 KiB
Swift

import SwiftUI
extension NewTaskView {
/// A container view that wraps form content with tap-to-dismiss keyboard functionality.
///
/// This view provides a transparent overlay that appears when any text field has focus,
/// allowing users to dismiss the keyboard by tapping anywhere outside the input fields.
/// This improves the user experience by making it easier to exit text editing mode without
/// needing to explicitly tap a "Done" button or similar control.
struct Content<Content: View>: View {
// MARK: Properties
/// Binding to track which text field currently has keyboard focus.
@FocusState.Binding
var inputFocus: InputFieldFocus?
/// The view content to display.
private let content: Content
// MARK: Initialization
/// Initializes the new task content with custom form content.
///
/// - Parameters:
/// - inputFocus: A binding to track keyboard focus state
/// - content: A closure that provides the view content to render
init(
inputFocus: FocusState<InputFieldFocus?>.Binding,
@ViewBuilder content: () -> Content
) {
self._inputFocus = inputFocus
self.content = content()
}
// MARK: Body
/// The view's body that combines the form content with a dismissal overlay.
///
/// This approach allows users to tap anywhere on the screen to exit text editing
/// mode, which is particularly useful on iOS where the keyboard can obscure
/// significant screen space.
var body: some View {
ZStack {
content
if inputFocus != nil {
Color.clear
.contentShape(Rectangle())
.onTapGesture {
inputFocus = nil
}
}
}
}
}
}
// MARK: - Previews
#Preview("New todo task content") {
struct PreviewWrapper: View {
@FocusState
private var inputFocus: InputFieldFocus?
var body: some View {
NewTaskView.Content(inputFocus: $inputFocus) {
Form {
TextField(
"Title",
text: .constant(
"Sample Task"
)
)
.focused(
$inputFocus,
equals: .title
)
}
}
}
}
return PreviewWrapper()
}