/*! * All material copyright ESRI, All Rights Reserved, unless otherwise specified. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details. * v1.0.0-beta.97 */ import { proxyCustomElement, HTMLElement, createEvent, h, Fragment } from '@stencil/core/internal/client/index.js'; import { e as getOrder, b as dateFromRange, n as nextMonth, f as prevMonth } from './date.js'; import { H as Heading } from './Heading.js'; import { i as isActivationKey } from './key.js'; import { n as numberStringFormatter } from './locale.js'; import { o as closestElementCrossShadowBoundary } from './dom.js'; import { d as defineCustomElement$1 } from './icon.js'; const BUDDHIST_CALENDAR_YEAR_OFFSET = 543; 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}"; const DatePickerMonthHeader = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement { constructor() { super(); this.__registerHost(); this.__attachShadow(); this.calciteDatePickerSelect = createEvent(this, "calciteDatePickerSelect", 6); //-------------------------------------------------------------------------- // // Private State/Props // //-------------------------------------------------------------------------- this.globalAttributes = {}; //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- /** * Increment year on UP/DOWN keys * * @param event */ this.onYearKey = (event) => { const localizedYear = this.parseCalendarYear(event.target.value); switch (event.key) { case "ArrowDown": event.preventDefault(); this.setYear({ localizedYear, offset: -1 }); break; case "ArrowUp": event.preventDefault(); this.setYear({ localizedYear, offset: 1 }); break; } }; this.onYearChange = (event) => { this.setYear({ localizedYear: this.parseCalendarYear(event.target.value) }); }; this.onYearInput = (event) => { this.setYear({ localizedYear: this.parseCalendarYear(event.target.value), commit: false }); }; this.prevMonthClick = (event) => { this.handleArrowClick(event, this.prevMonthDate); }; this.prevMonthKeydown = (event) => { if (isActivationKey(event.key)) { this.prevMonthClick(event); } }; this.nextMonthClick = (event) => { this.handleArrowClick(event, this.nextMonthDate); }; this.nextMonthKeydown = (event) => { if (isActivationKey(event.key)) { this.nextMonthClick(event); } }; /* * Update active month on clicks of left/right arrows */ this.handleArrowClick = (event, date) => { event.preventDefault(); this.calciteDatePickerSelect.emit(date); }; } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- componentWillLoad() { this.parentDatePickerEl = closestElementCrossShadowBoundary(this.el, "calcite-date-picker"); } connectedCallback() { this.setNextPrevMonthDates(); } render() { return h("div", { class: "header" }, this.renderContent()); } renderContent() { var _a; if (!this.activeDate || !this.localeData) { return null; } if (this.parentDatePickerEl) { const { numberingSystem, lang: locale } = this.parentDatePickerEl; numberStringFormatter.numberFormatOptions = { useGrouping: false, ...(numberingSystem && { numberingSystem }), ...(locale && { locale }) }; } const activeMonth = this.activeDate.getMonth(); const { months, unitOrder } = this.localeData; const localizedMonth = (months.wide || months.narrow || months.abbreviated)[activeMonth]; const localizedYear = this.formatCalendarYear(this.activeDate.getFullYear()); const iconScale = this.scale === "l" ? "m" : "s"; const order = getOrder(unitOrder); const reverse = order.indexOf("y") < order.indexOf("m"); const suffix = (_a = this.localeData.year) === null || _a === void 0 ? void 0 : _a.suffix; 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: { year: true, "year--suffix": !!suffix }, 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 })))); } setNextPrevMonthDates() { if (!this.activeDate) { return; } this.nextMonthDate = dateFromRange(nextMonth(this.activeDate), this.min, this.max); this.prevMonthDate = dateFromRange(prevMonth(this.activeDate), this.min, this.max); } formatCalendarYear(year) { const { localeData } = this; const buddhistCalendar = localeData["default-calendar"] === "buddhist"; const yearOffset = buddhistCalendar ? BUDDHIST_CALENDAR_YEAR_OFFSET : 0; return numberStringFormatter.localize(`${year + yearOffset}`); } parseCalendarYear(year) { const { localeData } = this; const buddhistCalendar = localeData["default-calendar"] === "buddhist"; const yearOffset = buddhistCalendar ? BUDDHIST_CALENDAR_YEAR_OFFSET : 0; const parsedYear = Number(numberStringFormatter.delocalize(year)) - yearOffset; return numberStringFormatter.localize(`${parsedYear}`); } getInRangeDate({ localizedYear, offset = 0 }) { const { min, max, activeDate } = this; const parsedYear = Number(numberStringFormatter.delocalize(localizedYear)); const length = parsedYear.toString().length; const year = isNaN(parsedYear) ? false : parsedYear + offset; const inRange = year && (!min || min.getFullYear() <= year) && (!max || max.getFullYear() >= year); // if you've supplied a year and it's in range if (year && inRange && length === localizedYear.length) { const nextDate = new Date(activeDate); nextDate.setFullYear(year); return dateFromRange(nextDate, min, max); } } /** * Parse localized year string from input, * set to active if in range * * @param root0 * @param root0.localizedYear * @param root0.commit * @param root0.offset */ setYear({ localizedYear, commit = true, offset = 0 }) { const { yearInput, activeDate } = this; const inRangeDate = this.getInRangeDate({ localizedYear, offset }); // if you've supplied a year and it's in range, update active date if (inRangeDate) { this.calciteDatePickerSelect.emit(inRangeDate); } if (commit) { yearInput.value = this.formatCalendarYear((inRangeDate || activeDate).getFullYear()); } } get el() { return this; } static get watchers() { return { "min": ["setNextPrevMonthDates"], "max": ["setNextPrevMonthDates"], "activeDate": ["setNextPrevMonthDates"] }; } static get style() { return datePickerMonthHeaderCss; } }, [1, "calcite-date-picker-month-header", { "selectedDate": [16], "activeDate": [16], "headingLevel": [2, "heading-level"], "min": [16], "max": [16], "intlPrevMonth": [1, "intl-prev-month"], "intlNextMonth": [1, "intl-next-month"], "intlYear": [1, "intl-year"], "scale": [513], "localeData": [16], "globalAttributes": [32], "nextMonthDate": [32], "prevMonthDate": [32] }]); function defineCustomElement() { if (typeof customElements === "undefined") { return; } const components = ["calcite-date-picker-month-header", "calcite-icon"]; components.forEach(tagName => { switch (tagName) { case "calcite-date-picker-month-header": if (!customElements.get(tagName)) { customElements.define(tagName, DatePickerMonthHeader); } break; case "calcite-icon": if (!customElements.get(tagName)) { defineCustomElement$1(); } break; } }); } defineCustomElement(); export { DatePickerMonthHeader as D, defineCustomElement as d };