From fee1864d7a7e1c58b8ce3d4788ae8ed5067305cf Mon Sep 17 00:00:00 2001 From: Robbie Date: Tue, 7 Sep 2021 11:46:10 +1000 Subject: [PATCH] [daterange] add :initial-display to datepicker --- src/re_com/datepicker.cljs | 7 ++++--- src/re_com/daterange.cljs | 5 ++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/re_com/datepicker.cljs b/src/re_com/datepicker.cljs index 0ecfe11a..fa1b98aa 100644 --- a/src/re_com/datepicker.cljs +++ b/src/re_com/datepicker.cljs @@ -505,6 +505,7 @@ [{:name :model :required false :type "satisfies DateTimeProtocol | r/atom" :validate-fn date-like? :description [:span "the selected date. If provided, should pass pred " [:code ":selectable-fn"] ". If not provided, (now->utc) will be used and the returned date will be a " [:code "goog.date.UtcDateTime"]]} {:name :on-change :required true :type "satisfies DateTimeProtocol -> nil" :validate-fn fn? :description [:span "called when a new selection is made. Returned type is the same as model (unless model is nil, in which case it will be " [:code "goog.date.UtcDateTime"] ")"]} {:name :disabled? :required false :default false :type "boolean | atom" :description "when true, the user can't select dates but can navigate"} + {:name :initial-display :required false :type "satisfies DateTimeProtocol | r/atom" :validate-fn date-like? :description "set the months shown when no model is selected, defaults to the current month"} {:name :selectable-fn :required false :default "(fn [date] true)" :type "function" :validate-fn fn? :description "This predicate function is called with one argument, the date. If it answers false, day will be shown disabled and can't be selected."} {:name :show-weeks? :required false :default false :type "boolean" :description "when true, week numbers are shown to the left"} {:name :show-today? :required false :default false :type "boolean" :description "when true, today's date is highlighted"} @@ -521,12 +522,12 @@ {:name :debug-as :required false :type "map" :validate-fn map? :description [:span "Used in dev builds to assist with debugging, when one component is used implement another component, and we want the implementation component to masquerade as the original component in debug output, such as component stacks. A map optionally containing keys" [:code ":component"] "and" [:code ":args"] "."]}])) (defn datepicker - [& {:keys [model] :as args}] + [& {:keys [model initial-display] :as args}] (or (validate-args-macro datepicker-args-desc args) (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 - display-month (reagent/atom (cljs-time/first-day-of-the-month (or @internal-model (now->utc))))] + display-month (reagent/atom (cljs-time/first-day-of-the-month (or @internal-model initial-display (now->utc))))] (fn datepicker-render [& {:keys [model on-change disabled? start-of-week hide-border? class style attr parts src debug-as] :or {start-of-week 6} ;; Default to Sunday @@ -540,7 +541,7 @@ (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 (cljs-time/first-day-of-the-month (or @internal-model (now->utc))))) + (reset! display-month (cljs-time/first-day-of-the-month (or @internal-model initial-display (now->utc))))) [main-div-with [:table (merge diff --git a/src/re_com/daterange.cljs b/src/re_com/daterange.cljs index 95c3e72a..10a594bb 100644 --- a/src/re_com/daterange.cljs +++ b/src/re_com/daterange.cljs @@ -4,8 +4,8 @@ (:require [reagent.core :as r] [re-com.config :refer [include-args-desc?]] - [re-com.box :refer [line border flex-child-style flex-flow-style]] - [re-com.core :as re-com :refer [at v-box h-box box gap popover-anchor-wrapper popover-content-wrapper]] + [re-com.box :refer [line border flex-child-style]] + [re-com.core :as re-com :refer [at v-box h-box box popover-anchor-wrapper popover-content-wrapper]] [re-com.validate :refer [date-like? css-style? html-attr? parts?] :refer-macros [validate-args-macro]] [re-com.util :refer [deref-or-value now->utc]] [cljs-time.format :refer [parse unparse formatter]] @@ -16,7 +16,6 @@ (defn- dec-month [date-time] (cljs-time/minus date-time (cljs-time/months 1))) (defn- plus-month [date-time] (cljs-time/plus date-time (cljs-time/months 1))) -(defn- dec-day [date-time] (cljs-time/minus date-time (cljs-time/days 1))) (defn- plus-day [date-time] (cljs-time/plus date-time (cljs-time/days 1))) (defn- dec-year [date-time] (cljs-time/minus date-time (cljs-time/years 1))) (defn- plus-year [date-time] (cljs-time/plus date-time (cljs-time/years 1)))