mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 02:04:07 +00:00
Add tap on search to focus search field on iOS
This commit is contained in:
29
Shared/Search/FocusableSearchTextField.swift
Normal file
29
Shared/Search/FocusableSearchTextField.swift
Normal file
@@ -0,0 +1,29 @@
|
||||
import Introspect
|
||||
import Repeat
|
||||
import SwiftUI
|
||||
|
||||
@available(iOS 15.0, macOS 12, *)
|
||||
struct FocusableSearchTextField: View {
|
||||
@ObservedObject private var state = SearchModel.shared
|
||||
|
||||
#if os(iOS)
|
||||
@State private var textField: UITextField?
|
||||
#elseif os(macOS)
|
||||
@State private var textField: NSTextField?
|
||||
#endif
|
||||
|
||||
var body: some View {
|
||||
SearchTextField()
|
||||
#if os(iOS)
|
||||
.introspectTextField { field in
|
||||
textField = field
|
||||
}
|
||||
.onChange(of: state.focused) { newValue in
|
||||
if newValue, let textField, !textField.isFirstResponder {
|
||||
textField.becomeFirstResponder()
|
||||
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
@@ -95,7 +95,11 @@ struct SearchView: View {
|
||||
filtersMenu
|
||||
}
|
||||
|
||||
SearchTextField()
|
||||
if #available(macOS 12, *) {
|
||||
FocusableSearchTextField()
|
||||
} else {
|
||||
SearchTextField()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -175,7 +179,11 @@ struct SearchView: View {
|
||||
searchMenu
|
||||
}
|
||||
ToolbarItem(placement: .principal) {
|
||||
SearchTextField()
|
||||
if #available(iOS 15, *) {
|
||||
FocusableSearchTextField()
|
||||
} else {
|
||||
SearchTextField()
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
|
Reference in New Issue
Block a user