Component

public protocol Component

A component represents a small reusable piece of code of element to be rendered. This behaves as all elements of UIKit’s UITableView and UICollectionView, and can be easily support SwiftUI by used in conjunction with the View protocol.

Example for the simple component:

struct Label: Component {
    var text: String

    init(_ text: String) {
        self.text = text
    }

    func renderContent() -> UILabel {
        return UILabel()
    }

    func render(in content: UILabel) {
        content.text = text
    }
}

Example for use with SwiftUI:

extension Label: View {}

struct ContentView: View {
    var body: some View {
        VStack {
            Text("This is SwiftUI view")
            Label("This is Carbon component")
        }
    }
}
  • A type that represents a content to be render on the element of list UI.

    Declaration

    Swift

    associatedtype Content
  • reuseIdentifier Default implementation

    A string used to identify a element that is reusable. Default is the type name of self.

    Default Implementation

    A string used to identify a element that is reusable. Default is the type name of self.

    Declaration

    Swift

    var reuseIdentifier: String { get }
  • referenceSize(in:) Default implementation

    Returns the referencing size of content to render on the list UI.

    Note

    Only CGSize.height is used to determine the size of element in UITableView.

    Default Implementation

    Returns the referencing size of content to render on the list UI. Returns nil by default.

    Note

    Only CGSize.height is used to determine the size of element in UITableView.

    Declaration

    Swift

    func referenceSize(in bounds: CGRect) -> CGSize?

    Return Value

    The referencing size of content to render on the list UI. If returns nil, the element of list UI falls back to its default size like UITableView.rowHeight or UICollectionViewFlowLayout.itemSize.

  • shouldContentUpdate(with:) Default implementation

    Returns a Bool value indicating whether the content should be reloaded.

    Note

    Unlike Equatable, this doesn’t compare whether the two values exactly equal. It’s can be ignore property comparisons, if not expect to reload content.

    Default Implementation

    Returns a Bool value indicating whether the content should be reloaded. Default is false.

    Note

    Unlike Equatable, this doesn’t compare whether the two values exactly equal. It’s can be ignore property comparisons, if not expect to reload content.

    Declaration

    Swift

    func shouldContentUpdate(with next: Self) -> Bool

    Return Value

    A Bool value indicating whether the content should be reloaded.

  • shouldRender(next:in:) Default implementation

    Returns a Bool value indicating whether component should be render again.

    Default Implementation

    Returns a Bool value indicating whether component should be render again.

    Declaration

    Swift

    func shouldRender(next: Self, in content: Content) -> Bool

    Parameters

    next

    The next value to be compared to the receiver.

    content

    An instance of content laid out on the element.

    Return Value

    A Bool value indicating whether the component should be render again.

  • layout(content:in:) Default implementation

    Layout the content on top of element of the list UI.

    Note

    UIView and UIViewController are laid out with edge constraints by default.

    Default Implementation

    Layout the content on top of element of the list UI.

    Declaration

    Swift

    func layout(content: Content, in container: UIView)

    Parameters

    content

    An instance of content to be laid out on top of element.

    container

    A container view to layout content.

  • intrinsicContentSize(for:) Default implementation

    The natural size for the passed content.

    Default Implementation

    The natural size for the passed content.

    Declaration

    Swift

    func intrinsicContentSize(for content: Content) -> CGSize

    Return Value

    A CGSize value represents a natural size of the passed content.

  • contentWillDisplay(_:) Default implementation

    Invoked every time of before a component got into visible area.

    Default Implementation

    Invoked every time of before a component got into visible area.

    Declaration

    Swift

    func contentWillDisplay(_ content: Content)
  • contentDidEndDisplay(_:) Default implementation

    Invoked every time of after a component went out from visible area.

    Default Implementation

    Invoked every time of after a component went out from visible area.

    Declaration

    Swift

    func contentDidEndDisplay(_ content: Content)
  • identified(by:) Extension method

    Returns an identified component wrapping self and given id.

    Declaration

    Swift

    @inlinable
    func identified<ID>(by id: ID) -> IdentifiedComponentWrapper<ID, Self> where ID : Hashable

    Return Value

    An identified component wrapping self and given id.

  • identified(by:) Extension method

    Returns an identified component wrapping self and the id that accessed by given key path.

    Declaration

    Swift

    @inlinable
    func identified<ID>(by keyPath: KeyPath<Self, ID>) -> IdentifiedComponentWrapper<ID, Self> where ID : Hashable

    Return Value

    An identified component wrapping self and the id that accessed by given key path.