calcite-input-time-picker.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
  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 './label.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 { d as defineCustomElement$8 } from './action.js';
  13. import { d as defineCustomElement$7 } from './icon.js';
  14. import { d as defineCustomElement$6 } from './input.js';
  15. import { d as defineCustomElement$5 } from './loader.js';
  16. import { d as defineCustomElement$4 } from './popover.js';
  17. import { d as defineCustomElement$3 } from './progress.js';
  18. const inputTimePickerCss = "@-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}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:inline-block;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}: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}::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}";
  19. const InputTimePicker = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  20. constructor() {
  21. super();
  22. this.__registerHost();
  23. this.__attachShadow();
  24. this.calciteInputTimePickerChange = createEvent(this, "calciteInputTimePickerChange", 7);
  25. //--------------------------------------------------------------------------
  26. //
  27. // Properties
  28. //
  29. //--------------------------------------------------------------------------
  30. /** The active state of the time input */
  31. this.active = false;
  32. /** The disabled state of the time input */
  33. this.disabled = false;
  34. /**
  35. * BCP 47 language tag for desired language and country format
  36. * @internal
  37. */
  38. this.locale = document.documentElement.lang || navigator.language || "en";
  39. /**
  40. * When true, makes the component required for form-submission.
  41. *
  42. * @internal
  43. */
  44. this.required = false;
  45. /** The scale (size) of the time input */
  46. this.scale = "m";
  47. /**
  48. * Determines where the popover will be positioned relative to the input.
  49. * @see [PopperPlacement](https://github.com/Esri/calcite-components/blob/master/src/utils/popper.ts#L25)
  50. */
  51. this.placement = "auto";
  52. /** number (seconds) that specifies the granularity that the value must adhere to */
  53. this.step = 60;
  54. /** The selected time in UTC (always 24-hour format) */
  55. this.value = null;
  56. /** whether the value of the input was changed as a result of user typing or not */
  57. this.internalValueChange = false;
  58. this.previousValidValue = null;
  59. this.referenceElementId = `input-time-picker-${guid()}`;
  60. //--------------------------------------------------------------------------
  61. //
  62. // Event Listeners
  63. //
  64. //--------------------------------------------------------------------------
  65. this.calciteInputBlurHandler = () => {
  66. this.active = false;
  67. const shouldIncludeSeconds = this.shouldIncludeSeconds();
  68. const localizedInputValue = localizeTimeString(this.calciteInputEl.value, this.locale, shouldIncludeSeconds);
  69. this.setInputValue(localizedInputValue || localizeTimeString(this.value, this.locale, shouldIncludeSeconds));
  70. };
  71. this.calciteInputFocusHandler = () => {
  72. this.active = true;
  73. };
  74. this.calciteInputInputHandler = (event) => {
  75. const target = event.target;
  76. this.setValue({ value: target.value });
  77. };
  78. this.timePickerChangeHandler = (event) => {
  79. event.stopPropagation();
  80. const target = event.target;
  81. const value = target.value;
  82. this.setValue({ value, origin: "time-picker" });
  83. };
  84. // --------------------------------------------------------------------------
  85. //
  86. // Private Methods
  87. //
  88. // --------------------------------------------------------------------------
  89. this.keyDownHandler = (event) => {
  90. if (event.key === "Enter" && !event.defaultPrevented) {
  91. submitForm(this);
  92. }
  93. };
  94. this.setCalcitePopoverEl = (el) => {
  95. this.popoverEl = el;
  96. };
  97. this.setCalciteInputEl = (el) => {
  98. this.calciteInputEl = el;
  99. };
  100. this.setCalciteTimePickerEl = (el) => {
  101. this.calciteTimePickerEl = el;
  102. };
  103. this.setInputValue = (newInputValue) => {
  104. if (!this.calciteInputEl) {
  105. return;
  106. }
  107. this.calciteInputEl.value = newInputValue;
  108. };
  109. this.setValue = ({ value, origin = "input" }) => {
  110. const previousValue = this.value;
  111. const newValue = formatTimeString(value);
  112. const newLocalizedValue = localizeTimeString(newValue, this.locale, this.shouldIncludeSeconds());
  113. this.internalValueChange = origin !== "external" && origin !== "loading";
  114. const shouldEmit = origin !== "loading" &&
  115. origin !== "external" &&
  116. ((value !== this.previousValidValue && !value) ||
  117. !!(!this.previousValidValue && newValue) ||
  118. (newValue !== this.previousValidValue && newValue));
  119. if (value) {
  120. if (shouldEmit) {
  121. this.previousValidValue = newValue;
  122. }
  123. if (newValue && newValue !== this.value) {
  124. this.value = newValue;
  125. }
  126. this.localizedValue = newLocalizedValue;
  127. }
  128. else {
  129. this.value = value;
  130. this.localizedValue = null;
  131. }
  132. if (origin === "time-picker" || origin === "external") {
  133. this.setInputValue(newLocalizedValue);
  134. }
  135. if (shouldEmit) {
  136. const changeEvent = this.calciteInputTimePickerChange.emit();
  137. if (changeEvent.defaultPrevented) {
  138. this.internalValueChange = false;
  139. this.value = previousValue;
  140. this.setInputValue(previousValue);
  141. this.previousValidValue = previousValue;
  142. }
  143. else {
  144. this.previousValidValue = newValue;
  145. }
  146. }
  147. };
  148. }
  149. activeHandler() {
  150. if (this.disabled) {
  151. this.active = false;
  152. return;
  153. }
  154. this.reposition();
  155. }
  156. handleDisabledChange(value) {
  157. if (!value) {
  158. this.active = false;
  159. }
  160. }
  161. localeWatcher(newLocale) {
  162. this.setInputValue(localizeTimeString(this.value, newLocale, this.shouldIncludeSeconds()));
  163. }
  164. valueWatcher(newValue) {
  165. if (!this.internalValueChange) {
  166. this.setValue({ value: newValue, origin: "external" });
  167. }
  168. this.internalValueChange = false;
  169. }
  170. clickHandler(event) {
  171. if (event.composedPath().includes(this.calciteTimePickerEl)) {
  172. return;
  173. }
  174. this.setFocus();
  175. }
  176. keyUpHandler(event) {
  177. if (event.key === "Escape" && this.active) {
  178. this.active = false;
  179. }
  180. }
  181. timePickerBlurHandler(event) {
  182. event.preventDefault();
  183. event.stopPropagation();
  184. this.active = false;
  185. }
  186. timePickerFocusHandler(event) {
  187. event.preventDefault();
  188. event.stopPropagation();
  189. this.active = true;
  190. }
  191. // --------------------------------------------------------------------------
  192. //
  193. // Public Methods
  194. //
  195. // --------------------------------------------------------------------------
  196. /** Sets focus on the component. */
  197. async setFocus() {
  198. this.calciteInputEl.setFocus();
  199. }
  200. /** Updates the position of the component. */
  201. async reposition() {
  202. var _a;
  203. (_a = this.popoverEl) === null || _a === void 0 ? void 0 : _a.reposition();
  204. }
  205. onLabelClick() {
  206. this.setFocus();
  207. }
  208. shouldIncludeSeconds() {
  209. return this.step < 60;
  210. }
  211. //--------------------------------------------------------------------------
  212. //
  213. // Lifecycle
  214. //
  215. //--------------------------------------------------------------------------
  216. connectedCallback() {
  217. if (this.value) {
  218. this.setValue({ value: isValidTime(this.value) ? this.value : undefined, origin: "loading" });
  219. }
  220. connectLabel(this);
  221. connectForm(this);
  222. }
  223. componentDidLoad() {
  224. this.setInputValue(this.localizedValue);
  225. }
  226. disconnectedCallback() {
  227. disconnectLabel(this);
  228. disconnectForm(this);
  229. }
  230. componentDidRender() {
  231. updateHostInteraction(this);
  232. }
  233. // --------------------------------------------------------------------------
  234. //
  235. // Render Methods
  236. //
  237. // --------------------------------------------------------------------------
  238. render() {
  239. const popoverId = `${this.referenceElementId}-popover`;
  240. 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), onCalciteInputBlur: this.calciteInputBlurHandler, onCalciteInputFocus: this.calciteInputFocusHandler, onCalciteInputInput: this.calciteInputInputHandler, ref: this.setCalciteInputEl, scale: this.scale, step: this.step })), h("calcite-popover", { id: popoverId, label: "Time Picker", open: this.active, placement: this.placement, ref: this.setCalcitePopoverEl, referenceElement: this.referenceElementId }, 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.locale, onCalciteTimePickerChange: this.timePickerChangeHandler, ref: this.setCalciteTimePickerEl, scale: this.scale, step: this.step, value: this.value })), h(HiddenFormInputSlot, { component: this })));
  241. }
  242. get el() { return this; }
  243. static get watchers() { return {
  244. "active": ["activeHandler"],
  245. "disabled": ["handleDisabledChange"],
  246. "locale": ["localeWatcher"],
  247. "value": ["valueWatcher"]
  248. }; }
  249. static get style() { return inputTimePickerCss; }
  250. }, [1, "calcite-input-time-picker", {
  251. "active": [1540],
  252. "disabled": [516],
  253. "intlHour": [1, "intl-hour"],
  254. "intlHourDown": [1, "intl-hour-down"],
  255. "intlHourUp": [1, "intl-hour-up"],
  256. "intlMeridiem": [1, "intl-meridiem"],
  257. "intlMeridiemDown": [1, "intl-meridiem-down"],
  258. "intlMeridiemUp": [1, "intl-meridiem-up"],
  259. "intlMinute": [1, "intl-minute"],
  260. "intlMinuteDown": [1, "intl-minute-down"],
  261. "intlMinuteUp": [1, "intl-minute-up"],
  262. "intlSecond": [1, "intl-second"],
  263. "intlSecondDown": [1, "intl-second-down"],
  264. "intlSecondUp": [1, "intl-second-up"],
  265. "locale": [1025, "lang"],
  266. "name": [1],
  267. "required": [516],
  268. "scale": [513],
  269. "placement": [513],
  270. "step": [2],
  271. "value": [1025],
  272. "localizedValue": [32],
  273. "setFocus": [64],
  274. "reposition": [64]
  275. }, [[0, "click", "clickHandler"], [0, "keyup", "keyUpHandler"], [0, "calciteTimePickerBlur", "timePickerBlurHandler"], [0, "calciteTimePickerFocus", "timePickerFocusHandler"]]]);
  276. function defineCustomElement$1() {
  277. if (typeof customElements === "undefined") {
  278. return;
  279. }
  280. const components = ["calcite-input-time-picker", "calcite-action", "calcite-icon", "calcite-input", "calcite-loader", "calcite-popover", "calcite-progress", "calcite-time-picker"];
  281. components.forEach(tagName => { switch (tagName) {
  282. case "calcite-input-time-picker":
  283. if (!customElements.get(tagName)) {
  284. customElements.define(tagName, InputTimePicker);
  285. }
  286. break;
  287. case "calcite-action":
  288. if (!customElements.get(tagName)) {
  289. defineCustomElement$8();
  290. }
  291. break;
  292. case "calcite-icon":
  293. if (!customElements.get(tagName)) {
  294. defineCustomElement$7();
  295. }
  296. break;
  297. case "calcite-input":
  298. if (!customElements.get(tagName)) {
  299. defineCustomElement$6();
  300. }
  301. break;
  302. case "calcite-loader":
  303. if (!customElements.get(tagName)) {
  304. defineCustomElement$5();
  305. }
  306. break;
  307. case "calcite-popover":
  308. if (!customElements.get(tagName)) {
  309. defineCustomElement$4();
  310. }
  311. break;
  312. case "calcite-progress":
  313. if (!customElements.get(tagName)) {
  314. defineCustomElement$3();
  315. }
  316. break;
  317. case "calcite-time-picker":
  318. if (!customElements.get(tagName)) {
  319. defineCustomElement$2();
  320. }
  321. break;
  322. } });
  323. }
  324. defineCustomElement$1();
  325. const CalciteInputTimePicker = InputTimePicker;
  326. const defineCustomElement = defineCustomElement$1;
  327. export { CalciteInputTimePicker, defineCustomElement };