date-picker-month-header.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, Fragment } from '@stencil/core/internal/client/index.js';
  7. import { e as getOrder, b as dateFromRange, n as nextMonth, f as prevMonth } from './date.js';
  8. import { H as Heading } from './Heading.js';
  9. import { i as isActivationKey } from './key.js';
  10. import { n as numberStringFormatter } from './locale.js';
  11. import { o as closestElementCrossShadowBoundary } from './dom.js';
  12. import { d as defineCustomElement$1 } from './icon.js';
  13. const BUDDHIST_CALENDAR_YEAR_OFFSET = 543;
  14. const datePickerMonthHeaderCss = "@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{display:block}.header{display:flex;justify-content:space-between;padding-block:0px;padding-inline:0.25rem}:host([scale=s]) .text{margin-block:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=s]) .chevron{block-size:2.25rem}:host([scale=m]) .text{margin-block:0.75rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=m]) .chevron{block-size:3rem}:host([scale=l]) .text{margin-block:1rem;font-size:var(--calcite-font-size-1);line-height:1.5rem}:host([scale=l]) .chevron{block-size:3.5rem}.chevron{margin-inline:-0.25rem;box-sizing:content-box;display:flex;flex-grow:0;cursor:pointer;align-items:center;justify-content:center;border-style:none;background-color:var(--calcite-ui-foreground-1);padding-inline:0.25rem;color:var(--calcite-ui-text-3);outline:2px solid transparent;outline-offset:2px;outline-color:transparent;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;inline-size: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-block:auto;display:flex;inline-size:100%;flex:1 1 auto;align-items:center;justify-content:center;text-align:center;line-height:1}.text--reverse{flex-direction:row-reverse}.month,.year,.suffix{margin-inline:0.25rem;margin-block: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;inline-size:2.5rem;border-style:none;background-color:transparent;text-align:center;font-family:inherit;outline-color:transparent}.year:hover{transition-duration:100ms;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);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{text-align:start}.year-wrap{position:relative}.suffix{inset-block-start:0px;white-space:nowrap;text-align:start;inset-inline-start:0}";
  15. const DatePickerMonthHeader = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  16. constructor() {
  17. super();
  18. this.__registerHost();
  19. this.__attachShadow();
  20. this.calciteDatePickerSelect = createEvent(this, "calciteDatePickerSelect", 6);
  21. //--------------------------------------------------------------------------
  22. //
  23. // Private State/Props
  24. //
  25. //--------------------------------------------------------------------------
  26. this.globalAttributes = {};
  27. //--------------------------------------------------------------------------
  28. //
  29. // Private Methods
  30. //
  31. //--------------------------------------------------------------------------
  32. /**
  33. * Increment year on UP/DOWN keys
  34. *
  35. * @param event
  36. */
  37. this.onYearKey = (event) => {
  38. const localizedYear = this.parseCalendarYear(event.target.value);
  39. switch (event.key) {
  40. case "ArrowDown":
  41. event.preventDefault();
  42. this.setYear({ localizedYear, offset: -1 });
  43. break;
  44. case "ArrowUp":
  45. event.preventDefault();
  46. this.setYear({ localizedYear, offset: 1 });
  47. break;
  48. }
  49. };
  50. this.onYearChange = (event) => {
  51. this.setYear({
  52. localizedYear: this.parseCalendarYear(event.target.value)
  53. });
  54. };
  55. this.onYearInput = (event) => {
  56. this.setYear({
  57. localizedYear: this.parseCalendarYear(event.target.value),
  58. commit: false
  59. });
  60. };
  61. this.prevMonthClick = (event) => {
  62. this.handleArrowClick(event, this.prevMonthDate);
  63. };
  64. this.prevMonthKeydown = (event) => {
  65. if (isActivationKey(event.key)) {
  66. this.prevMonthClick(event);
  67. }
  68. };
  69. this.nextMonthClick = (event) => {
  70. this.handleArrowClick(event, this.nextMonthDate);
  71. };
  72. this.nextMonthKeydown = (event) => {
  73. if (isActivationKey(event.key)) {
  74. this.nextMonthClick(event);
  75. }
  76. };
  77. /*
  78. * Update active month on clicks of left/right arrows
  79. */
  80. this.handleArrowClick = (event, date) => {
  81. event.preventDefault();
  82. this.calciteDatePickerSelect.emit(date);
  83. };
  84. }
  85. //--------------------------------------------------------------------------
  86. //
  87. // Lifecycle
  88. //
  89. //--------------------------------------------------------------------------
  90. componentWillLoad() {
  91. this.parentDatePickerEl = closestElementCrossShadowBoundary(this.el, "calcite-date-picker");
  92. }
  93. connectedCallback() {
  94. this.setNextPrevMonthDates();
  95. }
  96. render() {
  97. return h("div", { class: "header" }, this.renderContent());
  98. }
  99. renderContent() {
  100. var _a;
  101. if (!this.activeDate || !this.localeData) {
  102. return null;
  103. }
  104. if (this.parentDatePickerEl) {
  105. const { numberingSystem, lang: locale } = this.parentDatePickerEl;
  106. numberStringFormatter.numberFormatOptions = {
  107. useGrouping: false,
  108. ...(numberingSystem && { numberingSystem }),
  109. ...(locale && { locale })
  110. };
  111. }
  112. const activeMonth = this.activeDate.getMonth();
  113. const { months, unitOrder } = this.localeData;
  114. const localizedMonth = (months.wide || months.narrow || months.abbreviated)[activeMonth];
  115. const localizedYear = this.formatCalendarYear(this.activeDate.getFullYear());
  116. const iconScale = this.scale === "l" ? "m" : "s";
  117. const order = getOrder(unitOrder);
  118. const reverse = order.indexOf("y") < order.indexOf("m");
  119. const suffix = (_a = this.localeData.year) === null || _a === void 0 ? void 0 : _a.suffix;
  120. return (h(Fragment, null, h("a", { "aria-disabled": `${this.prevMonthDate.getMonth() === activeMonth}`, "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: {
  121. year: true,
  122. "year--suffix": !!suffix
  123. }, 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" }, suffix))), h("a", { "aria-disabled": `${this.nextMonthDate.getMonth() === activeMonth}`, "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 }))));
  124. }
  125. setNextPrevMonthDates() {
  126. if (!this.activeDate) {
  127. return;
  128. }
  129. this.nextMonthDate = dateFromRange(nextMonth(this.activeDate), this.min, this.max);
  130. this.prevMonthDate = dateFromRange(prevMonth(this.activeDate), this.min, this.max);
  131. }
  132. formatCalendarYear(year) {
  133. const { localeData } = this;
  134. const buddhistCalendar = localeData["default-calendar"] === "buddhist";
  135. const yearOffset = buddhistCalendar ? BUDDHIST_CALENDAR_YEAR_OFFSET : 0;
  136. return numberStringFormatter.localize(`${year + yearOffset}`);
  137. }
  138. parseCalendarYear(year) {
  139. const { localeData } = this;
  140. const buddhistCalendar = localeData["default-calendar"] === "buddhist";
  141. const yearOffset = buddhistCalendar ? BUDDHIST_CALENDAR_YEAR_OFFSET : 0;
  142. const parsedYear = Number(numberStringFormatter.delocalize(year)) - yearOffset;
  143. return numberStringFormatter.localize(`${parsedYear}`);
  144. }
  145. getInRangeDate({ localizedYear, offset = 0 }) {
  146. const { min, max, activeDate } = this;
  147. const parsedYear = Number(numberStringFormatter.delocalize(localizedYear));
  148. const length = parsedYear.toString().length;
  149. const year = isNaN(parsedYear) ? false : parsedYear + offset;
  150. const inRange = year && (!min || min.getFullYear() <= year) && (!max || max.getFullYear() >= year);
  151. // if you've supplied a year and it's in range
  152. if (year && inRange && length === localizedYear.length) {
  153. const nextDate = new Date(activeDate);
  154. nextDate.setFullYear(year);
  155. return dateFromRange(nextDate, min, max);
  156. }
  157. }
  158. /**
  159. * Parse localized year string from input,
  160. * set to active if in range
  161. *
  162. * @param root0
  163. * @param root0.localizedYear
  164. * @param root0.commit
  165. * @param root0.offset
  166. */
  167. setYear({ localizedYear, commit = true, offset = 0 }) {
  168. const { yearInput, activeDate } = this;
  169. const inRangeDate = this.getInRangeDate({ localizedYear, offset });
  170. // if you've supplied a year and it's in range, update active date
  171. if (inRangeDate) {
  172. this.calciteDatePickerSelect.emit(inRangeDate);
  173. }
  174. if (commit) {
  175. yearInput.value = this.formatCalendarYear((inRangeDate || activeDate).getFullYear());
  176. }
  177. }
  178. get el() { return this; }
  179. static get watchers() { return {
  180. "min": ["setNextPrevMonthDates"],
  181. "max": ["setNextPrevMonthDates"],
  182. "activeDate": ["setNextPrevMonthDates"]
  183. }; }
  184. static get style() { return datePickerMonthHeaderCss; }
  185. }, [1, "calcite-date-picker-month-header", {
  186. "selectedDate": [16],
  187. "activeDate": [16],
  188. "headingLevel": [2, "heading-level"],
  189. "min": [16],
  190. "max": [16],
  191. "intlPrevMonth": [1, "intl-prev-month"],
  192. "intlNextMonth": [1, "intl-next-month"],
  193. "intlYear": [1, "intl-year"],
  194. "scale": [513],
  195. "localeData": [16],
  196. "globalAttributes": [32],
  197. "nextMonthDate": [32],
  198. "prevMonthDate": [32]
  199. }]);
  200. function defineCustomElement() {
  201. if (typeof customElements === "undefined") {
  202. return;
  203. }
  204. const components = ["calcite-date-picker-month-header", "calcite-icon"];
  205. components.forEach(tagName => { switch (tagName) {
  206. case "calcite-date-picker-month-header":
  207. if (!customElements.get(tagName)) {
  208. customElements.define(tagName, DatePickerMonthHeader);
  209. }
  210. break;
  211. case "calcite-icon":
  212. if (!customElements.get(tagName)) {
  213. defineCustomElement$1();
  214. }
  215. break;
  216. } });
  217. }
  218. defineCustomElement();
  219. export { DatePickerMonthHeader as D, defineCustomElement as d };