DaisukeNagata / SwiftUI2.0

3 stars 0 forks source link

Outlinegroup #6

Open DaisukeNagata opened 4 years ago

DaisukeNagata commented 4 years ago

https://developer.apple.com/documentation/swiftui/outlinegroup

スクリーンショット 2020-06-28 16 16 34
DaisukeNagata commented 4 years ago
import SwiftUI

struct ContentView: View {

    let data =
        FileItem(name: "users", children: [FileItem(name: "user1234", children: [FileItem(name:"Photos")])])

    var body: some View {
        OutlineGroup(data, children: \.children) { item in
            Text ("\(item.description)")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct FileItem: Hashable, Identifiable, CustomStringConvertible {
    var id: Self { self }
    var name: String
    var children: [FileItem]? = nil
    var description: String {
        switch (children) {
        case nil:
            return "📄 \(name)"
        case .some(let children):
            return children.count > 0 ? "📂 \(name)" : "📁 \(name)"
        }
    }
}