calcite-input-date-picker.entry.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-8ece2564.js';
  7. import { T as TEXT, d as dateToISO, a as dateFromISO, b as dateFromRange, g as getLocaleData, s as sameDate, p as parseDateString, i as inRange } from './resources-f170fbe8.js';
  8. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label-50132b90.js';
  9. import { s as submitForm, c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form-bca481e1.js';
  10. import { d as defaultMenuPlacement, f as filterComputedPlacements, u as updatePopper, C as CSS, c as createPopper, p as popperMenuComputedPlacements } from './popper-0fd1fd89.js';
  11. import { u as updateHostInteraction } from './interactive-cb5bf285.js';
  12. import { t as toAriaBoolean } from './dom-da697a3f.js';
  13. import './locale-7ce850e5.js';
  14. import './number-237e74a9.js';
  15. import './guid-b4461004.js';
  16. const inputDatePickerCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:host{--calcite-icon-size:1rem;--calcite-spacing-eighth:0.125rem;--calcite-spacing-quarter:0.25rem;--calcite-spacing-half:0.5rem;--calcite-spacing-three-quarters:0.75rem;--calcite-spacing:1rem;--calcite-spacing-plus-quarter:1.25rem;--calcite-spacing-plus-half:1.5rem;--calcite-spacing-double:2rem;--calcite-menu-min-width:10rem;--calcite-header-min-height:3rem;--calcite-footer-min-height:3rem}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:relative;display:inline-block;width:100%;overflow:visible;vertical-align:top;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;-webkit-box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.calendar-picker-wrapper{position:static;width:100%;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;-webkit-box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);-webkit-transform:translate3d(0, 0, 0);transform:translate3d(0, 0, 0)}.input-wrapper{position:relative}:host([range]) .input-container{display:-ms-flexbox;display:flex}:host([range]) .input-wrapper{-ms-flex:1 1 auto;flex:1 1 auto}:host([range]) .horizontal-arrow-container{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;border-width:1px;border-left-width:0px;border-right-width:0px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-background);padding-top:0px;padding-bottom:0px;padding-left:0.25rem;padding-right:0.25rem}:host([range][layout=vertical]) .input-wrapper{width:100%}:host([range][layout=vertical]) .input-container{-ms-flex-direction:column;flex-direction:column;-ms-flex-align:start;align-items:flex-start}:host([range][layout=vertical]) .calendar-picker-wrapper--end{-webkit-transform:translate3d(0, 0, 0);transform:translate3d(0, 0, 0)}:host([range][layout=vertical]) .vertical-arrow-container{top:1.5rem;position:absolute;left:0px;z-index:10;margin-left:1px;margin-right:1px;background-color:var(--calcite-ui-foreground-1);padding-left:0.625rem;padding-right:0.625rem}:host([scale=s][range]:not([layout=vertical])) .calendar-picker-wrapper{width:216px}:host([scale=m][range]:not([layout=vertical])) .calendar-picker-wrapper{width:286px}:host([scale=l][range]:not([layout=vertical])) .calendar-picker-wrapper{width:398px}.menu-container{display:block;position:absolute;z-index:900;-webkit-transform:scale(0);transform:scale(0);visibility:hidden;pointer-events:none}.menu-container .calcite-popper-anim{position:relative;z-index:1;-webkit-transition:var(--calcite-popper-transition);transition:var(--calcite-popper-transition);visibility:hidden;-webkit-transition-property:visibility, opacity, -webkit-transform;transition-property:visibility, opacity, -webkit-transform;transition-property:transform, visibility, opacity;transition-property:transform, visibility, opacity, -webkit-transform;opacity:0;-webkit-box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);border-radius:0.25rem}.menu-container[data-popper-placement^=bottom] .calcite-popper-anim{-webkit-transform:translateY(-5px);transform:translateY(-5px)}.menu-container[data-popper-placement^=top] .calcite-popper-anim{-webkit-transform:translateY(5px);transform:translateY(5px)}.menu-container[data-popper-placement^=left] .calcite-popper-anim{-webkit-transform:translateX(5px);transform:translateX(5px)}.menu-container[data-popper-placement^=right] .calcite-popper-anim{-webkit-transform:translateX(-5px);transform:translateX(-5px)}.menu-container[data-popper-placement] .calcite-popper-anim--active{opacity:1;visibility:visible;-webkit-transform:translate(0);transform:translate(0)}:host([active]) .menu-container{pointer-events:initial;visibility:visible}.menu-container--active .menu-container .calcite-popper-anim{position:relative;z-index:1;-webkit-transition:var(--calcite-popper-transition);transition:var(--calcite-popper-transition);visibility:hidden;-webkit-transition-property:visibility, opacity, -webkit-transform;transition-property:visibility, opacity, -webkit-transform;transition-property:transform, visibility, opacity;transition-property:transform, visibility, opacity, -webkit-transform;opacity:0;-webkit-box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);border-radius:0.25rem}.menu-container--active .menu-container[data-popper-placement^=bottom] .calcite-popper-anim{-webkit-transform:translateY(-5px);transform:translateY(-5px)}.menu-container--active .menu-container[data-popper-placement^=top] .calcite-popper-anim{-webkit-transform:translateY(5px);transform:translateY(5px)}.menu-container--active .menu-container[data-popper-placement^=left] .calcite-popper-anim{-webkit-transform:translateX(5px);transform:translateX(5px)}.menu-container--active .menu-container[data-popper-placement^=right] .calcite-popper-anim{-webkit-transform:translateX(-5px);transform:translateX(-5px)}.menu-container--active .menu-container[data-popper-placement] .calcite-popper-anim--active{opacity:1;visibility:visible;-webkit-transform:translate(0);transform:translate(0)}.input .calcite-input__wrapper{margin-top:0px}:host([range][layout=vertical][scale=m]) .vertical-arrow-container{top:1.5rem;padding-left:0.75rem}:host([range][layout=vertical][scale=m]) .vertical-arrow-container calcite-icon{height:0.75rem;width:0.75rem;min-width:0px}:host([range][layout=vertical][scale=l]) .vertical-arrow-container{top:2.25rem;padding-left:0.875rem;padding-right:0.875rem}:host([range][layout=vertical][active]) .vertical-arrow-container{display:none}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  17. const InputDatePicker = class {
  18. constructor(hostRef) {
  19. registerInstance(this, hostRef);
  20. this.calciteDatePickerChange = createEvent(this, "calciteDatePickerChange", 7);
  21. this.calciteDatePickerRangeChange = createEvent(this, "calciteDatePickerRangeChange", 7);
  22. this.calciteInputDatePickerChange = createEvent(this, "calciteInputDatePickerChange", 7);
  23. this.calciteInputDatePickerOpen = createEvent(this, "calciteInputDatePickerOpen", 7);
  24. this.calciteInputDatePickerClose = createEvent(this, "calciteInputDatePickerClose", 7);
  25. //--------------------------------------------------------------------------
  26. //
  27. // Public Properties
  28. //
  29. //--------------------------------------------------------------------------
  30. /**
  31. * When false, the component won't be interactive.
  32. */
  33. this.disabled = false;
  34. /** Expand or collapse when calendar does not have input */
  35. this.active = false;
  36. /** Localized string for "previous month" (used for aria label)
  37. * @default "Previous month"
  38. */
  39. this.intlPrevMonth = TEXT.prevMonth;
  40. /** Localized string for "next month" (used for aria label)
  41. * @default "Next month"
  42. */
  43. this.intlNextMonth = TEXT.nextMonth;
  44. /** Localized string for "year" (used for aria label)
  45. * @default "Year"
  46. */
  47. this.intlYear = TEXT.year;
  48. /** BCP 47 language tag for desired language and country format */
  49. this.locale = document.documentElement.lang || "en";
  50. /** specify the scale of the date picker */
  51. this.scale = "m";
  52. /**
  53. * Determines where the date-picker component will be positioned relative to the input.
  54. * @default "bottom-leading"
  55. */
  56. this.placement = defaultMenuPlacement;
  57. /** Range mode activation */
  58. this.range = false;
  59. /**
  60. * When true, makes the component required for form-submission.
  61. *
  62. * @internal
  63. */
  64. this.required = false;
  65. /** Describes the type of positioning to use for the overlaid content. If your element is in a fixed container, use the 'fixed' value. */
  66. this.overlayPositioning = "absolute";
  67. /** Disables the default behaviour on the third click of narrowing or extending the range and instead starts a new range. */
  68. this.proximitySelectionDisabled = false;
  69. /** Layout */
  70. this.layout = "horizontal";
  71. this.focusedInput = "start";
  72. this.activeTransitionProp = "opacity";
  73. //--------------------------------------------------------------------------
  74. //
  75. // Private Methods
  76. //
  77. //--------------------------------------------------------------------------
  78. this.setFilteredPlacements = () => {
  79. const { el, flipPlacements } = this;
  80. this.filteredFlipPlacements = flipPlacements
  81. ? filterComputedPlacements(flipPlacements, el)
  82. : null;
  83. };
  84. this.transitionEnd = (event) => {
  85. if (event.propertyName === this.activeTransitionProp) {
  86. this.active
  87. ? this.calciteInputDatePickerOpen.emit()
  88. : this.calciteInputDatePickerClose.emit();
  89. }
  90. };
  91. this.setStartInput = (el) => {
  92. this.startInput = el;
  93. };
  94. this.setEndInput = (el) => {
  95. this.endInput = el;
  96. };
  97. this.deactivate = () => {
  98. this.active = false;
  99. };
  100. this.keyDownHandler = (event) => {
  101. if (event.key === "Enter" && !event.defaultPrevented) {
  102. submitForm(this);
  103. }
  104. };
  105. this.keyUpHandler = (e) => {
  106. if (e.key === "Escape") {
  107. this.active = false;
  108. }
  109. };
  110. this.inputBlur = (e) => {
  111. this.blur(e.detail);
  112. };
  113. this.startInputFocus = () => {
  114. this.active = true;
  115. this.focusedInput = "start";
  116. };
  117. this.endInputFocus = () => {
  118. this.active = true;
  119. this.focusedInput = "end";
  120. };
  121. this.inputInput = (e) => {
  122. this.input(e.detail.value);
  123. };
  124. this.setMenuEl = (el) => {
  125. if (el) {
  126. this.menuEl = el;
  127. this.createPopper();
  128. }
  129. };
  130. this.setStartWrapper = (el) => {
  131. this.startWrapper = el;
  132. this.setReferenceEl();
  133. };
  134. this.setEndWrapper = (el) => {
  135. this.endWrapper = el;
  136. this.setReferenceEl();
  137. };
  138. /**
  139. * Event handler for when the selected date changes
  140. */
  141. this.handleDateChange = (event) => {
  142. if (this.range) {
  143. return;
  144. }
  145. this.value = dateToISO(event.detail);
  146. };
  147. this.handleDateRangeChange = (event) => {
  148. var _a, _b;
  149. if (!this.range || !event.detail) {
  150. return;
  151. }
  152. const { startDate, endDate } = event.detail;
  153. this.start = dateToISO(startDate);
  154. this.end = dateToISO(endDate);
  155. this.value = [this.start, this.end];
  156. if (this.shouldFocusRangeEnd()) {
  157. (_a = this.endInput) === null || _a === void 0 ? void 0 : _a.setFocus();
  158. }
  159. else if (this.shouldFocusRangeStart()) {
  160. (_b = this.startInput) === null || _b === void 0 ? void 0 : _b.setFocus();
  161. }
  162. };
  163. }
  164. handleDisabledChange(value) {
  165. if (!value) {
  166. this.active = false;
  167. }
  168. }
  169. valueHandler(value) {
  170. if (Array.isArray(value)) {
  171. this.valueAsDate = value.map((v) => dateFromISO(v));
  172. this.start = value[0];
  173. this.end = value[1];
  174. }
  175. else if (value) {
  176. this.valueAsDate = dateFromISO(value);
  177. this.start = "";
  178. this.end = "";
  179. }
  180. else {
  181. this.valueAsDate = undefined;
  182. this.start = undefined;
  183. this.end = undefined;
  184. }
  185. }
  186. flipPlacementsHandler() {
  187. this.setFilteredPlacements();
  188. }
  189. onMinChanged(min) {
  190. if (min) {
  191. this.minAsDate = dateFromISO(min);
  192. }
  193. }
  194. onMaxChanged(max) {
  195. if (max) {
  196. this.maxAsDate = dateFromISO(max);
  197. }
  198. }
  199. activeHandler() {
  200. if (!this.disabled) {
  201. this.reposition();
  202. return;
  203. }
  204. this.active = false;
  205. }
  206. //--------------------------------------------------------------------------
  207. //
  208. // Event Listeners
  209. //
  210. //--------------------------------------------------------------------------
  211. handleDateOrRangeChange() {
  212. this.calciteInputDatePickerChange.emit();
  213. }
  214. calciteDaySelectHandler() {
  215. if (this.shouldFocusRangeStart() || this.shouldFocusRangeEnd()) {
  216. return;
  217. }
  218. this.active = false;
  219. }
  220. // --------------------------------------------------------------------------
  221. //
  222. // Public Methods
  223. //
  224. // --------------------------------------------------------------------------
  225. /** Updates the position of the component. */
  226. async setFocus() {
  227. var _a;
  228. (_a = this.startInput) === null || _a === void 0 ? void 0 : _a.setFocus();
  229. }
  230. /** Updates the position of the component. */
  231. async reposition() {
  232. const { placement, popper, menuEl } = this;
  233. const modifiers = this.getModifiers();
  234. popper
  235. ? await updatePopper({
  236. el: menuEl,
  237. modifiers,
  238. placement,
  239. popper
  240. })
  241. : this.createPopper();
  242. }
  243. // --------------------------------------------------------------------------
  244. //
  245. // Lifecycle
  246. //
  247. // --------------------------------------------------------------------------
  248. connectedCallback() {
  249. if (Array.isArray(this.value)) {
  250. this.valueAsDate = this.value.map((v) => dateFromISO(v));
  251. this.start = this.value[0];
  252. this.end = this.value[1];
  253. }
  254. else if (this.value) {
  255. this.valueAsDate = dateFromISO(this.value);
  256. this.start = "";
  257. this.end = "";
  258. }
  259. if (this.start) {
  260. this.startAsDate = dateFromISO(this.start);
  261. }
  262. if (this.end) {
  263. this.endAsDate = dateFromISO(this.end);
  264. }
  265. if (this.min) {
  266. this.minAsDate = dateFromISO(this.min);
  267. }
  268. if (this.max) {
  269. this.maxAsDate = dateFromISO(this.max);
  270. }
  271. this.createPopper();
  272. connectLabel(this);
  273. connectForm(this);
  274. this.setFilteredPlacements();
  275. }
  276. async componentWillLoad() {
  277. await this.loadLocaleData();
  278. this.onMinChanged(this.min);
  279. this.onMaxChanged(this.max);
  280. }
  281. disconnectedCallback() {
  282. this.destroyPopper();
  283. disconnectLabel(this);
  284. disconnectForm(this);
  285. }
  286. componentDidRender() {
  287. updateHostInteraction(this);
  288. }
  289. render() {
  290. var _a, _b;
  291. const { disabled } = this;
  292. const date = dateFromRange(this.range ? this.startAsDate : this.valueAsDate, this.minAsDate, this.maxAsDate);
  293. const endDate = this.range
  294. ? dateFromRange(this.endAsDate, this.minAsDate, this.maxAsDate)
  295. : null;
  296. const formattedEndDate = endDate ? endDate.toLocaleDateString(this.locale) : "";
  297. const formattedDate = date ? date.toLocaleDateString(this.locale) : "";
  298. return (h(Host, { onBlur: this.deactivate, onKeyDown: this.keyDownHandler, onKeyUp: this.keyUpHandler, role: "application" }, this.localeData && (h("div", { "aria-expanded": toAriaBoolean(this.active), class: "input-container", role: "application" }, h("div", { class: "input-wrapper", ref: this.setStartWrapper }, h("calcite-input", { class: `input ${this.layout === "vertical" && this.range ? `no-bottom-border` : ``}`, disabled: disabled, icon: "calendar", label: getLabelText(this), "number-button-type": "none", onCalciteInputBlur: this.inputBlur, onCalciteInputFocus: this.startInputFocus, onCalciteInputInput: this.inputInput, placeholder: (_a = this.localeData) === null || _a === void 0 ? void 0 : _a.placeholder, ref: this.setStartInput, scale: this.scale, type: "text", value: formattedDate })), h("div", { "aria-hidden": toAriaBoolean(!this.active), class: {
  299. "menu-container": true,
  300. "menu-container--active": this.active
  301. }, ref: this.setMenuEl }, h("div", { class: {
  302. ["calendar-picker-wrapper"]: true,
  303. ["calendar-picker-wrapper--end"]: this.focusedInput === "end",
  304. [CSS.animation]: true,
  305. [CSS.animationActive]: this.active
  306. }, onTransitionEnd: this.transitionEnd }, h("calcite-date-picker", { activeRange: this.focusedInput, endAsDate: this.endAsDate, headingLevel: this.headingLevel, intlNextMonth: this.intlNextMonth, intlPrevMonth: this.intlPrevMonth, intlYear: this.intlYear, locale: this.locale, max: this.max, maxAsDate: this.maxAsDate, min: this.min, minAsDate: this.minAsDate, onCalciteDatePickerChange: this.handleDateChange, onCalciteDatePickerRangeChange: this.handleDateRangeChange, proximitySelectionDisabled: this.proximitySelectionDisabled, range: this.range, scale: this.scale, startAsDate: this.startAsDate, tabIndex: 0, valueAsDate: this.valueAsDate }))), this.range && this.layout === "horizontal" && (h("div", { class: "horizontal-arrow-container" }, h("calcite-icon", { flipRtl: true, icon: "arrow-right", scale: "s" }))), this.range && this.layout === "vertical" && this.scale !== "s" && (h("div", { class: "vertical-arrow-container" }, h("calcite-icon", { icon: "arrow-down", scale: "s" }))), this.range && (h("div", { class: "input-wrapper", ref: this.setEndWrapper }, h("calcite-input", { class: {
  307. input: true,
  308. "border-top-color-one": this.layout === "vertical" && this.range
  309. }, disabled: disabled, icon: "calendar", "number-button-type": "none", onCalciteInputBlur: this.inputBlur, onCalciteInputFocus: this.endInputFocus, onCalciteInputInput: this.inputInput, placeholder: (_b = this.localeData) === null || _b === void 0 ? void 0 : _b.placeholder, ref: this.setEndInput, scale: this.scale, type: "text", value: formattedEndDate }))))), h(HiddenFormInputSlot, { component: this })));
  310. }
  311. setReferenceEl() {
  312. const { focusedInput, layout, endWrapper, startWrapper } = this;
  313. this.referenceEl =
  314. focusedInput === "end" || layout === "vertical"
  315. ? endWrapper || startWrapper
  316. : startWrapper || endWrapper;
  317. this.createPopper();
  318. }
  319. onLabelClick() {
  320. this.setFocus();
  321. }
  322. getModifiers() {
  323. const flipModifier = {
  324. name: "flip",
  325. enabled: true
  326. };
  327. flipModifier.options = {
  328. fallbackPlacements: this.filteredFlipPlacements || popperMenuComputedPlacements
  329. };
  330. const eventListenerModifier = {
  331. name: "eventListeners",
  332. enabled: this.active
  333. };
  334. return [flipModifier, eventListenerModifier];
  335. }
  336. createPopper() {
  337. this.destroyPopper();
  338. const { menuEl, placement, referenceEl, overlayPositioning } = this;
  339. if (!menuEl || !referenceEl) {
  340. return;
  341. }
  342. const modifiers = this.getModifiers();
  343. this.popper = createPopper({
  344. el: menuEl,
  345. modifiers,
  346. overlayPositioning,
  347. placement,
  348. referenceEl
  349. });
  350. }
  351. destroyPopper() {
  352. const { popper } = this;
  353. if (popper) {
  354. popper.destroy();
  355. }
  356. this.popper = null;
  357. }
  358. startWatcher(start) {
  359. this.startAsDate = dateFromISO(start);
  360. }
  361. endWatcher(end) {
  362. this.endAsDate = dateFromISO(end);
  363. }
  364. async loadLocaleData() {
  365. const { locale } = this;
  366. this.localeData = await getLocaleData(locale);
  367. }
  368. clearCurrentValue() {
  369. if (!this.range) {
  370. if (typeof this.value === "string" && this.value) {
  371. this.calciteDatePickerChange.emit();
  372. }
  373. this.value = "";
  374. return;
  375. }
  376. const { focusedInput } = this;
  377. if (focusedInput === "start") {
  378. if (this.start) {
  379. this.calciteDatePickerRangeChange.emit();
  380. }
  381. this.value = Array.isArray(this.value) ? ["", this.value[1] || ""] : [""];
  382. this.start = undefined;
  383. }
  384. else if (focusedInput === "end") {
  385. if (this.end) {
  386. this.calciteDatePickerRangeChange.emit();
  387. }
  388. this.value = Array.isArray(this.value) ? [this.value[0] || "", ""] : ["", ""];
  389. this.end = undefined;
  390. }
  391. }
  392. /**
  393. * If inputted string is a valid date, update value/active
  394. */
  395. input(value) {
  396. const date = this.getDateFromInput(value);
  397. if (!date) {
  398. this.clearCurrentValue();
  399. return;
  400. }
  401. if (!this.range) {
  402. this.value = dateToISO(date);
  403. this.calciteDatePickerChange.emit(date);
  404. return;
  405. }
  406. const { focusedInput } = this;
  407. if (focusedInput === "start") {
  408. if (!this.startAsDate || !sameDate(date, this.startAsDate)) {
  409. const startDateISO = dateToISO(date);
  410. this.value = Array.isArray(this.value)
  411. ? [startDateISO, this.value[1] || ""]
  412. : [startDateISO];
  413. this.start = startDateISO;
  414. this.calciteDatePickerRangeChange.emit({
  415. startDate: date,
  416. endDate: this.endAsDate
  417. });
  418. }
  419. }
  420. else if (focusedInput === "end") {
  421. if (!this.endAsDate || !sameDate(date, this.endAsDate)) {
  422. const endDateISO = dateToISO(date);
  423. this.value = Array.isArray(this.value)
  424. ? [this.value[0] || "", endDateISO]
  425. : ["", endDateISO];
  426. this.end = endDateISO;
  427. this.calciteDatePickerRangeChange.emit({
  428. startDate: this.startAsDate,
  429. endDate: date
  430. });
  431. }
  432. }
  433. }
  434. /**
  435. * Clean up invalid date from input on blur
  436. */
  437. blur(target) {
  438. const { locale, focusedInput, endAsDate, range, startAsDate, valueAsDate } = this;
  439. const date = this.getDateFromInput(target.value);
  440. if (!date) {
  441. if (!range && valueAsDate) {
  442. target.value = Array.isArray(valueAsDate)
  443. ? valueAsDate[focusedInput === "end" ? 1 : 0].toLocaleDateString(locale)
  444. : valueAsDate.toLocaleDateString(locale);
  445. }
  446. else if (focusedInput === "start" && startAsDate) {
  447. target.value = startAsDate.toLocaleDateString(locale);
  448. }
  449. else if (focusedInput === "end" && endAsDate) {
  450. target.value = endAsDate.toLocaleDateString(locale);
  451. }
  452. }
  453. }
  454. shouldFocusRangeStart() {
  455. return !!(this.endAsDate &&
  456. !this.startAsDate &&
  457. this.focusedInput === "end" &&
  458. this.startInput);
  459. }
  460. shouldFocusRangeEnd() {
  461. return !!(this.startAsDate &&
  462. !this.endAsDate &&
  463. this.focusedInput === "start" &&
  464. this.endInput);
  465. }
  466. /**
  467. * Find a date from input string
  468. * return false if date is invalid, or out of range
  469. */
  470. getDateFromInput(value) {
  471. if (!this.localeData) {
  472. return false;
  473. }
  474. const { separator } = this.localeData;
  475. const { day, month, year } = parseDateString(value, this.localeData);
  476. const validDay = day > 0;
  477. const validMonth = month > -1;
  478. const date = new Date(year, month, day);
  479. date.setFullYear(year);
  480. const validDate = !isNaN(date.getTime());
  481. const validLength = value.split(separator).filter((c) => c).length > 2;
  482. const validYear = year.toString().length > 0;
  483. if (validDay &&
  484. validMonth &&
  485. validDate &&
  486. validLength &&
  487. validYear &&
  488. inRange(date, this.min, this.max)) {
  489. return date;
  490. }
  491. return false;
  492. }
  493. get el() { return getElement(this); }
  494. static get watchers() { return {
  495. "disabled": ["handleDisabledChange"],
  496. "value": ["valueHandler"],
  497. "flipPlacements": ["flipPlacementsHandler"],
  498. "min": ["onMinChanged"],
  499. "max": ["onMaxChanged"],
  500. "active": ["activeHandler"],
  501. "layout": ["setReferenceEl"],
  502. "focusedInput": ["setReferenceEl"],
  503. "start": ["startWatcher"],
  504. "end": ["endWatcher"],
  505. "locale": ["loadLocaleData"]
  506. }; }
  507. };
  508. InputDatePicker.style = inputDatePickerCss;
  509. export { InputDatePicker as calcite_input_date_picker };