Listable icon indicating copy to clipboard operation
Listable copied to clipboard

Improve using elements in lists

Open kyleve opened this issue 3 years ago • 2 comments

This PR changes the recommended way to convert Blueprint Elements into Items or HeaderFooters. The old way was to either manually define a BlueprintItemContent or BlueprintHeaderFooterContent, which worked great for more complex needs, or to use the ElementItem(...) or ElementHeaderFooter(...) funcs, which were meant for simpler use cases. However, the latter was still pretty wordy, and people would still regularly mess it up.

Here, we instead add extensions to Element to support both Item and HeaderFooter use cases: You can just directly return an Element anywhere an ItemContent or HeaderFooterContent was previously required (caveat: Where assignment happens directly; such as Section.header, you still need to do section.header = element.headerFooter()).

This PR also moves isEquivalent(to:) into its own interface (LayoutEquivalent), so (a) the docs can be shared, and (b) so elements can conform to it and be used in either the item or header/footer case.

TL;DR

Now that this PR exists, you can now do:

List {
   Section("some-id") {
       SomeElement()
       AnotherElement()
       SomeItemContent()
       MyCoolElement().listItem(id: "my-id") {
          $0.selectionStyle = .tappable
       }
   } header: {
      MyHeaderElement()
   } footer: {
      MyFooterElement().listHeaderFooter {
         ...
      }
   }
}

Checklist

Please do the following before merging:

  • [x] Ensure any public-facing changes are reflected in the changelog. Include them in the Main section.

kyleve avatar Jul 25 '22 03:07 kyleve

I agree with the core aspects of the direction here - the builders, the reversal of construction (from Item containing Element to Element modified to Item), and even some of the more controversial sugar that you've included. Thanks for backing this up with so much testing.

nononoah avatar Sep 12 '22 15:09 nononoah