Created
March 7, 2021 15:27
-
-
Save prafullakumar/3420354db6ed81d7180d8320527f546e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SearchBar: View { | |
@Binding var text: String | |
@Binding var isEditing: Bool | |
init(text: Binding<String>, isEditing: Binding<Bool>) { | |
self._text = text | |
self._isEditing = isEditing | |
} | |
var body: some View { | |
HStack { | |
TextField("Search Country...", text: $text) | |
.padding(7.5) | |
.padding(.horizontal, 20) | |
.background(Color(.systemGray6)) | |
.cornerRadius(7.5) | |
.overlay( | |
HStack { | |
Image(systemName: "magnifyingglass") | |
.foregroundColor(.gray) | |
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) | |
.padding(.leading, 7.5) | |
if isEditing && text.count != 0 { | |
Button(action: { | |
self.text = "" | |
}) { | |
Image(systemName: "multiply.circle.fill") | |
.foregroundColor(.gray) | |
.padding(.trailing, 7.5) | |
} | |
} | |
} | |
) | |
.padding(.horizontal, 10) | |
.onTapGesture { | |
withAnimation { | |
self.isEditing = true | |
} | |
} | |
if isEditing { | |
Button(action: { | |
withAnimation { | |
self.isEditing = false | |
self.text = "" | |
} | |
// Dismiss the keyboard | |
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) | |
}) { | |
Text("Cancel") | |
} | |
.padding(.trailing, 10) | |
.animation(.linear(duration: 0.25)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment