lilactown / helix

A simple, easy to use library for React development in ClojureScript.
Eclipse Public License 2.0
631 stars 52 forks source link

Spread props should work with a raw JS obj #41

Closed lilactown closed 4 years ago

lilactown commented 4 years ago

Sometimes you get a JS object and want to forward it as props to a component.

What we'd like to do:

(defnc my-component []
  ;; `some-lib/Foo` is an external lib that uses a common pattern:
  ;; pass it a function-as-children which it then passes props to as a JS object
  ($ some-lib/Foo
   (fn [foo-props]
     (d/div {& foo-props} "I'm so foo-y!"))))

Currently, this requires converting or wrapping foo-props so that it is a CLJS map-alike, so that spread props works.

(defnc my-component []
  ($ some-lib/Foo
   (fn [foo-props]
     ;; wrap it in a bean, so that it can then be converted into a JS obj again... :sad:
     (d/div {& (cljs-bean.core/bean foo-props)} "I'm so foo-y!"))))

To support writing spread props like in the first snippet, spread props should check to see if it's a map-alike and if not, fall back to merging it in by Object.keys.

lilactown commented 4 years ago

Fixed with https://github.com/Lokeh/helix/pull/46