Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Dynamic generation of Fulcro Forms options
;;-> In fh ns
;; Can be used in a mutation to assoc-in new options
(defn input-options [[table-name id :as ident] field]
[table-name id ::f/form :elements/by-name field :input/options])
(defn input-default-value [[table-name id :as ident] field]
[table-name id ::f/form :elements/by-name field :input/default-value])
(defn options-generator [data->list-fn item->option-fn list->selected-fn]
(fn [data parent-selection]
(assert data)
(assert (seq data))
(let [list (data->list-fn parent-selection data)
_ (assert (seq list))
options (mapv item->option-fn list)
;_ (println "options" options)
selected (list->selected-fn list parent-selection)
;_ (println "selected" selected)
_ (assert selected (str "No default selected found from: " parent-selection))]
;(println "selected, options" selected options)
[selected options])))
;; Current field value and current options get changed together.
;; have one of these for every dropdown.
;; The inner function here alters state in a swap with thread first macro
(defn dropdown-rebuilder [field-whereabouts options-whereabouts default-value-whereabouts]
(fn [st field-ident options-value]
(-> st
(assoc-in field-whereabouts field-ident)
(assoc-in default-value-whereabouts field-ident)
(assoc-in options-whereabouts options-value))))
;;-> Also called in a mutation. What it returns is what `year-dropdown-rebuilder` needs
;; So selected goes to field-ident and options goes to options-value
(def years-options-generator (fh/options-generator
(fn [_ data]
(dhs/finished-range-of-years nil (:organisation/regular-periods-timespan data)))
#(f/option (keyword (str %)) (str %))
#(-> % first str keyword)))
;;-> In ident ns
(def request-form [:user-request/by-id p/USER_REQUEST_FORM])
;;-> In join ns
(def request-year (conj ident/request-form :request/year))
(def -request-form-input-options (partial fh/input-options ident/request-form))
(def request-year-options (-request-form-input-options :request/year))
(def -request-form-input-default-value (partial fh/input-default-value ident/request-form))
(def request-year-default-value (-request-form-input-default-value :request/year))
;;-> This fn called in a mutation
(def year-dropdown-rebuilder
join/request-year join/request-year-options join/request-year-default-value))

This comment has been minimized.

Copy link
Owner Author

@chrismurrph chrismurrph commented Nov 15, 2017

finished-range-of-years is missing because it is the actual data. Substitute [2017 2018 2019]. In reality that's where the dynamic (or static!) data comes from, so where you might be fetching the dynamic list of values from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.