HomeiOS Developmentios - SwiftUI: ui slots and slot props?

ios – SwiftUI: ui slots and slot props?


I am beginning with Swift (coming from front-end) with check initiatives and have a query about view and look at builders

For reference: in a front-end framework Vue.js there’s a idea of slots and slot props https://vuejs.org/information/parts/slots.html#scoped-slots
Identical goes for Svelte https://svelte.dev/docs/special-elements#slot and in addition identical performance could be achieved in React

For instance I’ve some generic listing that has some primary structure and makes some logic (filtration, sorting and so on) inside and I wish to use it to move a listing and show listing gadgets nevertheless I need, and if I will not present a builder, there might be some default view that’s specified inside that listing

Is there any option to obtain such performance in SwiftUI/UIKit?

What I’ve tried to do is one thing like the subsequent code, I simplified it to point out the overall thought

import SwiftUI

struct ListTestView<T: Hashable, Content material: View>: View {
    non-public let gadgets: [T]
    non-public let listItemBuilder: (ListItemBuilderParams) -> Content material

    init(
        _ listing: [T],
        @ViewBuilder listItem: @escaping (_ params: ListItemBuilderParams) -> Content material
    ) {
        self.gadgets = listing
        self.listItemBuilder = listItem
    }
    
    var physique: some View {
        VStack {
            ForEach(filteredItems, id: .self) { merchandise in
                listItemBuilder(.init(item1: merchandise, item2: merchandise, item3: merchandise)) // ?? Textual content(String(merchandise))
            }
        }
    }
    
    non-public var filteredItems: [T] {
        // ...some filtration
        return gadgets
    }
    
    struct ListItemBuilderParams {
        let item1: T
        let item2: T
        let item3: T
        // ... and attainable extra
    }
}

struct ViewWithList: View {
    var physique: some View {
        ListTestView([1, 2, 3, 4]) { params in
            Textual content(String(params.item1))
            Button(String(params.item2)) {}
            Textual content(String(params.item3))
        }
    }
}

But it surely will not compile as a result of in ViewWithList I get an error Generic parameter 'Content material' couldn't be inferred

But it surely compiles if I move all of the params as plain arguments listing, with out a struct, however it might get actually messy as a result of there is no such thing as a option to omit some particular arguments and I’ve to listing all of them in closure

import SwiftUI

struct ListTestView<T: Hashable, Content material: View>: View {
    non-public let gadgets: [T]
    non-public let listItemBuilder: (T, T, T) -> Content material

    init(
        _ listing: [T],
        @ViewBuilder listItem: @escaping (T, T, T) -> Content material
    ) {
        self.gadgets = listing
        self.listItemBuilder = listItem
    }

    var physique: some View {
        VStack {
            ForEach(filteredItems, id: .self) { merchandise in
                listItemBuilder(merchandise, merchandise, merchandise) // ?? Textual content(String(merchandise))
            }
        }
    }

    non-public var filteredItems: [T] {
        // ...some filtration
        return gadgets
    }
}

struct ViewWithList: View {
    var physique: some View {
        ListTestView([1, 2, 3, 4]) { item1, item2, item3 in
            Textual content(String(item1))
            Button(String(item2)) {}
            Textual content(String(item3))
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments