Fork me on GitHub
#reagent
<
2015-07-27
>
ulsa19:07:24

I want to add the :disabled attribute only if the value in an atom is empty. In Hoplon, I can conditionally set attributes like this:

(button
  :do-attr (cell= {:disabled (string/blank? cell-with-contents)})
  "Get stuff")
How do I do this in Reagent?

jonas19:07:47

@ulsa you should be able to do more or less the same thing with reagent: [:button {:disabled (string/blank? @some-atom)} “Get stuff”]

ulsa19:07:04

Tried that. Can't seem to get the dynamicity working.

jonas19:07:26

can you gist the code?

ulsa19:07:23

I want the attribute if true, otherwise I don't want it at all.

ulsa19:07:29

But I end up with disabled="" regardless

colin.yates19:07:57

@ulsa I find reagent mostly behaves the same for {:a nil} and {} in which case I write code like {:my-attr (when pred? “some value”)}

colin.yates19:07:49

if not then (cond-> {} pred? (assoc :my-attr “some value”))

jonas19:07:50

you can also do something like (when-not (string/blank? …) {:disabled true})

jonas19:07:40

[:button (when-not (string/blank? …) {:disabled true}) “Get stuff”]

ulsa19:07:58

I have more attrs though

colin.yates19:07:09

(cond-> {} pred? (assoc :my-attr “some value”))

jonas19:07:35

yes, something like @colin.yates is suggesting would work well then

jonas19:07:00

or (merge my-attrs (when-not (string/blank? …) {:disabled true}))

ulsa19:07:20

will this code be run whenever the dereferenced atom changes?

jonas19:07:46

it should yes, if you’re using reagents version of atoms

ulsa19:07:17

everything else works, just not disabled

ulsa19:07:07

the atom is updated when I type into the textfield, but disabled is always = ""

jonas19:07:35

that’s very strange

ulsa19:07:03

my fault, used reagent atom in other files, but not this one

jonas19:07:15

problem hopefully solved then simple_smile

ulsa19:07:06

will clean up and report what works

ulsa19:07:32

cond-> works

ulsa19:07:42

regular boolean pred works; it goes away on false

ulsa19:07:39

when works too; it goes away on nil

ulsa19:07:43

thanks guys

ulsa19:07:37

btw, having a good time diving into reagent for the first time

jonas19:07:10

yeah, reagent is really easy to work with

colin.yates19:07:32

btw - you might want to take a look at re-frame if you like reagent

ulsa19:07:32

ok, will do that; I bootstrapped using luminus +cljs

ulsa19:07:27

so reaction is like cell=?

mikethompson21:07:07

@ulsa yes, very similar intent.

mikethompson21:07:14

BTW, depending on your requirements, as well as re-frame, you might like re-com

ulsa21:07:00

is there anything like hoplon's :do-toggle?

ulsa21:07:35

I suppose {:style {:display (if pred "block" "none")}} can do it.