Last active
March 27, 2025 21:04
-
-
Save bannzai/2b525de11add7e19299e5aa1e67aae64 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 SearchView: View { | |
@State var text: String | |
@FocusState var focused: Bool | |
@Async<[Item]> var async | |
var body: some View { | |
VStack { | |
TextField("Placeholder", text: $text) | |
.focused($focused) | |
.onSubmit { | |
async(fetch) | |
} | |
if text.isEmpty { | |
Text("Do Input") | |
} else if focused { | |
Text("Do submit") | |
} else { | |
// 違うViewに切り出しても良いかも | |
switch async.state { | |
case .success(let items): | |
if items.isEmpty { | |
Text("No Result") | |
} else { | |
List { | |
ForEach(items) { item in | |
SearchedView(item) | |
} | |
} | |
} | |
case .failure(let error): | |
ErrorPage(error: error) | |
case .loading: | |
ProgressView() | |
} | |
} | |
} | |
} | |
func fetch() async throws -> [Item] { | |
try await apiClient.call("search", args: ["query": text]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment