I am creating a page that displays, and then edits, renter info using om-boostrap. (I know Clojure, but am new to CLJS/Om/React/modern web development in general.) The UI and functionality between displaying and editing tenant info is similar -- both can use input fields; editing just needs the fields to be "text" instead of "static" and needs "Submit" and "Cancel" buttons.
The problem I face is that I can't figure out the React/Om way to change a component like that. The view is defined as follows:
(defcomponent view [{:keys [id] :as app} owner]
(render
[_]
(let [tenant (get @tenants id)]
(dom/div
(om/build header/header-view app)
(dom/div {:class "h3"} "View Tenant\t"
(utils/button {:on-click (fn []
(om/update-state! owner
#(assoc app :edit? true))
(om/refresh! owner))}
"Edit"))
(om/build tenant-info {:edit? (:edit? app)
:tenant {:id id
:name "Funny name"
:unit-addr "Funny addr"
:rent "Lots of rent"}})))))
I won't paste the entire tenant-view
function here, but it builds Bootstrap inputs for each tenant data field using om-bootstrap:
. . .
(let [input-type (if edit? "text" "static")]
(i/input {:ref "name-display"
:type input-type
:label "Tenant Name"
:label-classname "col-xs-2"
:wrapper-classname "col-xs-4"
:value (str name)})
. . .)
I've tried multiple approaches. I've posted my most recent, using the button's :on-click
function to modify the app state, setting the edit?
property to true
and prompting a re-render to make the inputs editable.
This doesn't work and I am not finding guidance in the React or Om documentation.
- What is the right way to render the same component differently? (In my case, the
view
function's input fields.) - What React or Om documentation is relevant?
UPDATE: I can get the inputs to be editable when I hard-code the edit?
flag to true
, so making the inputs editable is not the issue: the problem is toggling from static
to text
(and presumably vice versa).