|
3 | 3 | [clojure.string :as str]
|
4 | 4 | [re-com.util :as u :refer [px deref-or-value]]
|
5 | 5 | [reagent.core :as r]
|
| 6 | + [re-com.config :refer [debug? include-args-desc?]] |
| 7 | + [re-com.validate :refer [vector-atom? ifn-or-nil? map-atom? parts? part?]] |
6 | 8 | [re-com.theme :as theme]
|
7 | 9 | [re-com.box :as box]
|
8 | 10 | [re-com.buttons :as buttons]))
|
9 | 11 |
|
10 |
| -(def nested-grid-args-desc {}) |
11 | 12 | (def nested-grid-parts-desc {})
|
12 | 13 |
|
| 14 | +(def nested-grid-parts |
| 15 | + (when include-args-desc? |
| 16 | + (-> (map :name nested-grid-parts-desc) set))) |
| 17 | + |
| 18 | +(def nested-grid-args-desc |
| 19 | + (when include-args-desc? |
| 20 | + [{:name :cell |
| 21 | + :default "constantly nil" |
| 22 | + :type "part" |
| 23 | + :validate-fn part? |
| 24 | + :description |
| 25 | + [:span "String, hiccup or function. When a function, acceps keyword args " |
| 26 | + [:code ":column-path"] " and " [:code ":row-path"] |
| 27 | + ". Returns either a string or hiccup, which will appear within a single grid cell."]} |
| 28 | + {:name :column-tree |
| 29 | + :default "[]" |
| 30 | + :type "vector or seq of column-specs or column-trees" |
| 31 | + :validate-fn seq? |
| 32 | + :description |
| 33 | + [:span "Describes a nested arrangement of " [:code ":column-spec"] "s. " |
| 34 | + "A spec's path derives from its depth within the hierarchy of vectors or seqs. " |
| 35 | + " When a non-vector A precedes a vector B, then the items of B are children of A." |
| 36 | + " When a non-vector C follows B, then C is a sibling of A." |
| 37 | + " This nesting can be arbitrarily deep."]} |
| 38 | + {:name :row-tree |
| 39 | + :default "[]" |
| 40 | + :type "vector or seq of row-specs or row-trees" |
| 41 | + :validate-fn seq? |
| 42 | + :description |
| 43 | + [:span "Describes a nested arrangement of " [:code ":row-spec"] "s. " |
| 44 | + "A spec's path derives from its depth within the hierarchy of vectors or seqs. " |
| 45 | + " When a non-vector A precedes a vector B, then the items of B are children of A." |
| 46 | + " When a non-vector C follows B, then C is a sibling of A." |
| 47 | + " This nesting can be arbitrarily deep."]} |
| 48 | + {:name :column-header |
| 49 | + :type "part" |
| 50 | + :validate-fn part? |
| 51 | + :description |
| 52 | + [:span "A string, hiccup, or function of " [:code "{:keys [column-path]}"] "." |
| 53 | + " By default, returns the " [:code ":label"] ", " [:code ":id"] |
| 54 | + ", or else a string of the entire value of the last item in " |
| 55 | + [:code ":column-path"] "."]} |
| 56 | + {:name :row-header |
| 57 | + :type "part" |
| 58 | + :validate-fn part? |
| 59 | + :description |
| 60 | + [:span "A string, hiccup, or function of " [:code "{:keys [row-path]}"] "." |
| 61 | + " By default, returns the " [:code ":label"] ", " [:code ":id"] |
| 62 | + ", or else a string of the entire value of the last item in " |
| 63 | + [:code ":row-path"] "."]} |
| 64 | + {:name :cell-wrapper |
| 65 | + :type "part" |
| 66 | + :validate-fn part? |
| 67 | + :description |
| 68 | + [:span "A wrapper div, responsible for positioning one " [:code ":cell"] |
| 69 | + " within the css grid."]} |
| 70 | + {:name :column-header-wrapper |
| 71 | + :type "part" |
| 72 | + :validate-fn part? |
| 73 | + :description |
| 74 | + [:span "A wrapper div, responsible for positioning one " [:code ":column-header"] |
| 75 | + " within the css grid."]} |
| 76 | + {:name :row-header-wrapper |
| 77 | + :type "part" |
| 78 | + :validate-fn part? |
| 79 | + :description |
| 80 | + [:span "A wrapper div, responsible for positioning one " [:code ":row-header"] |
| 81 | + " within the css grid."]} |
| 82 | + {:name :header-spacer-wrapper |
| 83 | + :type "part" |
| 84 | + :validate-fn part? |
| 85 | + :description |
| 86 | + [:span "A wrapper responsible for positioning one " [:code ":header-spacer"] |
| 87 | + " within the css grid."]} |
| 88 | + {:name :show-branch-paths? |
| 89 | + :type "boolean" |
| 90 | + :default "false" |
| 91 | + :validate-fn boolean? |
| 92 | + :description |
| 93 | + [:span "When " [:code "true"] ", displays cells and headers for all " |
| 94 | + [:code ":column-paths"] " and " [:code ":row-paths"] ", not just the leaf paths."]} |
| 95 | + {:name :max-height |
| 96 | + :required false |
| 97 | + :type "string" |
| 98 | + :validate-fn string? |
| 99 | + :description "standard CSS max-height setting of the entire grid. Literally constrains the grid to the given width so that if the grid is taller than this it will add scrollbars. Ignored if value is larger than the combined width of all the rendered grid rows."} |
| 100 | + {:name :max-width |
| 101 | + :required false |
| 102 | + :type "string" |
| 103 | + :validate-fn string? |
| 104 | + :description |
| 105 | + [:span "standard CSS max-width setting of the entire grid. " |
| 106 | + "Literally constrains the grid to the given width so that " |
| 107 | + "if the grid is wider than this it will add scrollbars." |
| 108 | + " Ignored if value is larger than the combined width of all the rendered grid columns."]} |
| 109 | + {:name :column-header-height |
| 110 | + :default 30 |
| 111 | + :type "number" |
| 112 | + :validate-fn number? |
| 113 | + :description |
| 114 | + [:span "The default height that a column-header will use. " |
| 115 | + "Can be overridden by a " [:code ":height"] "key in the " |
| 116 | + [:code ":column-spec"] ", or by component-local state."]} |
| 117 | + {:name :column-width |
| 118 | + :default 30 |
| 119 | + :type "number" |
| 120 | + :validate-fn number? |
| 121 | + :description |
| 122 | + [:span "The default width that a column of grid cells will use. " |
| 123 | + "Can be overridden by a " [:code ":height"] "key in the " |
| 124 | + [:code ":column-spec"] ", or by component-local state."]} |
| 125 | + {:name :row-header-width |
| 126 | + :default 30 |
| 127 | + :type "number" |
| 128 | + :validate-fn number? |
| 129 | + :description |
| 130 | + [:span "The default width that a row-header will use. " |
| 131 | + "Can be overridden by a " [:code ":width"] "key in the " |
| 132 | + [:code ":row-spec"] ", or by component-local state."]} |
| 133 | + {:name :row-width |
| 134 | + :default 30 |
| 135 | + :type "number" |
| 136 | + :validate-fn number? |
| 137 | + :description |
| 138 | + [:span "The default width that a row of grid cells will use. " |
| 139 | + "Can be overridden by a " [:code ":width"] |
| 140 | + "key in the " [:code ":row-spec"] ", or by component-local state."]} |
| 141 | + {:name :show-export-button? |
| 142 | + :required false |
| 143 | + :default false |
| 144 | + :type "boolean" |
| 145 | + :description |
| 146 | + [:span "When non-nil, adds a hiccup of " [:code ":export-button-render"] |
| 147 | + " to the component tree."]} |
| 148 | + {:name :on-export |
| 149 | + :required false |
| 150 | + :type "function" |
| 151 | + :validate-fn ifn? |
| 152 | + :description |
| 153 | + [:span "Called whenever the export button is clicked." |
| 154 | + " Expects keyword arguments " |
| 155 | + [:code ":header-rows"] " and " [:code ":main-rows"] "."]} |
| 156 | + {:name :on-export-cell |
| 157 | + :required false |
| 158 | + :type "{:keys [row-path column-path]} -> string" |
| 159 | + :validate-fn ifn? |
| 160 | + :description |
| 161 | + [:span "Similar to " [:code ":cell"] ", but it should return a string value only." |
| 162 | + " After the export button is clicked, " [:code "nested-grid"] " maps " |
| 163 | + [:code ":on-export-cell"] "over any cells marked for export, passing the " |
| 164 | + "results to " [:code ":on-export"] " via the " [:code ":main-rows"] " prop."]} |
| 165 | + {:name :on-export-row-header |
| 166 | + :required false |
| 167 | + :type "{:keys [row-path]} -> string" |
| 168 | + :validate-fn ifn? |
| 169 | + :description |
| 170 | + [:span "Similar to " [:code ":row-header"] |
| 171 | + ", but it should return a string value only." |
| 172 | + " After the export button is clicked, " [:code "nested-grid"] " maps " |
| 173 | + [:code ":on-export-row-header"] "over any row headers marked for export, passing the " |
| 174 | + "results to " [:code ":on-export"] " via the " [:code ":main-rows"] " prop."]} |
| 175 | + {:name :on-export-column-header |
| 176 | + :required false |
| 177 | + :type "{:keys [column-path]} -> string" |
| 178 | + :validate-fn ifn? |
| 179 | + :description |
| 180 | + [:span "Similar to " [:code ":column-header"] |
| 181 | + ", but it should return a string value only." |
| 182 | + " After the export button is clicked, " [:code "nested-grid"] " maps " |
| 183 | + [:code ":on-export-column-header"] "over any cells marked for export, passing the " |
| 184 | + "results to " [:code ":on-export"] " via the " [:code ":header-rows"] " prop."]}])) |
| 185 | + |
13 | 186 | (defn descendant? [path-a path-b]
|
14 | 187 | (and (not= path-a path-b)
|
15 | 188 | (= path-a (vec (take (count path-a) path-b)))))
|
|
321 | 494 | on-resize-cell (fn [{:keys [distance path]}]
|
322 | 495 | (swap! column-state update-in [path :width]
|
323 | 496 | #(+ distance (or % (column-header-prop path :width column-width)))))]
|
324 |
| - (fn [& {:keys [column-tree row-tree cell |
325 |
| - cell-wrapper column-header-wrapper column-header row-header row-header-wrapper header-spacer-wrapper header-spacer |
| 497 | + (fn [& {:keys [column-tree row-tree |
| 498 | + cell column-header row-header header-spacer |
| 499 | + cell-wrapper column-header-wrapper row-header-wrapper header-spacer-wrapper |
326 | 500 | show-branch-paths?
|
327 | 501 | max-height column-width column-header-height row-header-width row-height
|
328 | 502 | show-export-button? on-export on-export-success on-export-failure
|
|
408 | 582 | (map add-padding)
|
409 | 583 | (map add-cell-values)
|
410 | 584 | (map render-row-headers))))
|
411 |
| - default-on-export (fn default-on-export [header-rows main-rows] |
| 585 | + default-on-export (fn default-on-export [{:keys [header-rows main-rows]}] |
412 | 586 | (->> (concat header-rows main-rows)
|
413 | 587 | (map u/tsv-line)
|
414 | 588 | str/join
|
|
430 | 604 | :on-export
|
431 | 605 | (fn [_] (let [header-rows (get-header-rows)
|
432 | 606 | main-rows (get-main-rows)]
|
433 |
| - ((or on-export default-on-export) header-rows main-rows) |
| 607 | + ((or on-export default-on-export) |
| 608 | + {:header-rows header-rows |
| 609 | + :main-rows main-rows}) |
434 | 610 | (when on-export-success (on-export-success header-rows main-rows))))}]
|
435 | 611 | grid-container
|
436 | 612 | [:div {:on-scroll #(do (reset! scroll-top (.-scrollTop (.-target %)))
|
|
0 commit comments