calcite-input-date-picker.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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.97
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Host, Build } from '@stencil/core/internal/client/index.js';
  7. import { T as TEXT, g as getValueAsDateRange, a as getLocaleData, d as defineCustomElement$8 } from './date-picker.js';
  8. import { d as dateToISO, a as dateFromISO, s as setEndOfDay, b as dateFromRange, c as sameDate, p as parseDateString, i as inRange } from './date.js';
  9. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label2.js';
  10. import { s as submitForm, c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
  11. import { d as defaultMenuPlacement, f as filterComputedPlacements, c as connectFloatingUI, u as updateAfterClose, r as reposition, a as disconnectFloatingUI, F as FloatingCSS } from './floating-ui.js';
  12. import { u as updateHostInteraction } from './interactive.js';
  13. import { t as toAriaBoolean } from './dom.js';
  14. import { c as connectOpenCloseComponent, d as disconnectOpenCloseComponent } from './openCloseComponent.js';
  15. import { n as numberStringFormatter, c as connectLocalized, d as disconnectLocalized } from './locale.js';
  16. import { n as numberKeys } from './key.js';
  17. import { d as defineCustomElement$7 } from './date-picker-day.js';
  18. import { d as defineCustomElement$6 } from './date-picker-month.js';
  19. import { d as defineCustomElement$5 } from './date-picker-month-header.js';
  20. import { d as defineCustomElement$4 } from './icon.js';
  21. import { d as defineCustomElement$3 } from './input.js';
  22. import { d as defineCustomElement$2 } from './progress.js';
  23. const CSS = {
  24. menu: "menu-container",
  25. menuActive: "menu-container--active"
  26. };
  27. const inputDatePickerCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity: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;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}: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-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host{position:relative;display:inline-block;inline-size:100%;overflow:visible;vertical-align:top;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}:host .menu-container .calcite-floating-ui-anim{position:relative;transition:var(--calcite-floating-ui-transition);visibility:hidden;transition-property:transform, visibility, opacity;opacity:0;box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);z-index:1;border-radius:0.25rem}:host .menu-container[data-placement^=bottom] .calcite-floating-ui-anim{transform:translateY(-5px)}:host .menu-container[data-placement^=top] .calcite-floating-ui-anim{transform:translateY(5px)}:host .menu-container[data-placement^=left] .calcite-floating-ui-anim{transform:translateX(5px)}:host .menu-container[data-placement^=right] .calcite-floating-ui-anim{transform:translateX(-5px)}:host .menu-container[data-placement] .calcite-floating-ui-anim--active{opacity:1;visibility:visible;transform:translate(0)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.calendar-picker-wrapper{position:static;inline-size:100%;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);transform:translate3d(0, 0, 0)}.input-wrapper{position:relative}:host([range]) .input-container{display:flex}:host([range]) .input-wrapper{flex:1 1 auto}:host([range]) .horizontal-arrow-container{display:flex;align-items:center;border-width:1px;border-inline-start-width:0px;border-inline-end-width:0px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-background);padding-block:0px;padding-inline:0.25rem}:host([range][layout=vertical]) .input-wrapper{inline-size:100%}:host([range][layout=vertical]) .input-container{flex-direction:column;align-items:flex-start}:host([range][layout=vertical]) .calendar-picker-wrapper--end{transform:translate3d(0, 0, 0)}:host([range][layout=vertical]) .vertical-arrow-container{inset-block-start:1.5rem;position:absolute;z-index:1;margin-inline:1px;background-color:var(--calcite-ui-foreground-1);padding-inline:0.625rem;inset-inline-start:0}:host([scale=s][range]:not([layout=vertical])) .calendar-picker-wrapper{inline-size:216px}:host([scale=m][range]:not([layout=vertical])) .calendar-picker-wrapper{inline-size:286px}:host([scale=l][range]:not([layout=vertical])) .calendar-picker-wrapper{inline-size:398px}.menu-container{display:block;position:absolute;z-index:900;inline-size:0;block-size:0;overflow:hidden;pointer-events:none;visibility:hidden}.menu-container .calcite-floating-ui-anim{position:relative;transition:var(--calcite-floating-ui-transition);visibility:hidden;transition-property:transform, visibility, opacity;opacity:0;box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);z-index:1;border-radius:0.25rem}.menu-container[data-placement^=bottom] .calcite-floating-ui-anim{transform:translateY(-5px)}.menu-container[data-placement^=top] .calcite-floating-ui-anim{transform:translateY(5px)}.menu-container[data-placement^=left] .calcite-floating-ui-anim{transform:translateX(5px)}.menu-container[data-placement^=right] .calcite-floating-ui-anim{transform:translateX(-5px)}.menu-container[data-placement] .calcite-floating-ui-anim--active{opacity:1;visibility:visible;transform:translate(0)}:host([open]) .menu-container{pointer-events:initial;visibility:visible;inline-size:unset;block-size:unset;overflow:unset}.menu-container--active{pointer-events:initial;visibility:visible;inline-size:unset;block-size:unset;overflow:unset}.input .calcite-input__wrapper{margin-block-start:0px}:host([range][layout=vertical][scale=m]) .vertical-arrow-container{inset-block-start:1.5rem;padding-inline-start:0.75rem}:host([range][layout=vertical][scale=m]) .vertical-arrow-container calcite-icon{block-size:0.75rem;inline-size:0.75rem;min-inline-size:0px}:host([range][layout=vertical][scale=l]) .vertical-arrow-container{inset-block-start:2.25rem;padding-inline:0.875rem}:host([range][layout=vertical][open]) .vertical-arrow-container{display:none}::slotted(input[slot=hidden-form-input]){margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;inset:0 !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  28. const InputDatePicker = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  29. constructor() {
  30. super();
  31. this.__registerHost();
  32. this.__attachShadow();
  33. this.calciteDatePickerChange = createEvent(this, "calciteDatePickerChange", 6);
  34. this.calciteDatePickerRangeChange = createEvent(this, "calciteDatePickerRangeChange", 6);
  35. this.calciteInputDatePickerChange = createEvent(this, "calciteInputDatePickerChange", 6);
  36. this.calciteInputDatePickerBeforeClose = createEvent(this, "calciteInputDatePickerBeforeClose", 6);
  37. this.calciteInputDatePickerClose = createEvent(this, "calciteInputDatePickerClose", 6);
  38. this.calciteInputDatePickerBeforeOpen = createEvent(this, "calciteInputDatePickerBeforeOpen", 6);
  39. this.calciteInputDatePickerOpen = createEvent(this, "calciteInputDatePickerOpen", 6);
  40. //--------------------------------------------------------------------------
  41. //
  42. // Public Properties
  43. //
  44. //--------------------------------------------------------------------------
  45. /**
  46. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  47. */
  48. this.disabled = false;
  49. /**
  50. * When `true`, the component's value can be read, but controls are not accessible and the value cannot be modified.
  51. *
  52. * @mdn [readOnly](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly)
  53. */
  54. this.readOnly = false;
  55. /**
  56. * When `true`, the component is active.
  57. *
  58. * @deprecated use `open` instead.
  59. */
  60. this.active = false;
  61. /** When `true`, displays the `calcite-date-picker` component. */
  62. this.open = false;
  63. /**
  64. * Accessible name for the component's previous month button.
  65. *
  66. * @default "Previous month"
  67. */
  68. this.intlPrevMonth = TEXT.prevMonth;
  69. /**
  70. * Accessible name for the component's next month button.
  71. *
  72. * @default "Next month"
  73. */
  74. this.intlNextMonth = TEXT.nextMonth;
  75. /**
  76. * Accessible name for the component's year input.
  77. *
  78. * @default "Year"
  79. */
  80. this.intlYear = TEXT.year;
  81. /** Specifies the size of the component. */
  82. this.scale = "m";
  83. /**
  84. * Specifies the placement of the `calcite-date-picker` relative to the component.
  85. *
  86. * @default "bottom-start"
  87. */
  88. this.placement = defaultMenuPlacement;
  89. /** When `true`, activates a range for the component. */
  90. this.range = false;
  91. /**
  92. * When `true`, the component must have a value in order for the form to submit.
  93. *
  94. * @internal
  95. */
  96. this.required = false;
  97. /**
  98. * Determines the type of positioning to use for the overlaid content.
  99. *
  100. * Using `"absolute"` will work for most cases. The component will be positioned inside of overflowing parent containers and will affect the container's layout.
  101. *
  102. * `"fixed"` should be used to escape an overflowing parent container, or when the reference element's `position` CSS property is `"fixed"`.
  103. *
  104. */
  105. this.overlayPositioning = "absolute";
  106. /**
  107. * When `true`, disables the default behavior on the third click of narrowing or extending the range.
  108. * Instead starts a new range.
  109. */
  110. this.proximitySelectionDisabled = false;
  111. /** Defines the layout of the component. */
  112. this.layout = "horizontal";
  113. this.effectiveLocale = "";
  114. this.focusedInput = "start";
  115. this.globalAttributes = {};
  116. this.openTransitionProp = "opacity";
  117. //--------------------------------------------------------------------------
  118. //
  119. // Private Methods
  120. //
  121. //--------------------------------------------------------------------------
  122. this.setFilteredPlacements = () => {
  123. const { el, flipPlacements } = this;
  124. this.filteredFlipPlacements = flipPlacements
  125. ? filterComputedPlacements(flipPlacements, el)
  126. : null;
  127. };
  128. this.setTransitionEl = (el) => {
  129. this.transitionEl = el;
  130. connectOpenCloseComponent(this);
  131. };
  132. this.setStartInput = (el) => {
  133. this.startInput = el;
  134. };
  135. this.setEndInput = (el) => {
  136. this.endInput = el;
  137. };
  138. this.deactivate = () => {
  139. this.open = false;
  140. };
  141. this.keyDownHandler = (event) => {
  142. const { defaultPrevented, key } = event;
  143. if (key === "Enter" && !defaultPrevented) {
  144. if (submitForm(this)) {
  145. event.preventDefault();
  146. }
  147. }
  148. else if (key === "Escape" && !defaultPrevented) {
  149. this.active = false;
  150. this.open = false;
  151. event.preventDefault();
  152. }
  153. };
  154. this.inputBlur = (event) => {
  155. this.blur(event.currentTarget);
  156. };
  157. this.startInputFocus = () => {
  158. if (!this.readOnly) {
  159. this.open = true;
  160. }
  161. this.focusedInput = "start";
  162. };
  163. this.endInputFocus = () => {
  164. if (!this.readOnly) {
  165. this.open = true;
  166. }
  167. this.focusedInput = "end";
  168. };
  169. this.startInputInput = () => {
  170. const parsedValue = this.parseNumerals(this.startInput.value);
  171. const formattedValue = this.formatNumerals(parsedValue);
  172. this.startInput.value = formattedValue;
  173. this.input(parsedValue);
  174. };
  175. this.endInputInput = () => {
  176. const parsedValue = this.parseNumerals(this.endInput.value);
  177. const formattedValue = this.formatNumerals(parsedValue);
  178. this.endInput.value = formattedValue;
  179. this.input(parsedValue);
  180. };
  181. this.setFloatingEl = (el) => {
  182. if (el) {
  183. this.floatingEl = el;
  184. connectFloatingUI(this, this.referenceEl, this.floatingEl);
  185. }
  186. };
  187. this.setStartWrapper = (el) => {
  188. this.startWrapper = el;
  189. this.setReferenceEl();
  190. };
  191. this.setEndWrapper = (el) => {
  192. this.endWrapper = el;
  193. this.setReferenceEl();
  194. };
  195. /**
  196. * Event handler for when the selected date changes
  197. *
  198. * @param event
  199. */
  200. this.handleDateChange = (event) => {
  201. if (this.range) {
  202. return;
  203. }
  204. this.value = dateToISO(event.detail);
  205. };
  206. this.handleDateRangeChange = (event) => {
  207. var _a, _b;
  208. if (!this.range || !event.detail) {
  209. return;
  210. }
  211. const { startDate, endDate } = event.detail;
  212. this.start = dateToISO(startDate);
  213. this.end = dateToISO(endDate);
  214. this.value = [this.start, this.end];
  215. if (this.shouldFocusRangeEnd()) {
  216. (_a = this.endInput) === null || _a === void 0 ? void 0 : _a.setFocus();
  217. }
  218. else if (this.shouldFocusRangeStart()) {
  219. (_b = this.startInput) === null || _b === void 0 ? void 0 : _b.setFocus();
  220. }
  221. };
  222. this.commonDateSeparators = [".", "-", "/"];
  223. this.formatNumerals = (value) => value
  224. ? value
  225. .split("")
  226. .map((char) =>
  227. // convert common separators to the locale's
  228. this.commonDateSeparators.includes(char)
  229. ? this.localeData.separator
  230. : numberKeys.includes(char)
  231. ? numberStringFormatter.numberFormatter.format(Number(char))
  232. : char)
  233. .join("")
  234. : "";
  235. this.parseNumerals = (value) => value
  236. ? value
  237. .split("")
  238. .map((char) => numberKeys.includes(char) ? numberStringFormatter.delocalize(char) : char)
  239. .join("")
  240. : "";
  241. }
  242. handleDisabledAndReadOnlyChange(value) {
  243. if (!value) {
  244. this.open = false;
  245. }
  246. }
  247. valueHandler(value) {
  248. if (Array.isArray(value)) {
  249. this.valueAsDate = getValueAsDateRange(value);
  250. this.start = value[0];
  251. this.end = value[1];
  252. }
  253. else if (value) {
  254. this.valueAsDate = dateFromISO(value);
  255. this.start = "";
  256. this.end = "";
  257. }
  258. else {
  259. this.valueAsDate = undefined;
  260. this.start = undefined;
  261. this.end = undefined;
  262. }
  263. }
  264. flipPlacementsHandler() {
  265. this.setFilteredPlacements();
  266. this.reposition(true);
  267. }
  268. onMinChanged(min) {
  269. if (min) {
  270. this.minAsDate = dateFromISO(min);
  271. }
  272. }
  273. onMaxChanged(max) {
  274. if (max) {
  275. this.maxAsDate = dateFromISO(max);
  276. }
  277. }
  278. activeHandler(value) {
  279. this.open = value;
  280. }
  281. openHandler(value) {
  282. this.active = value;
  283. if (this.disabled || this.readOnly) {
  284. if (!value) {
  285. updateAfterClose(this.floatingEl);
  286. }
  287. this.open = false;
  288. return;
  289. }
  290. if (value) {
  291. this.reposition(true);
  292. }
  293. else {
  294. updateAfterClose(this.floatingEl);
  295. }
  296. }
  297. overlayPositioningHandler() {
  298. this.reposition(true);
  299. }
  300. //--------------------------------------------------------------------------
  301. //
  302. // Event Listeners
  303. //
  304. //--------------------------------------------------------------------------
  305. handleDateOrRangeChange() {
  306. this.calciteInputDatePickerChange.emit();
  307. }
  308. calciteDaySelectHandler() {
  309. if (this.shouldFocusRangeStart() || this.shouldFocusRangeEnd()) {
  310. return;
  311. }
  312. this.open = false;
  313. }
  314. // --------------------------------------------------------------------------
  315. //
  316. // Public Methods
  317. //
  318. // --------------------------------------------------------------------------
  319. /** Sets focus on the component. */
  320. async setFocus() {
  321. var _a;
  322. (_a = this.startInput) === null || _a === void 0 ? void 0 : _a.setFocus();
  323. }
  324. /**
  325. * Updates the position of the component.
  326. *
  327. * @param delayed
  328. */
  329. async reposition(delayed = false) {
  330. const { floatingEl, referenceEl, placement, overlayPositioning, filteredFlipPlacements } = this;
  331. return reposition(this, {
  332. floatingEl,
  333. referenceEl,
  334. overlayPositioning,
  335. placement,
  336. flipPlacements: filteredFlipPlacements,
  337. type: "menu"
  338. }, delayed);
  339. }
  340. // --------------------------------------------------------------------------
  341. //
  342. // Lifecycle
  343. //
  344. // --------------------------------------------------------------------------
  345. connectedCallback() {
  346. connectLocalized(this);
  347. const isOpen = this.active || this.open;
  348. isOpen && this.activeHandler(isOpen);
  349. isOpen && this.openHandler(isOpen);
  350. if (Array.isArray(this.value)) {
  351. this.valueAsDate = getValueAsDateRange(this.value);
  352. this.start = this.value[0];
  353. this.end = this.value[1];
  354. }
  355. else if (this.value) {
  356. this.valueAsDate = dateFromISO(this.value);
  357. this.start = "";
  358. this.end = "";
  359. }
  360. if (this.start) {
  361. this.startAsDate = dateFromISO(this.start);
  362. }
  363. if (this.end) {
  364. this.endAsDate = setEndOfDay(dateFromISO(this.end));
  365. }
  366. if (this.min) {
  367. this.minAsDate = dateFromISO(this.min);
  368. }
  369. if (this.max) {
  370. this.maxAsDate = dateFromISO(this.max);
  371. }
  372. connectLabel(this);
  373. connectForm(this);
  374. connectOpenCloseComponent(this);
  375. this.setFilteredPlacements();
  376. this.reposition(true);
  377. numberStringFormatter.numberFormatOptions = {
  378. numberingSystem: this.numberingSystem,
  379. locale: this.effectiveLocale,
  380. useGrouping: false
  381. };
  382. }
  383. async componentWillLoad() {
  384. await this.loadLocaleData();
  385. this.onMinChanged(this.min);
  386. this.onMaxChanged(this.max);
  387. }
  388. componentDidLoad() {
  389. this.reposition(true);
  390. }
  391. disconnectedCallback() {
  392. disconnectLabel(this);
  393. disconnectForm(this);
  394. disconnectFloatingUI(this, this.referenceEl, this.floatingEl);
  395. disconnectOpenCloseComponent(this);
  396. disconnectLocalized(this);
  397. }
  398. componentDidRender() {
  399. updateHostInteraction(this);
  400. }
  401. render() {
  402. var _a, _b;
  403. const { disabled, readOnly, numberingSystem, effectiveLocale } = this;
  404. numberStringFormatter.numberFormatOptions = {
  405. numberingSystem,
  406. locale: effectiveLocale,
  407. useGrouping: false
  408. };
  409. const date = dateFromRange(this.range ? this.startAsDate : this.valueAsDate, this.minAsDate, this.maxAsDate);
  410. const endDate = this.range
  411. ? dateFromRange(this.endAsDate, this.minAsDate, this.maxAsDate)
  412. : null;
  413. const formattedEndDate = endDate
  414. ? this.formatNumerals(endDate.toLocaleDateString(effectiveLocale))
  415. : "";
  416. const formattedDate = date ? this.formatNumerals(date.toLocaleDateString(effectiveLocale)) : "";
  417. return (h(Host, { onBlur: this.deactivate, onKeyDown: this.keyDownHandler, role: "application" }, this.localeData && (h("div", { "aria-expanded": toAriaBoolean(this.open), 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), lang: effectiveLocale, "number-button-type": "none", numberingSystem: numberingSystem, onCalciteInputInput: this.startInputInput, onCalciteInternalInputBlur: this.inputBlur, onCalciteInternalInputFocus: this.startInputFocus, placeholder: (_a = this.localeData) === null || _a === void 0 ? void 0 : _a.placeholder, readOnly: readOnly, ref: this.setStartInput, scale: this.scale, type: "text", value: formattedDate })), h("div", { "aria-hidden": toAriaBoolean(!this.open), class: {
  418. [CSS.menu]: true,
  419. [CSS.menuActive]: this.open
  420. }, ref: this.setFloatingEl }, h("div", { class: {
  421. ["calendar-picker-wrapper"]: true,
  422. ["calendar-picker-wrapper--end"]: this.focusedInput === "end",
  423. [FloatingCSS.animation]: true,
  424. [FloatingCSS.animationActive]: this.open
  425. }, ref: this.setTransitionEl }, h("calcite-date-picker", { activeRange: this.focusedInput, endAsDate: this.endAsDate, headingLevel: this.headingLevel, intlNextMonth: this.intlNextMonth, intlPrevMonth: this.intlPrevMonth, intlYear: this.intlYear, lang: effectiveLocale, max: this.max, maxAsDate: this.maxAsDate, min: this.min, minAsDate: this.minAsDate, numberingSystem: numberingSystem, 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: {
  426. input: true,
  427. "border-top-color-one": this.layout === "vertical" && this.range
  428. }, disabled: disabled, icon: "calendar", lang: effectiveLocale, "number-button-type": "none", numberingSystem: numberingSystem, onCalciteInputInput: this.endInputInput, onCalciteInternalInputBlur: this.inputBlur, onCalciteInternalInputFocus: this.endInputFocus, placeholder: (_b = this.localeData) === null || _b === void 0 ? void 0 : _b.placeholder, readOnly: readOnly, ref: this.setEndInput, scale: this.scale, type: "text", value: formattedEndDate }))))), h(HiddenFormInputSlot, { component: this })));
  429. }
  430. setReferenceEl() {
  431. const { focusedInput, layout, endWrapper, startWrapper } = this;
  432. this.referenceEl =
  433. focusedInput === "end" || layout === "vertical"
  434. ? endWrapper || startWrapper
  435. : startWrapper || endWrapper;
  436. connectFloatingUI(this, this.referenceEl, this.floatingEl);
  437. }
  438. onLabelClick() {
  439. this.setFocus();
  440. }
  441. onBeforeOpen() {
  442. this.calciteInputDatePickerBeforeOpen.emit();
  443. }
  444. onOpen() {
  445. this.calciteInputDatePickerOpen.emit();
  446. }
  447. onBeforeClose() {
  448. this.calciteInputDatePickerBeforeClose.emit();
  449. }
  450. onClose() {
  451. this.calciteInputDatePickerClose.emit();
  452. }
  453. startWatcher(start) {
  454. this.startAsDate = dateFromISO(start);
  455. }
  456. endWatcher(end) {
  457. this.endAsDate = end ? setEndOfDay(dateFromISO(end)) : dateFromISO(end);
  458. }
  459. async loadLocaleData() {
  460. if (!Build.isBrowser) {
  461. return;
  462. }
  463. this.localeData = await getLocaleData(this.effectiveLocale);
  464. }
  465. clearCurrentValue() {
  466. if (!this.range) {
  467. if (typeof this.value === "string" && this.value) {
  468. this.calciteDatePickerChange.emit();
  469. }
  470. this.value = "";
  471. return;
  472. }
  473. const { focusedInput } = this;
  474. if (focusedInput === "start") {
  475. if (this.start) {
  476. this.calciteDatePickerRangeChange.emit();
  477. }
  478. this.value = Array.isArray(this.value) ? ["", this.value[1] || ""] : [""];
  479. this.start = undefined;
  480. }
  481. else if (focusedInput === "end") {
  482. if (this.end) {
  483. this.calciteDatePickerRangeChange.emit();
  484. }
  485. this.value = Array.isArray(this.value) ? [this.value[0] || "", ""] : ["", ""];
  486. this.end = undefined;
  487. }
  488. }
  489. /**
  490. * If inputted string is a valid date, update value/active
  491. *
  492. * @param value
  493. */
  494. input(value) {
  495. const date = this.getDateFromInput(value);
  496. if (!date) {
  497. this.clearCurrentValue();
  498. return;
  499. }
  500. if (!this.range) {
  501. this.value = dateToISO(date);
  502. this.calciteDatePickerChange.emit(date);
  503. return;
  504. }
  505. const { focusedInput } = this;
  506. if (focusedInput === "start") {
  507. if (!this.startAsDate || !sameDate(date, this.startAsDate)) {
  508. const startDateISO = dateToISO(date);
  509. this.value = Array.isArray(this.value)
  510. ? [startDateISO, this.value[1] || ""]
  511. : [startDateISO];
  512. this.start = startDateISO;
  513. this.calciteDatePickerRangeChange.emit({
  514. startDate: date,
  515. endDate: this.endAsDate
  516. });
  517. }
  518. }
  519. else if (focusedInput === "end") {
  520. if (!this.endAsDate || !sameDate(date, this.endAsDate)) {
  521. const endDateISO = dateToISO(date);
  522. this.value = Array.isArray(this.value)
  523. ? [this.value[0] || "", endDateISO]
  524. : ["", endDateISO];
  525. this.end = endDateISO;
  526. this.calciteDatePickerRangeChange.emit({
  527. startDate: this.startAsDate,
  528. endDate: setEndOfDay(date)
  529. });
  530. }
  531. }
  532. }
  533. /**
  534. * Clean up invalid date from input on blur
  535. *
  536. * @param target
  537. */
  538. blur(target) {
  539. const { focusedInput, endAsDate, range, startAsDate, valueAsDate } = this;
  540. const locale = this.effectiveLocale;
  541. const date = this.getDateFromInput(target.value);
  542. if (!date) {
  543. if (!range && valueAsDate) {
  544. target.value = this.formatNumerals(Array.isArray(valueAsDate)
  545. ? valueAsDate[focusedInput === "end" ? 1 : 0].toLocaleDateString(locale)
  546. : valueAsDate.toLocaleDateString(locale));
  547. }
  548. else if (focusedInput === "start" && startAsDate) {
  549. target.value = this.formatNumerals(startAsDate.toLocaleDateString(locale));
  550. }
  551. else if (focusedInput === "end" && endAsDate) {
  552. target.value = this.formatNumerals(endAsDate.toLocaleDateString(locale));
  553. }
  554. }
  555. }
  556. shouldFocusRangeStart() {
  557. return !!(this.endAsDate &&
  558. !this.startAsDate &&
  559. this.focusedInput === "end" &&
  560. this.startInput);
  561. }
  562. shouldFocusRangeEnd() {
  563. return !!(this.startAsDate &&
  564. !this.endAsDate &&
  565. this.focusedInput === "start" &&
  566. this.endInput);
  567. }
  568. /**
  569. * Find a date from input string
  570. * return false if date is invalid, or out of range
  571. *
  572. * @param value
  573. */
  574. getDateFromInput(value) {
  575. if (!this.localeData) {
  576. return false;
  577. }
  578. const { separator } = this.localeData;
  579. const { day, month, year } = parseDateString(value, this.localeData);
  580. const validDay = day > 0;
  581. const validMonth = month > -1;
  582. const date = new Date(year, month, day);
  583. date.setFullYear(year);
  584. const validDate = !isNaN(date.getTime());
  585. const validLength = value.split(separator).filter((c) => c).length > 2;
  586. const validYear = year.toString().length > 0;
  587. if (validDay &&
  588. validMonth &&
  589. validDate &&
  590. validLength &&
  591. validYear &&
  592. inRange(date, this.min, this.max)) {
  593. return date;
  594. }
  595. return false;
  596. }
  597. get el() { return this; }
  598. static get watchers() { return {
  599. "disabled": ["handleDisabledAndReadOnlyChange"],
  600. "readOnly": ["handleDisabledAndReadOnlyChange"],
  601. "value": ["valueHandler"],
  602. "flipPlacements": ["flipPlacementsHandler"],
  603. "min": ["onMinChanged"],
  604. "max": ["onMaxChanged"],
  605. "active": ["activeHandler"],
  606. "open": ["openHandler"],
  607. "overlayPositioning": ["overlayPositioningHandler"],
  608. "layout": ["setReferenceEl"],
  609. "focusedInput": ["setReferenceEl"],
  610. "start": ["startWatcher"],
  611. "end": ["endWatcher"],
  612. "effectiveLocale": ["loadLocaleData"]
  613. }; }
  614. static get style() { return inputDatePickerCss; }
  615. }, [1, "calcite-input-date-picker", {
  616. "disabled": [516],
  617. "readOnly": [516, "read-only"],
  618. "value": [1025],
  619. "flipPlacements": [16],
  620. "headingLevel": [514, "heading-level"],
  621. "valueAsDate": [1040],
  622. "startAsDate": [1040],
  623. "endAsDate": [1040],
  624. "minAsDate": [1040],
  625. "maxAsDate": [1040],
  626. "min": [1025],
  627. "max": [1025],
  628. "active": [1540],
  629. "open": [1540],
  630. "name": [513],
  631. "intlPrevMonth": [1, "intl-prev-month"],
  632. "intlNextMonth": [1, "intl-next-month"],
  633. "intlYear": [1, "intl-year"],
  634. "locale": [1],
  635. "numberingSystem": [513, "numbering-system"],
  636. "scale": [513],
  637. "placement": [513],
  638. "range": [516],
  639. "required": [516],
  640. "start": [1537],
  641. "end": [1537],
  642. "overlayPositioning": [513, "overlay-positioning"],
  643. "proximitySelectionDisabled": [4, "proximity-selection-disabled"],
  644. "layout": [513],
  645. "effectiveLocale": [32],
  646. "focusedInput": [32],
  647. "globalAttributes": [32],
  648. "localeData": [32],
  649. "setFocus": [64],
  650. "reposition": [64]
  651. }, [[0, "calciteDatePickerChange", "handleDateOrRangeChange"], [0, "calciteDatePickerRangeChange", "handleDateOrRangeChange"], [0, "calciteDaySelect", "calciteDaySelectHandler"]]]);
  652. function defineCustomElement$1() {
  653. if (typeof customElements === "undefined") {
  654. return;
  655. }
  656. const components = ["calcite-input-date-picker", "calcite-date-picker", "calcite-date-picker-day", "calcite-date-picker-month", "calcite-date-picker-month-header", "calcite-icon", "calcite-input", "calcite-progress"];
  657. components.forEach(tagName => { switch (tagName) {
  658. case "calcite-input-date-picker":
  659. if (!customElements.get(tagName)) {
  660. customElements.define(tagName, InputDatePicker);
  661. }
  662. break;
  663. case "calcite-date-picker":
  664. if (!customElements.get(tagName)) {
  665. defineCustomElement$8();
  666. }
  667. break;
  668. case "calcite-date-picker-day":
  669. if (!customElements.get(tagName)) {
  670. defineCustomElement$7();
  671. }
  672. break;
  673. case "calcite-date-picker-month":
  674. if (!customElements.get(tagName)) {
  675. defineCustomElement$6();
  676. }
  677. break;
  678. case "calcite-date-picker-month-header":
  679. if (!customElements.get(tagName)) {
  680. defineCustomElement$5();
  681. }
  682. break;
  683. case "calcite-icon":
  684. if (!customElements.get(tagName)) {
  685. defineCustomElement$4();
  686. }
  687. break;
  688. case "calcite-input":
  689. if (!customElements.get(tagName)) {
  690. defineCustomElement$3();
  691. }
  692. break;
  693. case "calcite-progress":
  694. if (!customElements.get(tagName)) {
  695. defineCustomElement$2();
  696. }
  697. break;
  698. } });
  699. }
  700. defineCustomElement$1();
  701. const CalciteInputDatePicker = InputDatePicker;
  702. const defineCustomElement = defineCustomElement$1;
  703. export { CalciteInputDatePicker, defineCustomElement };