ra1028 / Carbon

🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.
https://ra1028.github.io/Carbon
Apache License 2.0
1.33k stars 97 forks source link

Need the ability to add event handling to the Component #75

Closed BarredEwe closed 4 years ago

BarredEwe commented 4 years ago

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.

ra1028 commented 4 years ago

@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.

BarredEwe commented 4 years ago

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
ra1028 commented 4 years ago

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