Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Last active October 1, 2020 21:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pesterhazy/10b4a01908e7632825a2321c06130989 to your computer and use it in GitHub Desktop.
Save pesterhazy/10b4a01908e7632825a2321c06130989 to your computer and use it in GitHub Desktop.
react-virtualized used from reagent
;; [cljsjs/react-virtualized "7.11.8-1" :exclusions [cljsjs/react]]
(ns react-virtualized.example
(:require [cljsjs.react-virtualized]
[clojure.string]
goog.date.DateTime
[reagent.core :as r]))
(defn fmt-value [v]
(cond
(map? v)
(->> v
(map (comp fmt-value val))
(clojure.string/join ", "))
(sequential? v)
(->> v
(map fmt-value)
(clojure.string/join ", "))
(instance? js/Date v)
(.toUTCIsoString (goog.date.DateTime. v))
:else
(str v)))
(defn sort-by-col [items f rev?]
(if f
(->> items
(map (juxt (comp fmt-value f) identity))
(sort-by first
(if rev?
(comp - compare)
compare))
(mapv second))
(vec items)))
(defn ui []
(let [!st (r/atom nil)]
(fn [{:keys [ks on-row-click show-fn]} items]
(let [{:keys [sortBy sortDirection]} @!st
sorted-items @(r/track sort-by-col
items
(some-> sortBy keyword)
(= "DESC" sortDirection))]
[:> js/ReactVirtualized.AutoSizer
(fn [m]
(r/as-element (into
[:> js/ReactVirtualized.FlexTable
(cond-> {:height 800
:className "inspector"
:width (aget m "width")
:headerHeight 70
:rowHeight 30
:rowCount (count sorted-items)
:rowClassName (fn [m]
(when (odd? (aget m "index"))
"table-odd"))
:rowGetter (fn [m]
(get sorted-items (aget m "index")))
:sort #(reset! !st (js->clj % :keywordize-keys true))
:sortBy (:sortBy @!st)
:sortDirection (:sortDirection @!st)}
on-row-click
(assoc :on-row-click (comp on-row-click sorted-items :index mapify)))]
(map (fn [k]
[:> js/ReactVirtualized.FlexColumn
{:key (name k)
:label (name k)
:dataKey (kw->str k)
:cellDataGetter (fn [m]
(fmt-value (get (aget m "rowData") (keyword (aget m "dataKey")))))
:width 200}])
(filter (or show-fn (constantly true)) ks)))))]))))
@mattford63
Copy link

Hi, from where do you get mapify and kw->str from?

@pesterhazy
Copy link
Author

@mattford63, can't remember, sorry - it's been a while

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment