Add button command, fix list selection
This commit is contained in:
@ -48,15 +48,13 @@ struct PostListView: View {
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Binding
|
||||
private var selectedPost: Post?
|
||||
@EnvironmentObject
|
||||
private var selection: SelectedContent
|
||||
|
||||
@State
|
||||
private var searchString = ""
|
||||
|
||||
init(selectedPost: Binding<Post?>) {
|
||||
self._selectedPost = selectedPost
|
||||
}
|
||||
init() { }
|
||||
|
||||
private var filteredPosts: [Post] {
|
||||
guard !searchString.isEmpty else {
|
||||
@ -74,13 +72,20 @@ struct PostListView: View {
|
||||
TextField("", text: $searchString, prompt: Text("Search"))
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.horizontal, 8)
|
||||
List(filteredAndSortedPosts, selection: $selectedPost) { post in
|
||||
List(filteredAndSortedPosts) { post in
|
||||
PostListItem(post: post)
|
||||
.tag(post)
|
||||
.listRowBackground(RoundedRectangle(cornerRadius: 5)
|
||||
.fill(selection.post == post ? Color.blue : Color.clear)
|
||||
.padding(.horizontal, 10)
|
||||
)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
selection.post = post
|
||||
}
|
||||
}
|
||||
}.onAppear {
|
||||
if selectedPost == nil {
|
||||
selectedPost = content.posts.first
|
||||
if selection.post == nil, let first = content.posts.first {
|
||||
selection.post = first
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user