date-picker-month-header.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, Fragment } from '@stencil/core/internal/client';
  7. import { l as localizeNumber, c as getOrder, b as dateFromRange, n as nextMonth, p as prevMonth, e as parseNumber } from './date.js';
  8. import { H as Heading } from './Heading.js';
  9. import { d as defineCustomElement$1 } from './icon.js';
  10. const datePickerMonthHeaderCss = "@-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:block}.header{display:-ms-flexbox;display:flex;-ms-flex-pack:justify;justify-content:space-between;padding-top:0px;padding-bottom:0px;padding-left:0.25rem;padding-right:0.25rem}:host([scale=s]) .text{margin-top:0.5rem;margin-bottom:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=s]) .chevron{height:2.25rem}:host([scale=m]) .text{margin-top:0.75rem;margin-bottom:0.75rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=m]) .chevron{height:3rem}:host([scale=l]) .text{margin-top:1rem;margin-bottom:1rem;font-size:var(--calcite-font-size-1);line-height:1.5rem}:host([scale=l]) .chevron{height:3.5rem}.chevron{margin-left:-0.25rem;margin-right:-0.25rem;-webkit-box-sizing:content-box;box-sizing:content-box;display:-ms-flexbox;display:flex;-ms-flex-positive:0;flex-grow:0;cursor:pointer;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;border-style:none;background-color:var(--calcite-ui-foreground-1);padding-left:0.25rem;padding-right:0.25rem;color:var(--calcite-ui-text-3);outline:2px solid transparent;outline-offset:2px;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:150ms;transition-duration:150ms;-webkit-transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;width:14.2857142857%}.chevron:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.chevron:hover,.chevron:focus{background-color:var(--calcite-ui-foreground-2);fill:var(--calcite-ui-text-1);color:var(--calcite-ui-text-1)}.chevron:active{background-color:var(--calcite-ui-foreground-3)}.chevron[aria-disabled=true]{pointer-events:none;opacity:0}.text{margin-top:auto;margin-bottom:auto;width:100%;-ms-flex:1 1 auto;flex:1 1 auto;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;text-align:center;line-height:1}.text-nreverse{-ms-flex-direction:row-reverse;flex-direction:row-reverse}.month,.year,.suffix{margin-left:0.25rem;margin-right:0.25rem;margin-top:auto;margin-bottom:auto;display:inline-block;background-color:var(--calcite-ui-foreground-1);font-weight:var(--calcite-font-weight-medium);line-height:1.25;color:var(--calcite-ui-text-1);font-size:inherit}.year{position:relative;width:3rem;border-style:none;background-color:transparent;text-align:center;font-family:inherit;outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;z-index:2}.year:hover{-webkit-transition-duration:100ms;transition-duration:100ms;-webkit-transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);-webkit-transition-property:outline-color;transition-property:outline-color;outline:2px solid var(--calcite-ui-border-2);outline-offset:2px}.year:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}.year--suffix{width:4rem;text-align:left}.year-wrap{position:relative}.suffix{position:absolute;top:0px;left:0px;width:4rem;white-space:nowrap;text-align:left}.suffix__invisible{visibility:hidden}";
  11. const DatePickerMonthHeader = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  12. constructor() {
  13. super();
  14. this.__registerHost();
  15. this.__attachShadow();
  16. this.calciteDatePickerSelect = createEvent(this, "calciteDatePickerSelect", 7);
  17. //--------------------------------------------------------------------------
  18. //
  19. // Private Methods
  20. //
  21. //--------------------------------------------------------------------------
  22. /**
  23. * Increment year on UP/DOWN keys
  24. */
  25. this.onYearKey = (e) => {
  26. const localizedYear = e.target.value;
  27. switch (e.key) {
  28. case "ArrowDown":
  29. e.preventDefault();
  30. this.setYear({ localizedYear, offset: -1 });
  31. break;
  32. case "ArrowUp":
  33. e.preventDefault();
  34. this.setYear({ localizedYear, offset: 1 });
  35. break;
  36. }
  37. };
  38. this.onYearChange = (event) => {
  39. this.setYear({ localizedYear: event.target.value });
  40. };
  41. this.onYearInput = (event) => {
  42. this.setYear({ localizedYear: event.target.value, commit: false });
  43. };
  44. this.prevMonthClick = (e) => {
  45. this.handleArrowClick(e, this.prevMonthDate);
  46. };
  47. this.prevMonthKeydown = (e) => {
  48. const key = e.key;
  49. if (key === " " || key === "Enter") {
  50. this.prevMonthClick(e);
  51. }
  52. };
  53. this.nextMonthClick = (e) => {
  54. this.handleArrowClick(e, this.nextMonthDate);
  55. };
  56. this.nextMonthKeydown = (e) => {
  57. const key = e.key;
  58. if (key === " " || key === "Enter") {
  59. this.nextMonthClick(e);
  60. }
  61. };
  62. /*
  63. * Update active month on clicks of left/right arrows
  64. */
  65. this.handleArrowClick = (e, date) => {
  66. e === null || e === void 0 ? void 0 : e.preventDefault();
  67. e.stopPropagation();
  68. this.calciteDatePickerSelect.emit(date);
  69. };
  70. }
  71. //--------------------------------------------------------------------------
  72. //
  73. // Lifecycle
  74. //
  75. //--------------------------------------------------------------------------
  76. connectedCallback() {
  77. this.setNextPrevMonthDates();
  78. }
  79. render() {
  80. return h("div", { class: "header" }, this.renderContent());
  81. }
  82. renderContent() {
  83. var _a;
  84. if (!this.activeDate || !this.localeData) {
  85. return null;
  86. }
  87. const activeMonth = this.activeDate.getMonth();
  88. const { months, unitOrder } = this.localeData;
  89. const localizedMonth = (months.wide || months.narrow || months.abbreviated)[activeMonth];
  90. const localizedYear = localizeNumber(this.activeDate.getFullYear(), this.localeData);
  91. const iconScale = this.scale === "l" ? "m" : "s";
  92. const order = getOrder(unitOrder);
  93. const reverse = order.indexOf("y") < order.indexOf("m");
  94. const suffix = (_a = this.localeData.year) === null || _a === void 0 ? void 0 : _a.suffix;
  95. return (h(Fragment, null, h("a", { "aria-disabled": (this.prevMonthDate.getMonth() === activeMonth).toString(), "aria-label": this.intlPrevMonth, class: "chevron", href: "#", onClick: this.prevMonthClick, onKeyDown: this.prevMonthKeydown, role: "button", tabindex: this.prevMonthDate.getMonth() === activeMonth ? -1 : 0 }, h("calcite-icon", { "flip-rtl": true, icon: "chevron-left", scale: iconScale })), h("div", { class: { text: true, "text--reverse": reverse } }, h(Heading, { class: "month", level: this.headingLevel }, localizedMonth), h("span", { class: "year-wrap" }, h("input", { "aria-label": this.intlYear, class: {
  96. year: true,
  97. "year--suffix": !!suffix
  98. }, inputmode: "numeric", maxlength: "4", minlength: "1", onChange: this.onYearChange, onInput: this.onYearInput, onKeyDown: this.onYearKey, pattern: "\\d*", ref: (el) => (this.yearInput = el), type: "text", value: localizedYear }), suffix && (h("span", { class: "suffix" }, h("span", { "aria-hidden": "true", class: "suffix__invisible" }, localizedYear), " " + suffix)))), h("a", { "aria-disabled": (this.nextMonthDate.getMonth() === activeMonth).toString(), "aria-label": this.intlNextMonth, class: "chevron", href: "#", onClick: this.nextMonthClick, onKeyDown: this.nextMonthKeydown, role: "button", tabindex: this.nextMonthDate.getMonth() === activeMonth ? -1 : 0 }, h("calcite-icon", { "flip-rtl": true, icon: "chevron-right", scale: iconScale }))));
  99. }
  100. setNextPrevMonthDates() {
  101. if (!this.activeDate) {
  102. return;
  103. }
  104. this.nextMonthDate = dateFromRange(nextMonth(this.activeDate), this.min, this.max);
  105. this.prevMonthDate = dateFromRange(prevMonth(this.activeDate), this.min, this.max);
  106. }
  107. getInRangeDate({ localizedYear, offset = 0 }) {
  108. const { min, max, activeDate, localeData } = this;
  109. const parsedYear = parseNumber(localizedYear, localeData);
  110. const length = parsedYear.toString().length;
  111. const year = isNaN(parsedYear) ? false : parsedYear + offset;
  112. const inRange = year && (!min || min.getFullYear() <= year) && (!max || max.getFullYear() >= year);
  113. // if you've supplied a year and it's in range
  114. if (year && inRange && length === localizedYear.length) {
  115. const nextDate = new Date(activeDate);
  116. nextDate.setFullYear(year);
  117. return dateFromRange(nextDate, min, max);
  118. }
  119. }
  120. /**
  121. * Parse localized year string from input,
  122. * set to active if in range
  123. */
  124. setYear({ localizedYear, commit = true, offset = 0 }) {
  125. const { yearInput, activeDate, localeData } = this;
  126. const inRangeDate = this.getInRangeDate({ localizedYear, offset });
  127. // if you've supplied a year and it's in range, update active date
  128. if (inRangeDate) {
  129. this.calciteDatePickerSelect.emit(inRangeDate);
  130. }
  131. if (commit) {
  132. yearInput.value = localizeNumber((inRangeDate || activeDate).getFullYear(), localeData);
  133. }
  134. }
  135. get el() { return this; }
  136. static get watchers() { return {
  137. "min": ["setNextPrevMonthDates"],
  138. "max": ["setNextPrevMonthDates"],
  139. "activeDate": ["setNextPrevMonthDates"]
  140. }; }
  141. static get style() { return datePickerMonthHeaderCss; }
  142. }, [1, "calcite-date-picker-month-header", {
  143. "selectedDate": [16],
  144. "activeDate": [16],
  145. "headingLevel": [2, "heading-level"],
  146. "min": [16],
  147. "max": [16],
  148. "locale": [1],
  149. "intlPrevMonth": [1, "intl-prev-month"],
  150. "intlNextMonth": [1, "intl-next-month"],
  151. "intlYear": [1, "intl-year"],
  152. "scale": [513],
  153. "localeData": [16],
  154. "nextMonthDate": [32],
  155. "prevMonthDate": [32]
  156. }]);
  157. function defineCustomElement() {
  158. if (typeof customElements === "undefined") {
  159. return;
  160. }
  161. const components = ["calcite-date-picker-month-header", "calcite-icon"];
  162. components.forEach(tagName => { switch (tagName) {
  163. case "calcite-date-picker-month-header":
  164. if (!customElements.get(tagName)) {
  165. customElements.define(tagName, DatePickerMonthHeader);
  166. }
  167. break;
  168. case "calcite-icon":
  169. if (!customElements.get(tagName)) {
  170. defineCustomElement$1();
  171. }
  172. break;
  173. } });
  174. }
  175. defineCustomElement();
  176. export { DatePickerMonthHeader as D, defineCustomElement as d };