Fixed the keyboard dismissal for the NewTaskView view in the app target.
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import SwiftData
|
||||
import SwiftUI
|
||||
|
||||
/// A view for creating new todo tasks.
|
||||
/// A view for creating new todo tasks with an intuitive form interface.
|
||||
///
|
||||
/// This view provides a form interface for users to input todo task details including:
|
||||
/// - *Title* (required)
|
||||
/// - *Note* (optional)
|
||||
/// - *Due date* (required)
|
||||
/// This view presents a modal sheet that allows users to create new todo tasks by providing
|
||||
/// task details through a structured form. The view integrates with SwiftData for persistence
|
||||
/// and implements smart keyboard focus management for an enhanced input experience.
|
||||
struct NewTaskView: View {
|
||||
|
||||
// MARK: Properties
|
||||
@@ -18,7 +17,7 @@ struct NewTaskView: View {
|
||||
/// SwiftData model context for saving todo tasks.
|
||||
@Environment(\.modelContext)
|
||||
private var modelContext
|
||||
|
||||
|
||||
/// Tracks which text field currently has keyboard focus.
|
||||
@FocusState
|
||||
private var inputFocus: InputFieldFocus?
|
||||
@@ -30,6 +29,14 @@ struct NewTaskView: View {
|
||||
// MARK: Initialization
|
||||
|
||||
/// Initializes a new task view with a fresh view model instance.
|
||||
///
|
||||
/// Creates a new `NewTaskViewModel` with default values:
|
||||
/// - Empty title
|
||||
/// - Empty note
|
||||
/// - Due date set to current date
|
||||
///
|
||||
/// The view model is stored as `@State` to ensure it persists across view updates
|
||||
/// and maintains its state throughout the view's lifecycle.
|
||||
init() {
|
||||
_viewModel = State(initialValue: .init())
|
||||
}
|
||||
@@ -38,15 +45,10 @@ struct NewTaskView: View {
|
||||
|
||||
/// The content and behavior of the view.
|
||||
///
|
||||
/// Displays a navigation stack containing a form with sections for:
|
||||
/// - Task details (title and note fields)
|
||||
/// - Due date picker with graphical style
|
||||
///
|
||||
/// The toolbar includes:
|
||||
/// - Cancel button to dismiss without saving
|
||||
/// - Confirm button to persist the todo and dismiss (disabled when title is empty)
|
||||
/// Constructs a form interface wrapped in the custom content container that provides
|
||||
/// tap-to-dismiss keyboard functionality.
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Content(inputFocus: $inputFocus) {
|
||||
Form {
|
||||
Section("view.new-task.section.task") {
|
||||
TextField(
|
||||
@@ -91,22 +93,22 @@ struct NewTaskView: View {
|
||||
.datePickerStyle(.graphical)
|
||||
}
|
||||
}
|
||||
.navigationTitle("view.new-task.navigation.title")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.onResignFocus()
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button(role: .cancel) {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
.navigationTitle("view.new-task.navigation.title")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button(role: .cancel) {
|
||||
dismiss()
|
||||
}
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button(role: .confirm) {
|
||||
viewModel.save(to: modelContext)
|
||||
dismiss()
|
||||
}
|
||||
.disabled(!viewModel.canConfirm)
|
||||
}
|
||||
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button(role: .confirm) {
|
||||
viewModel.save(to: modelContext)
|
||||
dismiss()
|
||||
}
|
||||
.disabled(!viewModel.canConfirm)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,5 +121,7 @@ struct NewTaskView: View {
|
||||
"New todo task",
|
||||
traits: .emptyContainer
|
||||
) {
|
||||
NewTaskView()
|
||||
NavigationStack {
|
||||
NewTaskView()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user