Skip to content

Commit

Permalink
Cleanup of input fix, along with a workspace card to test it out.
Browse files Browse the repository at this point in the history
  • Loading branch information
awkay committed May 24, 2024
1 parent b2383f1 commit 7a0084b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
33 changes: 18 additions & 15 deletions src/main/com/fulcrologic/fulcro/dom/inputs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,36 @@
(cljs.core/this-as this
(let [props (gobj/get props "fulcro$value")
{:keys [value]} props
initial-state {:oldPropValue value
:on-change (fn [evt]
(let [{:keys [value onChange]} (comp/props this)
nsv (evt/target-value evt)
nv (string->model nsv)]
(comp/set-state! this {:stringValue nsv
:oldPropValue value
:cacheValue nv
:cacheInvalid? false})
(when (and onChange (not= value nv))
(onChange nv))))
:stringValue (model->string value)
initial-state {:oldPropValue value
:on-change (fn [evt]
(let [{:keys [value onChange]} (comp/props this)
nsv (evt/target-value evt)
nv (string->model nsv)]
(comp/set-state! this {:stringValue nsv
:oldPropValue value
:cacheValue nv
:cacheInvalid? false})
(when (and onChange (not= value nv))
(onChange nv))))
:stringValue (model->string value)
:cacheInvalid? true}]
(set! (.-state this) (cljs.core/js-obj "fulcro$state" initial-state)))
nil)))]
(comp/configure-component! cls kw
{:getDerivedStateFromProps
(fn [latest-props state]
(let [{:keys [value]} latest-props
{:keys [oldPropValue stringValue cacheValue cacheInvalid?]} state
ignorePropValue? (or (= oldPropValue value) (when-not cacheInvalid? (= value cacheValue)))
{:keys [oldPropValue cacheValue cacheInvalid? stringValue]} state
ignorePropValue? (or (= oldPropValue value)
(and (not cacheInvalid?) (= value cacheValue)))
cacheInvalid? (or cacheInvalid? ignorePropValue?)
stringValue (cond-> (if ignorePropValue?
stringValue
(model->string value))
string-filter string-filter)
new-derived-state (merge state {:stringValue stringValue :oldPropValue value :cacheInvalid? cacheInvalid?})]
new-derived-state (merge state {:stringValue stringValue
:oldPropValue value
:cacheInvalid? cacheInvalid?})]
#js {"fulcro$state" new-derived-state}))
:render
(fn [this]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
(ct.fulcro/fulcro-card
{::ct.fulcro/wrap-root? false
::ct.fulcro/root Root
::ct.fulcro/app {:optimized-render! ior/render!}}))
::ct.fulcro/app {:optimized-render! ior/render!}}))
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(ns com.fulcrologic.fulcro.issues.pr552-string-buffered-input-cards
(:require
[com.fulcrologic.fulcro.algorithms.merge :as merge]
[com.fulcrologic.fulcro.components :as comp :refer [defsc]]
[com.fulcrologic.fulcro.dom :refer [b button div p h3 li p ul]]
[com.fulcrologic.fulcro.dom.inputs :refer [StringBufferedInput]]
[com.fulcrologic.fulcro.mutations :as m :refer [defmutation]]
[com.fulcrologic.fulcro.rendering.ident-optimized-render :as ior]
[nubank.workspaces.card-types.fulcro3 :as ct.fulcro]
[nubank.workspaces.core :as ws]))

(def MyInput (StringBufferedInput ::MyInput {:model->string identity
:string->model identity}))

(def ui-input (comp/factory MyInput))

(defsc Item
[this {:item/keys [name]}]
{:ident :item/id
:query [:item/id :item/name :ui/clicked?]
:initial-state {:item/id 1 :item/name "Item 1" :ui/clicked? false}}
(let [v (last (for [n (range 1000000)]
(+ n (* 2 n))))]
(div
(div (str v))
(ui-input {:value name
:onChange (fn [v] (m/set-string! this :item/name :value v))})
(button {:onClick (fn [] (m/set-string! this :item/name :value "Bar"))}
"Force to Bar")
(button {:onClick (fn [] (m/set-string! this :item/name :value "Foo"))}
"Force to Foo"))))

(def ui-item (comp/factory Item {:keyfn :item/id}))

(defsc Root [_this {:root/keys [item]}]
{:query [{:root/item (comp/get-query Item)}]
:initial-state {:root/item {}}}

(div
(ui-item item)))

(ws/defcard string-buffered-input-card
(ct.fulcro/fulcro-card
{::ct.fulcro/wrap-root? false
::ct.fulcro/root Root}))

0 comments on commit 7a0084b

Please sign in to comment.