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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-1f9b54dc.js';
  7. import { T as TEXT, d as dateToISO, g as getValueAsDateRange, a as dateFromISO, s as setEndOfDay, b as dateFromRange, c as getLocaleData, e as sameDate, p as parseDateString, i as inRange } from './resources-8812a15c.js';
  8. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label-333c4a4c.js';
  9. import { s as submitForm, c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form-80dbd90e.js';
  10. 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-5c49748f.js';
  11. import { u as updateHostInteraction } from './interactive-5db230e8.js';
  12. import { t as toAriaBoolean } from './dom-8f0a9ff2.js';
  13. import { c as connectOpenCloseComponent, d as disconnectOpenCloseComponent } from './openCloseComponent-ba7d452b.js';
  14. import { n as numberStringFormatter, c as connectLocalized, d as disconnectLocalized } from './locale-35f81208.js';
  15. import { n as numberKeys } from './key-acc755b7.js';
  16. import './debounce-d85a6654.js';
  17. import './resources-9c476cb6.js';
  18. import './guid-9f15e57a.js';
  19. import './observers-9f44e9b3.js';
  20. const CSS = {
  21. menu: "menu-container",
  22. menuActive: "menu-container--active"
  23. };
  24. 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}";
  25. const InputDatePicker = class {
  26. constructor(hostRef) {
  27. registerInstance(this, hostRef);
  28. this.calciteDatePickerChange = createEvent(this, "calciteDatePickerChange", 6);
  29. this.calciteDatePickerRangeChange = createEvent(this, "calciteDatePickerRangeChange", 6);
  30. this.calciteInputDatePickerChange = createEvent(this, "calciteInputDatePickerChange", 6);
  31. this.calciteInputDatePickerBeforeClose = createEvent(this, "calciteInputDatePickerBeforeClose", 6);
  32. this.calciteInputDatePickerClose = createEvent(this, "calciteInputDatePickerClose", 6);
  33. this.calciteInputDatePickerBeforeOpen = createEvent(this, "calciteInputDatePickerBeforeOpen", 6);
  34. this.calciteInputDatePickerOpen = createEvent(this, "calciteInputDatePickerOpen", 6);
  35. //--------------------------------------------------------------------------
  36. //
  37. // Public Properties
  38. //
  39. //--------------------------------------------------------------------------
  40. /**
  41. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  42. */
  43. this.disabled = false;
  44. /**
  45. * When `true`, the component's value can be read, but controls are not accessible and the value cannot be modified.
  46. *
  47. * @mdn [readOnly](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly)
  48. */
  49. this.readOnly = false;
  50. /**
  51. * When `true`, the component is active.
  52. *
  53. * @deprecated use `open` instead.
  54. */
  55. this.active = false;
  56. /** When `true`, displays the `calcite-date-picker` component. */
  57. this.open = false;
  58. /**
  59. * Accessible name for the component's previous month button.
  60. *
  61. * @default "Previous month"
  62. */
  63. this.intlPrevMonth = TEXT.prevMonth;
  64. /**
  65. * Accessible name for the component's next month button.
  66. *
  67. * @default "Next month"
  68. */
  69. this.intlNextMonth = TEXT.nextMonth;
  70. /**
  71. * Accessible name for the component's year input.
  72. *
  73. * @default "Year"
  74. */
  75. this.intlYear = TEXT.year;
  76. /** Specifies the size of the component. */
  77. this.scale = "m";
  78. /**
  79. * Specifies the placement of the `calcite-date-picker` relative to the component.
  80. *
  81. * @default "bottom-start"
  82. */
  83. this.placement = defaultMenuPlacement;
  84. /** When `true`, activates a range for the component. */
  85. this.range = false;
  86. /**
  87. * When `true`, the component must have a value in order for the form to submit.
  88. *
  89. * @internal
  90. */
  91. this.required = false;
  92. /**
  93. * Determines the type of positioning to use for the overlaid content.
  94. *
  95. * Using `"absolute"` will work for most cases. The component will be positioned inside of overflowing parent containers and will affect the container's layout.
  96. *
  97. * `"fixed"` should be used to escape an overflowing parent container, or when the reference element's `position` CSS property is `"fixed"`.
  98. *
  99. */
  100. this.overlayPositioning = "absolute";
  101. /**
  102. * When `true`, disables the default behavior on the third click of narrowing or extending the range.
  103. * Instead starts a new range.
  104. */
  105. this.proximitySelectionDisabled = false;
  106. /** Defines the layout of the component. */
  107. this.layout = "horizontal";
  108. this.effectiveLocale = "";
  109. this.focusedInput = "start";
  110. this.globalAttributes = {};
  111. this.openTransitionProp = "opacity";
  112. //--------------------------------------------------------------------------
  113. //
  114. // Private Methods
  115. //
  116. //--------------------------------------------------------------------------
  117. this.setFilteredPlacements = () => {
  118. const { el, flipPlacements } = this;
  119. this.filteredFlipPlacements = flipPlacements
  120. ? filterComputedPlacements(flipPlacements, el)
  121. : null;
  122. };
  123. this.setTransitionEl = (el) => {
  124. this.transitionEl = el;
  125. connectOpenCloseComponent(this);
  126. };
  127. this.setStartInput = (el) => {
  128. this.startInput = el;
  129. };
  130. this.setEndInput = (el) => {
  131. this.endInput = el;
  132. };
  133. this.deactivate = () => {
  134. this.open = false;
  135. };
  136. this.keyDownHandler = (event) => {
  137. const { defaultPrevented, key } = event;
  138. if (key === "Enter" && !defaultPrevented) {
  139. if (submitForm(this)) {
  140. event.preventDefault();
  141. }
  142. }
  143. else if (key === "Escape" && !defaultPrevented) {
  144. this.active = false;
  145. this.open = false;
  146. event.preventDefault();
  147. }
  148. };
  149. this.inputBlur = (event) => {
  150. this.blur(event.currentTarget);
  151. };
  152. this.startInputFocus = () => {
  153. if (!this.readOnly) {
  154. this.open = true;
  155. }
  156. this.focusedInput = "start";
  157. };
  158. this.endInputFocus = () => {
  159. if (!this.readOnly) {
  160. this.open = true;
  161. }
  162. this.focusedInput = "end";
  163. };
  164. this.startInputInput = () => {
  165. const parsedValue = this.parseNumerals(this.startInput.value);
  166. const formattedValue = this.formatNumerals(parsedValue);
  167. this.startInput.value = formattedValue;
  168. this.input(parsedValue);
  169. };
  170. this.endInputInput = () => {
  171. const parsedValue = this.parseNumerals(this.endInput.value);
  172. const formattedValue = this.formatNumerals(parsedValue);
  173. this.endInput.value = formattedValue;
  174. this.input(parsedValue);
  175. };
  176. this.setFloatingEl = (el) => {
  177. if (el) {
  178. this.floatingEl = el;
  179. connectFloatingUI(this, this.referenceEl, this.floatingEl);
  180. }
  181. };
  182. this.setStartWrapper = (el) => {
  183. this.startWrapper = el;
  184. this.setReferenceEl();
  185. };
  186. this.setEndWrapper = (el) => {
  187. this.endWrapper = el;
  188. this.setReferenceEl();
  189. };
  190. /**
  191. * Event handler for when the selected date changes
  192. *
  193. * @param event
  194. */
  195. this.handleDateChange = (event) => {
  196. if (this.range) {
  197. return;
  198. }
  199. this.value = dateToISO(event.detail);
  200. };
  201. this.handleDateRangeChange = (event) => {
  202. var _a, _b;
  203. if (!this.range || !event.detail) {
  204. return;
  205. }
  206. const { startDate, endDate } = event.detail;
  207. this.start = dateToISO(startDate);
  208. this.end = dateToISO(endDate);
  209. this.value = [this.start, this.end];
  210. if (this.shouldFocusRangeEnd()) {
  211. (_a = this.endInput) === null || _a === void 0 ? void 0 : _a.setFocus();
  212. }
  213. else if (this.shouldFocusRangeStart()) {
  214. (_b = this.startInput) === null || _b === void 0 ? void 0 : _b.setFocus();
  215. }
  216. };
  217. this.commonDateSeparators = [".", "-", "/"];
  218. this.formatNumerals = (value) => value
  219. ? value
  220. .split("")
  221. .map((char) =>
  222. // convert common separators to the locale's
  223. this.commonDateSeparators.includes(char)
  224. ? this.localeData.separator
  225. : numberKeys.includes(char)
  226. ? numberStringFormatter.numberFormatter.format(Number(char))
  227. : char)
  228. .join("")
  229. : "";
  230. this.parseNumerals = (value) => value
  231. ? value
  232. .split("")
  233. .map((char) => numberKeys.includes(char) ? numberStringFormatter.delocalize(char) : char)
  234. .join("")
  235. : "";
  236. }
  237. handleDisabledAndReadOnlyChange(value) {
  238. if (!value) {
  239. this.open = false;
  240. }
  241. }
  242. valueHandler(value) {
  243. if (Array.isArray(value)) {
  244. this.valueAsDate = getValueAsDateRange(value);
  245. this.start = value[0];
  246. this.end = value[1];
  247. }
  248. else if (value) {
  249. this.valueAsDate = dateFromISO(value);
  250. this.start = "";
  251. this.end = "";
  252. }
  253. else {
  254. this.valueAsDate = undefined;
  255. this.start = undefined;
  256. this.end = undefined;
  257. }
  258. }
  259. flipPlacementsHandler() {
  260. this.setFilteredPlacements();
  261. this.reposition(true);
  262. }
  263. onMinChanged(min) {
  264. if (min) {
  265. this.minAsDate = dateFromISO(min);
  266. }
  267. }
  268. onMaxChanged(max) {
  269. if (max) {
  270. this.maxAsDate = dateFromISO(max);
  271. }
  272. }
  273. activeHandler(value) {
  274. this.open = value;
  275. }
  276. openHandler(value) {
  277. this.active = value;
  278. if (this.disabled || this.readOnly) {
  279. if (!value) {
  280. updateAfterClose(this.floatingEl);
  281. }
  282. this.open = false;
  283. return;
  284. }
  285. if (value) {
  286. this.reposition(true);
  287. }
  288. else {
  289. updateAfterClose(this.floatingEl);
  290. }
  291. }
  292. overlayPositioningHandler() {
  293. this.reposition(true);
  294. }
  295. //--------------------------------------------------------------------------
  296. //
  297. // Event Listeners
  298. //
  299. //--------------------------------------------------------------------------
  300. handleDateOrRangeChange() {
  301. this.calciteInputDatePickerChange.emit();
  302. }
  303. calciteDaySelectHandler() {
  304. if (this.shouldFocusRangeStart() || this.shouldFocusRangeEnd()) {
  305. return;
  306. }
  307. this.open = false;
  308. }
  309. // --------------------------------------------------------------------------
  310. //
  311. // Public Methods
  312. //
  313. // --------------------------------------------------------------------------
  314. /** Sets focus on the component. */
  315. async setFocus() {
  316. var _a;
  317. (_a = this.startInput) === null || _a === void 0 ? void 0 : _a.setFocus();
  318. }
  319. /**
  320. * Updates the position of the component.
  321. *
  322. * @param delayed
  323. */
  324. async reposition(delayed = false) {
  325. const { floatingEl, referenceEl, placement, overlayPositioning, filteredFlipPlacements } = this;
  326. return reposition(this, {
  327. floatingEl,
  328. referenceEl,
  329. overlayPositioning,
  330. placement,
  331. flipPlacements: filteredFlipPlacements,
  332. type: "menu"
  333. }, delayed);
  334. }
  335. // --------------------------------------------------------------------------
  336. //
  337. // Lifecycle
  338. //
  339. // --------------------------------------------------------------------------
  340. connectedCallback() {
  341. connectLocalized(this);
  342. const isOpen = this.active || this.open;
  343. isOpen && this.activeHandler(isOpen);
  344. isOpen && this.openHandler(isOpen);
  345. if (Array.isArray(this.value)) {
  346. this.valueAsDate = getValueAsDateRange(this.value);
  347. this.start = this.value[0];
  348. this.end = this.value[1];
  349. }
  350. else if (this.value) {
  351. this.valueAsDate = dateFromISO(this.value);
  352. this.start = "";
  353. this.end = "";
  354. }
  355. if (this.start) {
  356. this.startAsDate = dateFromISO(this.start);
  357. }
  358. if (this.end) {
  359. this.endAsDate = setEndOfDay(dateFromISO(this.end));
  360. }
  361. if (this.min) {
  362. this.minAsDate = dateFromISO(this.min);
  363. }
  364. if (this.max) {
  365. this.maxAsDate = dateFromISO(this.max);
  366. }
  367. connectLabel(this);
  368. connectForm(this);
  369. connectOpenCloseComponent(this);
  370. this.setFilteredPlacements();
  371. this.reposition(true);
  372. numberStringFormatter.numberFormatOptions = {
  373. numberingSystem: this.numberingSystem,
  374. locale: this.effectiveLocale,
  375. useGrouping: false
  376. };
  377. }
  378. async componentWillLoad() {
  379. await this.loadLocaleData();
  380. this.onMinChanged(this.min);
  381. this.onMaxChanged(this.max);
  382. }
  383. componentDidLoad() {
  384. this.reposition(true);
  385. }
  386. disconnectedCallback() {
  387. disconnectLabel(this);
  388. disconnectForm(this);
  389. disconnectFloatingUI(this, this.referenceEl, this.floatingEl);
  390. disconnectOpenCloseComponent(this);
  391. disconnectLocalized(this);
  392. }
  393. componentDidRender() {
  394. updateHostInteraction(this);
  395. }
  396. render() {
  397. var _a, _b;
  398. const { disabled, readOnly, numberingSystem, effectiveLocale } = this;
  399. numberStringFormatter.numberFormatOptions = {
  400. numberingSystem,
  401. locale: effectiveLocale,
  402. useGrouping: false
  403. };
  404. const date = dateFromRange(this.range ? this.startAsDate : this.valueAsDate, this.minAsDate, this.maxAsDate);
  405. const endDate = this.range
  406. ? dateFromRange(this.endAsDate, this.minAsDate, this.maxAsDate)
  407. : null;
  408. const formattedEndDate = endDate
  409. ? this.formatNumerals(endDate.toLocaleDateString(effectiveLocale))
  410. : "";
  411. const formattedDate = date ? this.formatNumerals(date.toLocaleDateString(effectiveLocale)) : "";
  412. 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: {
  413. [CSS.menu]: true,
  414. [CSS.menuActive]: this.open
  415. }, ref: this.setFloatingEl }, h("div", { class: {
  416. ["calendar-picker-wrapper"]: true,
  417. ["calendar-picker-wrapper--end"]: this.focusedInput === "end",
  418. [FloatingCSS.animation]: true,
  419. [FloatingCSS.animationActive]: this.open
  420. }, 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: {
  421. input: true,
  422. "border-top-color-one": this.layout === "vertical" && this.range
  423. }, 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 })));
  424. }
  425. setReferenceEl() {
  426. const { focusedInput, layout, endWrapper, startWrapper } = this;
  427. this.referenceEl =
  428. focusedInput === "end" || layout === "vertical"
  429. ? endWrapper || startWrapper
  430. : startWrapper || endWrapper;
  431. connectFloatingUI(this, this.referenceEl, this.floatingEl);
  432. }
  433. onLabelClick() {
  434. this.setFocus();
  435. }
  436. onBeforeOpen() {
  437. this.calciteInputDatePickerBeforeOpen.emit();
  438. }
  439. onOpen() {
  440. this.calciteInputDatePickerOpen.emit();
  441. }
  442. onBeforeClose() {
  443. this.calciteInputDatePickerBeforeClose.emit();
  444. }
  445. onClose() {
  446. this.calciteInputDatePickerClose.emit();
  447. }
  448. startWatcher(start) {
  449. this.startAsDate = dateFromISO(start);
  450. }
  451. endWatcher(end) {
  452. this.endAsDate = end ? setEndOfDay(dateFromISO(end)) : dateFromISO(end);
  453. }
  454. async loadLocaleData() {
  455. this.localeData = await getLocaleData(this.effectiveLocale);
  456. }
  457. clearCurrentValue() {
  458. if (!this.range) {
  459. if (typeof this.value === "string" && this.value) {
  460. this.calciteDatePickerChange.emit();
  461. }
  462. this.value = "";
  463. return;
  464. }
  465. const { focusedInput } = this;
  466. if (focusedInput === "start") {
  467. if (this.start) {
  468. this.calciteDatePickerRangeChange.emit();
  469. }
  470. this.value = Array.isArray(this.value) ? ["", this.value[1] || ""] : [""];
  471. this.start = undefined;
  472. }
  473. else if (focusedInput === "end") {
  474. if (this.end) {
  475. this.calciteDatePickerRangeChange.emit();
  476. }
  477. this.value = Array.isArray(this.value) ? [this.value[0] || "", ""] : ["", ""];
  478. this.end = undefined;
  479. }
  480. }
  481. /**
  482. * If inputted string is a valid date, update value/active
  483. *
  484. * @param value
  485. */
  486. input(value) {
  487. const date = this.getDateFromInput(value);
  488. if (!date) {
  489. this.clearCurrentValue();
  490. return;
  491. }
  492. if (!this.range) {
  493. this.value = dateToISO(date);
  494. this.calciteDatePickerChange.emit(date);
  495. return;
  496. }
  497. const { focusedInput } = this;
  498. if (focusedInput === "start") {
  499. if (!this.startAsDate || !sameDate(date, this.startAsDate)) {
  500. const startDateISO = dateToISO(date);
  501. this.value = Array.isArray(this.value)
  502. ? [startDateISO, this.value[1] || ""]
  503. : [startDateISO];
  504. this.start = startDateISO;
  505. this.calciteDatePickerRangeChange.emit({
  506. startDate: date,
  507. endDate: this.endAsDate
  508. });
  509. }
  510. }
  511. else if (focusedInput === "end") {
  512. if (!this.endAsDate || !sameDate(date, this.endAsDate)) {
  513. const endDateISO = dateToISO(date);
  514. this.value = Array.isArray(this.value)
  515. ? [this.value[0] || "", endDateISO]
  516. : ["", endDateISO];
  517. this.end = endDateISO;
  518. this.calciteDatePickerRangeChange.emit({
  519. startDate: this.startAsDate,
  520. endDate: setEndOfDay(date)
  521. });
  522. }
  523. }
  524. }
  525. /**
  526. * Clean up invalid date from input on blur
  527. *
  528. * @param target
  529. */
  530. blur(target) {
  531. const { focusedInput, endAsDate, range, startAsDate, valueAsDate } = this;
  532. const locale = this.effectiveLocale;
  533. const date = this.getDateFromInput(target.value);
  534. if (!date) {
  535. if (!range && valueAsDate) {
  536. target.value = this.formatNumerals(Array.isArray(valueAsDate)
  537. ? valueAsDate[focusedInput === "end" ? 1 : 0].toLocaleDateString(locale)
  538. : valueAsDate.toLocaleDateString(locale));
  539. }
  540. else if (focusedInput === "start" && startAsDate) {
  541. target.value = this.formatNumerals(startAsDate.toLocaleDateString(locale));
  542. }
  543. else if (focusedInput === "end" && endAsDate) {
  544. target.value = this.formatNumerals(endAsDate.toLocaleDateString(locale));
  545. }
  546. }
  547. }
  548. shouldFocusRangeStart() {
  549. return !!(this.endAsDate &&
  550. !this.startAsDate &&
  551. this.focusedInput === "end" &&
  552. this.startInput);
  553. }
  554. shouldFocusRangeEnd() {
  555. return !!(this.startAsDate &&
  556. !this.endAsDate &&
  557. this.focusedInput === "start" &&
  558. this.endInput);
  559. }
  560. /**
  561. * Find a date from input string
  562. * return false if date is invalid, or out of range
  563. *
  564. * @param value
  565. */
  566. getDateFromInput(value) {
  567. if (!this.localeData) {
  568. return false;
  569. }
  570. const { separator } = this.localeData;
  571. const { day, month, year } = parseDateString(value, this.localeData);
  572. const validDay = day > 0;
  573. const validMonth = month > -1;
  574. const date = new Date(year, month, day);
  575. date.setFullYear(year);
  576. const validDate = !isNaN(date.getTime());
  577. const validLength = value.split(separator).filter((c) => c).length > 2;
  578. const validYear = year.toString().length > 0;
  579. if (validDay &&
  580. validMonth &&
  581. validDate &&
  582. validLength &&
  583. validYear &&
  584. inRange(date, this.min, this.max)) {
  585. return date;
  586. }
  587. return false;
  588. }
  589. get el() { return getElement(this); }
  590. static get watchers() { return {
  591. "disabled": ["handleDisabledAndReadOnlyChange"],
  592. "readOnly": ["handleDisabledAndReadOnlyChange"],
  593. "value": ["valueHandler"],
  594. "flipPlacements": ["flipPlacementsHandler"],
  595. "min": ["onMinChanged"],
  596. "max": ["onMaxChanged"],
  597. "active": ["activeHandler"],
  598. "open": ["openHandler"],
  599. "overlayPositioning": ["overlayPositioningHandler"],
  600. "layout": ["setReferenceEl"],
  601. "focusedInput": ["setReferenceEl"],
  602. "start": ["startWatcher"],
  603. "end": ["endWatcher"],
  604. "effectiveLocale": ["loadLocaleData"]
  605. }; }
  606. };
  607. InputDatePicker.style = inputDatePickerCss;
  608. export { InputDatePicker as calcite_input_date_picker };