import SwiftUI import UIKit extension View { /// Puts a Done button above the keyboard. /// /// The numeric fields (Settings → Average fuel economy and Typical /// fill-up, and the Worth the Drive fill-up box in the station list) use /// `.decimalPad`, which has NO Return key. Without this there is no way to /// dismiss the keyboard once such a field is tapped, and a /// `TextField(value:format:)` only commits the typed value when editing /// ends — so the number never stuck either. /// /// Apply ONCE per screen, to the list or form containing the fields: the /// keyboard toolbar belongs to the focused field, so a single attachment /// covers every field inside it. Attaching it to each field would show /// several Done buttons at once. /// /// - Parameter action: clears the screen's focus state. Prefer this where a /// `@FocusState` exists; the default resigns the current first responder, /// which works for fields inside subviews that have no focus binding. func keyboardDoneButton(action: (() -> Void)? = nil) -> some View { toolbar { ToolbarItemGroup(placement: .keyboard) { Spacer() Button("Done") { if let action { action() } else { UIApplication.shared.sendAction( #selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil ) } } } } } }