Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. (ns myapp.scratch-ws
  2. (:require
  3.  
  4. ["@material-ui/core" :as mui]
  5. [nubank.workspaces.core :as ws]
  6. [nubank.workspaces.card-types.fulcro :as ct.fulcro]
  7. [nubank.workspaces.lib.fulcro-portal :as f.portal]
  8.  
  9. [fulcro.client.localized-dom :as dom]
  10. [fulcro.client.mutations :as fm :refer [defmutation]]
  11. [fulcro.client.cards :refer [defcard-fulcro]]
  12. [fulcro.client.primitives :as fp :refer [defsc]]
  13.  
  14. ))
  15.  
  16.  
  17. ;; --- utilities ---
  18. (defn factory-apply
  19. [class]
  20. (fn [props & children]
  21. (apply js/React.createElement
  22. class
  23. (clj->js props)
  24. children)))
  25.  
  26.  
  27. ;; --- mutations ---
  28. (defmutation create-new-box
  29. "Creates a new box"
  30. [{:keys [db/id size title] :as params}]
  31. (action [{:keys [state]}]
  32. (swap! state assoc-in [:box/by-id id] {:size size :title title})))
  33.  
  34.  
  35.  
  36. (def TextField (factory-apply mui/TextField))
  37.  
  38. (defsc BoxComponent [this
  39. {:keys [:db/id title size]}
  40. computed
  41. {:keys [boxSize boxDiv] :as css-name-map}]
  42. {
  43. :ident [:box/by-id :db/id]
  44. :query [:db/id :title :size]
  45. :css [[:.boxSize {:width "50px"}]
  46. [:.boxDiv {:border "3px solid red"}]]}
  47. (let [[h w] size
  48. h-val (atom h)
  49. w-val (atom w)]
  50. (dom/div
  51. (TextField {:select true
  52. :label "Height"
  53. :className boxSize
  54. :value @h-val
  55. :onChange #(reset! h-val (.. % -target -value))
  56. }
  57. (map #(dom/option {:key % :value %} (str %)) (range 1 20))
  58. )
  59. (TextField {:select true
  60. :label "Width"
  61. :className boxSize
  62. :value @w-val
  63. :onChange #(reset! w-val (.. % -target -value))
  64. }
  65. (map #(dom/option {:key % :value %} (str %)) (range 1 20))
  66. )
  67. (dom/button
  68. {:onClick #(fp/transact! this `[(create-new-box {:db/id ~1
  69. :title "Box"
  70. :size ~[h w]})])}
  71. "Create New Box")
  72.  
  73. (dom/div :.boxDiv {:style {:width (str (* 10 w) "px")
  74. :height (str (* 10 h) "px")}})
  75. )))
  76.  
  77. (def ui-box (fp/factory BoxComponent {:key-fn :db/id}))
  78.  
  79. (defsc RootComponent [this {:keys [box]} computed {:keys [textField] :as css-name-map}]
  80. {:initial-state (fn [p] {:box {:title "Box" :db/id 1 :size [3 3]}})
  81. :query [{:box (fp/get-query BoxComponent)}]
  82. :css-include [BoxComponent]}
  83. (ui-box box))
  84.  
  85. (ws/defcard myapp-scratch-card
  86. (ct.fulcro/fulcro-card
  87. {
  88. ; if true generates a root. Useful for embedding normalized components with a placeholder root.
  89. ::f.portal/wrap-root? false
  90. ::f.portal/root RootComponent}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement