ra1028 / Carbon

🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.

Home Page:https://ra1028.github.io/Carbon

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need the ability to add event handling to the Component

BarredEwe opened this issue · comments

Checklist

Description

I would like to be able to add event handling directly to the component.

Example:
struct HelloMessage: Component {
    ...
}

HelloMessage().on(.click) { ... }.cellNode
HelloMessage().on(.viewWillAppear) { ... }.cellNode

or

HelloMessage().cellNode.on(.click) { ... }
HelloMessage().cellNode.on(.viewWillAppear) { ... }

Motivation and Context

Allows you to make ViewController cleaner.

@BarredEwe

A Component cannot have a state because it's a protocol.
It can be implemented by composition, but I don't like it because it complicates the API.

I have added this feature for myself. If anyone is interested, I can post a merge request with this feature.
https://github.com/BarredEwe/Carbon/tree/actions

Example:


struct TestView: Component {
        ...
}

class TestViewContent: UIView {
        ...
    @IBAction func testButtonTapped(_ sender: UIButton) {
        // Sending actions
        ComponentAction(.custom(.buttonTapped), sender: self).invoke()
    }
}

TestView(...)
        .on(.custom(.buttonTapped)) { action in /*Do something*/ }
        .cellNode

Congrats 🎉
I'll close this issue, but if you have further suggestions, please re-open it.