calcite-input-time-picker.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 } from '@stencil/core/internal/client/index.js';
  7. import { g as guid } from './guid.js';
  8. import { l as localizeTimeString, f as formatTimeString, i as isValidTime, d as defineCustomElement$2 } from './time-picker.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 { u as updateHostInteraction } from './interactive.js';
  12. import { n as numberStringFormatter, u as updateEffectiveLocale, c as connectLocalized, d as disconnectLocalized } from './locale.js';
  13. import { n as numberKeys } from './key.js';
  14. import { d as defineCustomElement$8 } from './action.js';
  15. import { d as defineCustomElement$7 } from './icon.js';
  16. import { d as defineCustomElement$6 } from './input.js';
  17. import { d as defineCustomElement$5 } from './loader.js';
  18. import { d as defineCustomElement$4 } from './popover.js';
  19. import { d as defineCustomElement$3 } from './progress.js';
  20. const inputTimePickerCss = "@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}}: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{display:inline-block;-webkit-user-select:none;user-select:none}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}::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}";
  21. const InputTimePicker = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  22. constructor() {
  23. super();
  24. this.__registerHost();
  25. this.__attachShadow();
  26. this.calciteInputTimePickerChange = createEvent(this, "calciteInputTimePickerChange", 7);
  27. //--------------------------------------------------------------------------
  28. //
  29. // Properties
  30. //
  31. //--------------------------------------------------------------------------
  32. /**
  33. * When `true`, the component is active.
  34. *
  35. * @deprecated Use `open` instead.
  36. */
  37. this.active = false;
  38. /** When `true`, displays the `calcite-time-picker` component. */
  39. this.open = false;
  40. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  41. this.disabled = false;
  42. /**
  43. * When `true`, the component's value can be read, but controls are not accessible and the value cannot be modified.
  44. *
  45. * @mdn [readOnly](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly)
  46. */
  47. this.readOnly = false;
  48. /**
  49. * When `true`, the component must have a value in order for the form to submit.
  50. *
  51. * @internal
  52. */
  53. this.required = false;
  54. /** Specifies the size of the component. */
  55. this.scale = "m";
  56. /**
  57. * Determines the type of positioning to use for the overlaid content.
  58. *
  59. * Using `"absolute"` will work for most cases. The component will be positioned inside of overflowing parent containers and will affect the container's layout.
  60. *
  61. * `"fixed"` should be used to escape an overflowing parent container, or when the reference element's `position` CSS property is `"fixed"`.
  62. *
  63. */
  64. this.overlayPositioning = "absolute";
  65. /**
  66. * Determines where the popover will be positioned relative to the input.
  67. *
  68. * @see [LogicalPlacement](https://github.com/Esri/calcite-components/blob/master/src/utils/floating-ui.ts#L25)
  69. */
  70. this.placement = "auto";
  71. /** Specifies the granularity the component's `value` must adhere to (in seconds). */
  72. this.step = 60;
  73. /** The component's value in UTC (always 24-hour format). */
  74. this.value = null;
  75. /** whether the value of the input was changed as a result of user typing or not */
  76. this.internalValueChange = false;
  77. this.previousValidValue = null;
  78. this.referenceElementId = `input-time-picker-${guid()}`;
  79. //--------------------------------------------------------------------------
  80. //
  81. // State
  82. //
  83. //--------------------------------------------------------------------------
  84. this.effectiveLocale = "";
  85. //--------------------------------------------------------------------------
  86. //
  87. // Event Listeners
  88. //
  89. //--------------------------------------------------------------------------
  90. this.calciteInternalInputBlurHandler = () => {
  91. this.open = false;
  92. const shouldIncludeSeconds = this.shouldIncludeSeconds();
  93. const { effectiveLocale: locale, numberingSystem, value, calciteInputEl } = this;
  94. numberStringFormatter.numberFormatOptions = {
  95. locale,
  96. numberingSystem,
  97. useGrouping: false
  98. };
  99. const delocalizedValue = numberStringFormatter.delocalize(calciteInputEl.value);
  100. const localizedInputValue = localizeTimeString({
  101. value: delocalizedValue,
  102. includeSeconds: shouldIncludeSeconds,
  103. locale,
  104. numberingSystem
  105. });
  106. this.setInputValue(localizedInputValue ||
  107. localizeTimeString({ value, locale, numberingSystem, includeSeconds: shouldIncludeSeconds }));
  108. };
  109. this.calciteInternalInputFocusHandler = (event) => {
  110. if (!this.readOnly) {
  111. this.open = true;
  112. event.stopPropagation();
  113. }
  114. };
  115. this.calciteInputInputHandler = (event) => {
  116. const target = event.target;
  117. numberStringFormatter.numberFormatOptions = {
  118. locale: this.effectiveLocale,
  119. numberingSystem: this.numberingSystem,
  120. useGrouping: false
  121. };
  122. const delocalizedValue = numberStringFormatter.delocalize(target.value);
  123. this.setValue({ value: delocalizedValue });
  124. // only translate the numerals until blur
  125. const localizedValue = delocalizedValue
  126. .split("")
  127. .map((char) => numberKeys.includes(char)
  128. ? numberStringFormatter.numberFormatter.format(Number(char))
  129. : char)
  130. .join("");
  131. this.setInputValue(localizedValue);
  132. };
  133. this.timePickerChangeHandler = (event) => {
  134. event.stopPropagation();
  135. const target = event.target;
  136. const value = target.value;
  137. this.setValue({ value, origin: "time-picker" });
  138. };
  139. // --------------------------------------------------------------------------
  140. //
  141. // Private Methods
  142. //
  143. // --------------------------------------------------------------------------
  144. this.keyDownHandler = (event) => {
  145. const { defaultPrevented, key } = event;
  146. if (defaultPrevented) {
  147. return;
  148. }
  149. if (key === "Enter") {
  150. if (submitForm(this)) {
  151. event.preventDefault();
  152. }
  153. }
  154. if (key === "Escape" && this.open) {
  155. this.open = false;
  156. event.preventDefault();
  157. }
  158. };
  159. this.setCalcitePopoverEl = (el) => {
  160. this.popoverEl = el;
  161. };
  162. this.setCalciteInputEl = (el) => {
  163. this.calciteInputEl = el;
  164. };
  165. this.setCalciteTimePickerEl = (el) => {
  166. this.calciteTimePickerEl = el;
  167. };
  168. this.setInputValue = (newInputValue) => {
  169. if (!this.calciteInputEl) {
  170. return;
  171. }
  172. this.calciteInputEl.value = newInputValue;
  173. };
  174. this.setValue = ({ value, origin = "input" }) => {
  175. const previousValue = this.value;
  176. const newValue = formatTimeString(value);
  177. const newLocalizedValue = localizeTimeString({
  178. value: newValue,
  179. locale: this.effectiveLocale,
  180. numberingSystem: this.numberingSystem,
  181. includeSeconds: this.shouldIncludeSeconds()
  182. });
  183. this.internalValueChange = origin !== "external" && origin !== "loading";
  184. const shouldEmit = origin !== "loading" &&
  185. origin !== "external" &&
  186. ((value !== this.previousValidValue && !value) ||
  187. !!(!this.previousValidValue && newValue) ||
  188. (newValue !== this.previousValidValue && newValue));
  189. if (value) {
  190. if (shouldEmit) {
  191. this.previousValidValue = newValue;
  192. }
  193. if (newValue && newValue !== this.value) {
  194. this.value = newValue;
  195. }
  196. this.localizedValue = newLocalizedValue;
  197. }
  198. else {
  199. this.value = value;
  200. this.localizedValue = null;
  201. }
  202. if (origin === "time-picker" || origin === "external") {
  203. this.setInputValue(newLocalizedValue);
  204. }
  205. if (shouldEmit) {
  206. const changeEvent = this.calciteInputTimePickerChange.emit();
  207. if (changeEvent.defaultPrevented) {
  208. this.internalValueChange = false;
  209. this.value = previousValue;
  210. this.setInputValue(previousValue);
  211. this.previousValidValue = previousValue;
  212. }
  213. else {
  214. this.previousValidValue = newValue;
  215. }
  216. }
  217. };
  218. }
  219. activeHandler(value) {
  220. this.open = value;
  221. }
  222. openHandler(value) {
  223. this.active = value;
  224. if (this.disabled || this.readOnly) {
  225. this.open = false;
  226. return;
  227. }
  228. if (value) {
  229. this.reposition(true);
  230. }
  231. }
  232. handleDisabledAndReadOnlyChange(value) {
  233. if (!value) {
  234. this.open = false;
  235. }
  236. }
  237. localeChanged() {
  238. updateEffectiveLocale(this);
  239. }
  240. valueWatcher(newValue) {
  241. if (!this.internalValueChange) {
  242. this.setValue({ value: newValue, origin: "external" });
  243. }
  244. this.internalValueChange = false;
  245. }
  246. effectiveLocaleWatcher() {
  247. this.setInputValue(localizeTimeString({
  248. value: this.value,
  249. locale: this.effectiveLocale,
  250. numberingSystem: this.numberingSystem,
  251. includeSeconds: this.shouldIncludeSeconds()
  252. }));
  253. }
  254. clickHandler(event) {
  255. if (event.composedPath().includes(this.calciteTimePickerEl)) {
  256. return;
  257. }
  258. this.setFocus();
  259. }
  260. timePickerBlurHandler(event) {
  261. event.preventDefault();
  262. event.stopPropagation();
  263. this.open = false;
  264. }
  265. timePickerFocusHandler(event) {
  266. event.preventDefault();
  267. event.stopPropagation();
  268. if (!this.readOnly) {
  269. this.open = true;
  270. }
  271. }
  272. // --------------------------------------------------------------------------
  273. //
  274. // Public Methods
  275. //
  276. // --------------------------------------------------------------------------
  277. /** Sets focus on the component. */
  278. async setFocus() {
  279. var _a;
  280. (_a = this.calciteInputEl) === null || _a === void 0 ? void 0 : _a.setFocus();
  281. }
  282. /**
  283. * Updates the position of the component.
  284. *
  285. * @param delayed
  286. */
  287. async reposition(delayed = false) {
  288. var _a;
  289. (_a = this.popoverEl) === null || _a === void 0 ? void 0 : _a.reposition(delayed);
  290. }
  291. onLabelClick() {
  292. this.setFocus();
  293. }
  294. shouldIncludeSeconds() {
  295. return this.step < 60;
  296. }
  297. //--------------------------------------------------------------------------
  298. //
  299. // Lifecycle
  300. //
  301. //--------------------------------------------------------------------------
  302. connectedCallback() {
  303. connectLocalized(this);
  304. const { active, open } = this;
  305. if (this.value) {
  306. this.setValue({ value: isValidTime(this.value) ? this.value : undefined, origin: "loading" });
  307. }
  308. connectLabel(this);
  309. connectForm(this);
  310. if (open) {
  311. this.active = open;
  312. }
  313. else if (active) {
  314. this.open = active;
  315. }
  316. }
  317. componentDidLoad() {
  318. this.setInputValue(this.localizedValue);
  319. }
  320. disconnectedCallback() {
  321. disconnectLabel(this);
  322. disconnectForm(this);
  323. disconnectLocalized(this);
  324. }
  325. componentDidRender() {
  326. updateHostInteraction(this);
  327. }
  328. // --------------------------------------------------------------------------
  329. //
  330. // Render Methods
  331. //
  332. // --------------------------------------------------------------------------
  333. render() {
  334. const popoverId = `${this.referenceElementId}-popover`;
  335. return (h(Host, { onKeyDown: this.keyDownHandler }, h("div", { "aria-controls": popoverId, "aria-haspopup": "dialog", "aria-label": this.name, "aria-owns": popoverId, id: this.referenceElementId, role: "combobox" }, h("calcite-input", { disabled: this.disabled, icon: "clock", label: getLabelText(this), onCalciteInputInput: this.calciteInputInputHandler, onCalciteInternalInputBlur: this.calciteInternalInputBlurHandler, onCalciteInternalInputFocus: this.calciteInternalInputFocusHandler, readOnly: this.readOnly, ref: this.setCalciteInputEl, scale: this.scale, step: this.step })), h("calcite-popover", { id: popoverId, label: "Time Picker", open: this.open, overlayPositioning: this.overlayPositioning, placement: this.placement, ref: this.setCalcitePopoverEl, referenceElement: this.referenceElementId, triggerDisabled: true }, h("calcite-time-picker", { intlHour: this.intlHour, intlHourDown: this.intlHourDown, intlHourUp: this.intlHourUp, intlMeridiem: this.intlMeridiem, intlMeridiemDown: this.intlMeridiemDown, intlMeridiemUp: this.intlMeridiemUp, intlMinute: this.intlMinute, intlMinuteDown: this.intlMinuteDown, intlMinuteUp: this.intlMinuteUp, intlSecond: this.intlSecond, intlSecondDown: this.intlSecondDown, intlSecondUp: this.intlSecondUp, lang: this.effectiveLocale, numberingSystem: this.numberingSystem, onCalciteInternalTimePickerChange: this.timePickerChangeHandler, ref: this.setCalciteTimePickerEl, scale: this.scale, step: this.step, value: this.value })), h(HiddenFormInputSlot, { component: this })));
  336. }
  337. get el() { return this; }
  338. static get watchers() { return {
  339. "active": ["activeHandler"],
  340. "open": ["openHandler"],
  341. "disabled": ["handleDisabledAndReadOnlyChange"],
  342. "readOnly": ["handleDisabledAndReadOnlyChange"],
  343. "locale": ["localeChanged"],
  344. "value": ["valueWatcher"],
  345. "effectiveLocale": ["effectiveLocaleWatcher"]
  346. }; }
  347. static get style() { return inputTimePickerCss; }
  348. }, [1, "calcite-input-time-picker", {
  349. "active": [1540],
  350. "open": [1540],
  351. "disabled": [516],
  352. "readOnly": [516, "read-only"],
  353. "intlHour": [1, "intl-hour"],
  354. "intlHourDown": [1, "intl-hour-down"],
  355. "intlHourUp": [1, "intl-hour-up"],
  356. "intlMeridiem": [1, "intl-meridiem"],
  357. "intlMeridiemDown": [1, "intl-meridiem-down"],
  358. "intlMeridiemUp": [1, "intl-meridiem-up"],
  359. "intlMinute": [1, "intl-minute"],
  360. "intlMinuteDown": [1, "intl-minute-down"],
  361. "intlMinuteUp": [1, "intl-minute-up"],
  362. "intlSecond": [1, "intl-second"],
  363. "intlSecondDown": [1, "intl-second-down"],
  364. "intlSecondUp": [1, "intl-second-up"],
  365. "locale": [1025],
  366. "name": [1],
  367. "numberingSystem": [1, "numbering-system"],
  368. "required": [516],
  369. "scale": [513],
  370. "overlayPositioning": [1, "overlay-positioning"],
  371. "placement": [513],
  372. "step": [2],
  373. "value": [1025],
  374. "effectiveLocale": [32],
  375. "localizedValue": [32],
  376. "setFocus": [64],
  377. "reposition": [64]
  378. }, [[0, "click", "clickHandler"], [0, "calciteInternalTimePickerBlur", "timePickerBlurHandler"], [0, "calciteInternalTimePickerFocus", "timePickerFocusHandler"]]]);
  379. function defineCustomElement$1() {
  380. if (typeof customElements === "undefined") {
  381. return;
  382. }
  383. const components = ["calcite-input-time-picker", "calcite-action", "calcite-icon", "calcite-input", "calcite-loader", "calcite-popover", "calcite-progress", "calcite-time-picker"];
  384. components.forEach(tagName => { switch (tagName) {
  385. case "calcite-input-time-picker":
  386. if (!customElements.get(tagName)) {
  387. customElements.define(tagName, InputTimePicker);
  388. }
  389. break;
  390. case "calcite-action":
  391. if (!customElements.get(tagName)) {
  392. defineCustomElement$8();
  393. }
  394. break;
  395. case "calcite-icon":
  396. if (!customElements.get(tagName)) {
  397. defineCustomElement$7();
  398. }
  399. break;
  400. case "calcite-input":
  401. if (!customElements.get(tagName)) {
  402. defineCustomElement$6();
  403. }
  404. break;
  405. case "calcite-loader":
  406. if (!customElements.get(tagName)) {
  407. defineCustomElement$5();
  408. }
  409. break;
  410. case "calcite-popover":
  411. if (!customElements.get(tagName)) {
  412. defineCustomElement$4();
  413. }
  414. break;
  415. case "calcite-progress":
  416. if (!customElements.get(tagName)) {
  417. defineCustomElement$3();
  418. }
  419. break;
  420. case "calcite-time-picker":
  421. if (!customElements.get(tagName)) {
  422. defineCustomElement$2();
  423. }
  424. break;
  425. } });
  426. }
  427. defineCustomElement$1();
  428. const CalciteInputTimePicker = InputTimePicker;
  429. const defineCustomElement = defineCustomElement$1;
  430. export { CalciteInputTimePicker, defineCustomElement };