-
Notifications
You must be signed in to change notification settings - Fork 414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for collections in :class property #154
Conversation
I think it could be useful to also add support for conditional classes, kind of like what react devs had in mind for inline styles https://github.com/reactjs/react-future/blob/master/04%20-%20Layout/04%20-%20Inline%20Styles.md#using-styles. So remove falsey values to enable Also wouldn't mind to have those conditional inline styles implemented heh |
@whodidthis Good idea. Added filtering step. |
(defn stringify-class [{:keys [class] :as props}] | ||
(if (coll? class) | ||
(->> class | ||
(filter identity) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why filter identity here? isn't this a no-op, or at best the equiv of seq?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, ok. Nevermind. I understand now - I read the comments.
You can already do this with clojure's built-in functionality. This might be best suited by writing a helper method. Adding this code adds at best an additional if statement around every single class in our apps. What if we don't want this in our code? At worst, the filter adds quite a bit of code to these collections, and ends in some magic. At best, another conditional. The more magic we have in our code, the more bloated and less understandable it becomes. One would think this comment isn't necessarily relevant for this patch, but it's actually the little things that matter. Clojure, and Reagent are awesome precisely because they are tiny, comprehensive and understandable. Precisely because of the magic they don't have. |
@JulianLeviston I agree that adding feature increasing code complexity just because it may prove useful is not a good idea. But in this actual case, maybe the added complexity is balanced out by how often it's found useful? It would be probably best if others had their say on this before deciding what to do about the PR. |
@zoldar Sure. It's only my two cents, afterall :) |
(defn stringify-class [{:keys [class] :as props}]
(if (coll? class)
(assoc props :class (string/join " " class))
props)) |
@Deraen Good points. Also, |
|
|
Right, Although i guess i would still go with filtering out falses, Also using |
@whodidthis @Deraen I also think that it will be best and least surprising if all falsey values are removed. Brought back the filtering by identity step. |
I'm in favour of merging this. Update |
Yes, I agree: it might be convenient to have. Let's do this after 0.5.1. (There should probably be some tests for this, though...) @mike-thompson-day8 Yes, we should definitely have some visible documentation of especially syntax. Ideally that should be on the website – as that is most visible, I guess? |
Added tests for the feature. |
Finally got my s**t together and learned how to use Git properly (hopefully). Now everything is squahsed into a single, nice commit. |
👍 |
2 similar comments
+1 |
+1 |
src/reagent/impl/template.cljs
Outdated
@@ -73,13 +73,23 @@ | |||
class)))) | |||
p)) | |||
|
|||
(defn stringify-class [{:keys [class] :as props}] | |||
(if (coll? class) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be sequential?
instead of coll?
. Maps and sets also implement ICollection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have used coll?
in order to support sets actually (which is not covered by tests as I see it now, bummer). Do you think it's impractical/unnecessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supporting sets is good, so just leave it that way.
I guess the remark meant to ask how to deal with maps. Maybe throwing an
error would be best, but probably "undefined behavior" is okay too - I
mean, who would do that anyways :-P
Adrian Gruntkowski [email protected] schrieb am Mi., 28. Okt. 2015
10:19:
In src/reagent/impl/template.cljs
#154 (comment)
:@@ -73,13 +73,23 @@
class))))
p))+(defn stringify-class [{:keys [class] :as props}]
- (if (coll? class)
I have used coll? in order to support sets actually (which is not covered
by tests as I see it now, bummer). Do you think it's
impractical/unnecessary?—
Reply to this email directly or view it on GitHub
https://github.com/reagent-project/reagent/pull/154/files#r43231780.
@holmsand are you okay with this being merged? It has tests but no docs. |
It would be good to add tests for sets if that usage is going to be supported. |
Added test for sets and a short mention of the feature in EDIT: Oops, hit some conflicts. Will try to sort it out later. |
@zoldar Sorry about that – I've just merged a whole lot of code all over the place :) I think this patch is a good idea btw, so we should merge it. |
Maps where mentioned in some comment. I can think a way how maps could be useful for setting classes: ;; active? true, odd? false
:class [(if active? "active") (if odd? "odd")]
:class {:active active? :odd odd?}
;; => "active"
;; active? true, odd? true
:class [(if active? "active") (if odd? "odd")]
:class {:active active? :odd odd?}
;; => "active odd" So map keys with truthy values would be used as class names, and if needed converted to strings. |
I like that way of using maps - it's like Angular's ng-class directive and On Fri, Oct 30, 2015 at 12:33 PM Juho Teperi [email protected]
|
@Deraen I'm not a fan of the map version. Too much magic, for not enough benefit. By magic I mean: an experienced clojurescript developer is not going to immediately understand what's going on without some coaching. There'd have to be a HUGE upside to warrant the overhead of puzzling people. Remember this is a relatively minor, non essential, convenience feature. |
I'm with @mike-thompson-day8 on the map suggestion. That's a bit too much magic for me too. P.S.: Resolved the conflicts. |
@holmsand are you still okay with this being merged? |
In principle yes. But first I'd like to make sure that it doesn't have any adverse effects on performance. I think it can be made quite fast, but I'd like to (finally) get a 0.6.0-alpha out the door first, this week I sincerely hope... |
Ping! Any idea when this will be released? |
@holmsand ping |
README.md
Outdated
@@ -55,6 +55,12 @@ can be written as: | |||
[:div>p>b "Nested Element"] | |||
``` | |||
|
|||
The `:class` attribute accepts collections in addition to plain string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: improve language.
"The :class attribute can accept either a collection or a string"
Applied feedback and rebased. |
Looks good. I was waiting for #313 before merging some PRs, as I broke CI config at some point, but as this has good tests, I'm sure this will work after I merge new test runner. |
Fantastic to see this land - a much requested feature! |
I can't seem to make this work - it separates the classes with commas not spaces so they aren't recognised. Any idea what I'm doing wrong? |
@MichaelFHScott Can you share a quick snippet of code so we can see what you're doing? |
Thanks for the very quick response. It was my stupid mistake - I had accidentally reverted to Reagent 0.7.0. |
That's my first stab at adding support for passing a collection for class property, as mentioned in #138. Is that the right place to implement it?