adwaita-swift/Sources/Demo/ListDemo.swift
david-swift d0d7d2b6d3
Some checks are pending
Deploy Docs / publish (push) Waiting to run
SwiftLint / SwiftLint (push) Waiting to run
Use the new wrapModifier for simple modifiers
2026-02-03 00:07:26 +01:00

58 lines
1.4 KiB
Swift

//
// ListDemo.swift
// Adwaita
//
// Created by david-swift on 01.01.24.
//
// swiftlint:disable missing_docs
import Adwaita
import Foundation
struct ListDemo: View {
@State private var items: [Element] = []
@State private var selectedItem = ""
var view: Body {
HStack {
Button("Add Row") {
let element = Element(id: UUID().uuidString)
items.append(element)
selectedItem = element.id
}
Button("Delete Selected Row") {
let index = items.firstIndex { $0.id == selectedItem }
items = items.filter { $0.id != selectedItem }
selectedItem = items[safe: index]?.id ?? items[safe: index ?? 0 - 1]?.id ?? items.first?.id ?? ""
}
}
.modifyContent(VStack.self) { $0.linked() }
.padding()
.halign(.center)
if !items.isEmpty {
List(items, selection: $selectedItem) { item in
HStack {
Text("\(item.id)")
.hexpand()
}
.padding()
}
.boxedList()
.valign(.center)
.padding()
}
}
struct Element: Identifiable, CustomStringConvertible, Equatable {
var id: String
var description: String { id }
}
}
// swiftlint:enable missing_docs