Skip to content

Commit

Permalink
Remove unnecessary code and tidy up in datepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregg8 committed Dec 19, 2018
1 parent 45f6d7e commit 0c87fd7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
26 changes: 6 additions & 20 deletions src/re_com/datepicker.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
; {:pre [(sunday? date)]}
(let [table-row (if (:show-weeks? attributes) [:tr (week-td date)] [:tr])
row-dates (map #(inc-date date %) (range 7))
today (when (:show-today? attributes) (:today attributes))]
today (when (:show-today? attributes) (now->utc))]
(into table-row (map #(table-td % focus-month selected today attributes disabled? on-change) row-dates))))


Expand All @@ -188,18 +188,8 @@
[attributes]
(let [selectable-fn (if (-> attributes :selectable-fn fn?)
(:selectable-fn attributes)
(fn [date] true))]
(merge attributes {:selectable-fn selectable-fn
:today (now->utc)})))


(defn now-today
[date-type]
(cond
(= date-type js/goog.date.Date) (today)
(= date-type js/goog.date.UtcDateTime) (now->utc)
(nil? date-type) (now->utc) ;; Default for when dat was not nil/unspecified
:else (throw (js/Error. "Invalid date type - must be goog.date.UtcDateTime/Date or nil"))))
(constantly true))]
(merge attributes {:selectable-fn selectable-fn})))


(def datepicker-args-desc
Expand All @@ -222,24 +212,20 @@
{:pre [(validate-args-macro datepicker-args-desc args "datepicker")]}
(let [external-model (reagent/atom (deref-or-value model)) ;; Set model type in stone on creation of this datepicker instance
internal-model (reagent/atom @external-model) ;; Holds the last known external value of model, to detect external model changes
date-type (if @internal-model ;; Create a new atom from the model to be used internally
(type @internal-model)
js/goog.date.UtcDateTime) ;; Default to UtcDateTime if model is nil (for backward compatibility)
display-month (reagent/atom (first-day-of-the-month (or @internal-model (now-today date-type))))]
display-month (reagent/atom (first-day-of-the-month (or @internal-model (now->utc))))]
(fn datepicker-component
[& {:keys [model on-change disabled? start-of-week hide-border? class style attr]
:or {start-of-week 6} ;; Default to Sunday
:as args}]
{:pre [(validate-args-macro datepicker-args-desc args "datepicker")]}
(let [latest-ext-model (deref-or-value model)
disabled? (deref-or-value disabled?)
props-with-defaults (merge args {:start-of-week start-of-week
#_:date-type #_date-type}) ;; Uncomment if required down the line
props-with-defaults (merge args {:start-of-week start-of-week})
configuration (configure props-with-defaults)]
(when (not= @external-model latest-ext-model) ;; Has model changed externally?
(reset! external-model latest-ext-model)
(reset! internal-model latest-ext-model)
(reset! display-month (first-day-of-the-month (or @internal-model (now-today date-type)))))
(reset! display-month (first-day-of-the-month (or @internal-model (now->utc)))))
[main-div-with
[:table {:class "table-condensed"}
[table-thead display-month configuration]
Expand Down
2 changes: 1 addition & 1 deletion src/re_com/validate.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns re-com.validate
(:require
[cljs-time.core :as time.core]
[cljs-time.core :as time.core]
[clojure.set :refer [superset?]]
[re-com.util :refer [deref-or-value-peek]]
[reagent.core :as reagent]
Expand Down
4 changes: 2 additions & 2 deletions src/re_demo/datepicker.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
:show-today? @show-today?
:show-weeks? @show-weeks?
:selectable-fn selectable-pred
:on-change #(reset! model1 %)]
:on-change #(do #_(js/console.log "model1:" %) (reset! model1 %))]
[label :style label-style :label (str "selected: " (date->string @model1))]
[h-box
:gap "6px"
Expand Down Expand Up @@ -154,7 +154,7 @@
:show-weeks? @show-weeks?
:selectable-fn selectable-pred
:disabled? disabled?
:on-change #(reset! model2 %)]
:on-change #(do #_(js/console.log "model2" %) (reset! model2 %))]
[label :style label-style :label (str "selected: " (date->string @model2))]]]]]
enabled-days
disabled?
Expand Down

0 comments on commit 0c87fd7

Please sign in to comment.