UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

669 lines (668 loc) • 20.1 kB
import { Component, h, Prop, Event, Element, Host, State, Listen, Build, Watch } from "@stencil/core"; import { getLocaleData } from "./utils"; import { getElementDir } from "../../utils/dom"; import { dateFromRange, dateFromISO, dateToISO, getDaysDiff } from "../../utils/date"; import { getKey } from "../../utils/key"; import { TEXT } from "./calcite-date-picker-resources"; export class CalciteDatePicker { constructor() { //-------------------------------------------------------------------------- // // Public Properties // //-------------------------------------------------------------------------- /** Active range */ this.activeRange = "start"; /** Localized string for "previous month" (used for aria label) */ this.intlPrevMonth = TEXT.prevMonth; /** Localized string for "next month" (used for aria label) */ this.intlNextMonth = TEXT.nextMonth; /** BCP 47 language tag for desired language and country format */ this.locale = document.documentElement.lang || "en-US"; /** specify the scale of the date picker */ this.scale = "m"; /** Range mode activation */ this.range = false; /** Disables the default behaviour on the third click of narrowing or extending the range and instead starts a new range. */ this.proximitySelectionDisabled = false; this.hasShadow = Build.isBrowser && !!document.head.attachShadow; //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- this.keyUpHandler = (e) => { if (getKey(e.key) === "Escape") { this.reset(); } }; this.monthHeaderSelectChange = (e) => { const date = new Date(e.detail); if (!this.range) { this.activeDate = date; } else { if (this.activeRange === "start") { this.activeStartDate = date; } else if (this.activeRange === "end") { this.activeEndDate = date; } this.mostRecentRangeValue = date; } }; this.monthActiveDateChange = (e) => { const date = new Date(e.detail); if (!this.range) { this.activeDate = date; } else { if (this.activeRange === "start") { this.activeStartDate = date; } else if (this.activeRange === "end") { this.activeEndDate = date; } this.mostRecentRangeValue = date; } }; this.monthHoverChange = (e) => { if (!this.startAsDate) { this.hoverRange = undefined; return this.hoverRange; } const date = new Date(e.detail); this.hoverRange = { focused: this.activeRange, start: this.startAsDate, end: this.endAsDate }; if (!this.proximitySelectionDisabled) { if (this.endAsDate) { const startDiff = getDaysDiff(date, this.startAsDate); const endDiff = getDaysDiff(date, this.endAsDate); if (startDiff < endDiff) { this.hoverRange.start = date; this.hoverRange.focused = "start"; } else { this.hoverRange.end = date; this.hoverRange.focused = "end"; } } else { if (date < this.startAsDate) { this.hoverRange = { focused: "start", start: date, end: this.startAsDate }; } else { this.hoverRange.end = date; this.hoverRange.focused = "end"; } } } else { if (!this.endAsDate) { if (date < this.startAsDate) { this.hoverRange = { focused: "start", start: date, end: this.startAsDate }; } else { this.hoverRange.end = date; this.hoverRange.focused = "end"; } } else { this.hoverRange = undefined; } } }; this.monthMouseOutChange = () => { if (this.hoverRange) { this.hoverRange = undefined; } }; /** * Reset active date and close */ this.reset = () => { var _a, _b, _c, _d, _e, _f; if (this.valueAsDate && ((_a = this.valueAsDate) === null || _a === void 0 ? void 0 : _a.getTime()) !== ((_b = this.activeDate) === null || _b === void 0 ? void 0 : _b.getTime())) { this.activeDate = new Date(this.valueAsDate); } if (this.startAsDate && ((_c = this.startAsDate) === null || _c === void 0 ? void 0 : _c.getTime()) !== ((_d = this.activeStartDate) === null || _d === void 0 ? void 0 : _d.getTime())) { this.activeStartDate = new Date(this.startAsDate); } if (this.endAsDate && ((_e = this.endAsDate) === null || _e === void 0 ? void 0 : _e.getTime()) !== ((_f = this.activeEndDate) === null || _f === void 0 ? void 0 : _f.getTime())) { this.activeEndDate = new Date(this.endAsDate); } }; /** * Event handler for when the selected date changes */ this.monthDateChange = (e) => { const date = new Date(e.detail); if (!this.range) { this.value = dateToISO(date); this.activeDate = date; return; } if (!this.startAsDate || (!this.endAsDate && date < this.startAsDate)) { if (this.startAsDate) { const newEndDate = new Date(this.startAsDate); this.end = dateToISO(newEndDate); this.setEndAsDate(newEndDate); this.activeEndDate = newEndDate; } this.start = dateToISO(date); this.setStartAsDate(date); this.activeStartDate = date; } else if (!this.endAsDate) { this.end = dateToISO(date); this.setEndAsDate(date); this.activeEndDate = date; } else { if (!this.proximitySelectionDisabled) { const startDiff = getDaysDiff(date, this.startAsDate); const endDiff = getDaysDiff(date, this.endAsDate); if (startDiff < endDiff) { this.start = dateToISO(date); this.setStartAsDate(date); this.activeStartDate = date; } else { this.end = dateToISO(date); this.setEndAsDate(date); this.activeEndDate = date; } } else { this.start = dateToISO(date); this.setStartAsDate(date); this.activeStartDate = date; this.endAsDate = this.activeEndDate = this.end = undefined; } } }; } handleValueAsDate(date) { this.activeDate = date; this.calciteDatePickerChange.emit(date); } handleRangeChange() { const { startAsDate: startDate, endAsDate: endDate } = this; this.activeEndDate = endDate; this.activeStartDate = startDate; this.calciteDatePickerRangeChange.emit({ startDate, endDate }); } //-------------------------------------------------------------------------- // // Event Listeners // //-------------------------------------------------------------------------- /** * Blur doesn't fire properly when there is no shadow dom (ege/IE11) * Check if the focused element is inside the date picker, if not close */ focusInHandler(e) { if (!this.hasShadow && !this.el.contains(e.target)) { this.reset(); } } // -------------------------------------------------------------------------- // // Lifecycle // // -------------------------------------------------------------------------- connectedCallback() { this.loadLocaleData(); if (this.value) { this.valueAsDate = dateFromISO(this.value); } if (this.start) { this.setStartAsDate(dateFromISO(this.start)); } if (this.end) { this.setEndAsDate(dateFromISO(this.end)); } } render() { var _a; const min = dateFromISO(this.min); const max = dateFromISO(this.max); const date = dateFromRange(this.range ? this.startAsDate : this.valueAsDate, min, max); const activeStartDate = this.range ? this.getActiveStartDate(date, min, max) : this.getActiveDate(date, min, max); let activeDate = activeStartDate; const endDate = this.range ? dateFromRange(this.endAsDate, min, max) : null; const activeEndDate = this.getActiveEndDate(endDate, min, max); if ((this.activeRange === "end" || (((_a = this.hoverRange) === null || _a === void 0 ? void 0 : _a.focused) === "end" && (!this.proximitySelectionDisabled || endDate))) && activeEndDate) { activeDate = activeEndDate; } if (this.range && this.mostRecentRangeValue) { activeDate = this.mostRecentRangeValue; } const minDate = this.activeRange === "start" ? min : date || min; const maxDate = max; const dir = getElementDir(this.el); return (h(Host, { dir: dir, onBlur: this.reset, onKeyUp: this.keyUpHandler, role: "application" }, this.renderCalendar(activeDate, dir, maxDate, minDate, date, endDate))); } valueWatcher(value) { this.valueAsDate = dateFromISO(value); } startWatcher(start) { this.setStartAsDate(dateFromISO(start)); } endWatcher(end) { this.setEndAsDate(dateFromISO(end)); } async loadLocaleData() { const { locale } = this; this.localeData = await getLocaleData(locale); } /** * Render calcite-date-picker-month-header and calcite-date-picker-month */ renderCalendar(activeDate, dir, maxDate, minDate, date, endDate) { return (this.localeData && [ h("calcite-date-picker-month-header", { activeDate: activeDate, dir: dir, intlNextMonth: this.intlNextMonth, intlPrevMonth: this.intlPrevMonth, localeData: this.localeData, max: maxDate, min: minDate, onCalciteDatePickerSelect: this.monthHeaderSelectChange, scale: this.scale, selectedDate: this.activeRange === "start" ? date : endDate || new Date() }), h("calcite-date-picker-month", { activeDate: activeDate, dir: dir, endDate: this.range ? endDate : undefined, hoverRange: this.hoverRange, localeData: this.localeData, max: maxDate, min: minDate, onCalciteDatePickerActiveDateChange: this.monthActiveDateChange, onCalciteDatePickerHover: this.monthHoverChange, onCalciteDatePickerMouseOut: this.monthMouseOutChange, onCalciteDatePickerSelect: this.monthDateChange, scale: this.scale, selectedDate: this.activeRange === "start" ? date : endDate, startDate: this.range ? date : undefined }) ]); } /** * Update date instance of start if valid */ setStartAsDate(startDate) { this.startAsDate = startDate; this.mostRecentRangeValue = this.startAsDate; } /** * Update date instance of end if valid */ setEndAsDate(endDate) { this.endAsDate = endDate; this.mostRecentRangeValue = this.endAsDate; } /** * Get an active date using the value, or current date as default */ getActiveDate(value, min, max) { return dateFromRange(this.activeDate, min, max) || value || dateFromRange(new Date(), min, max); } getActiveStartDate(value, min, max) { return (dateFromRange(this.activeStartDate, min, max) || value || dateFromRange(new Date(), min, max)); } getActiveEndDate(value, min, max) { return (dateFromRange(this.activeEndDate, min, max) || value || dateFromRange(new Date(), min, max)); } static get is() { return "calcite-date-picker"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["calcite-date-picker.scss"] }; } static get styleUrls() { return { "$": ["calcite-date-picker.css"] }; } static get assetsDirs() { return ["assets"]; } static get properties() { return { "activeRange": { "type": "string", "mutable": false, "complexType": { "original": "\"start\" | \"end\"", "resolved": "\"end\" | \"start\"", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Active range" }, "attribute": "active-range", "reflect": false, "defaultValue": "\"start\"" }, "value": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Selected date" }, "attribute": "value", "reflect": false }, "valueAsDate": { "type": "unknown", "mutable": true, "complexType": { "original": "Date", "resolved": "Date", "references": { "Date": { "location": "global" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Selected date as full date object" } }, "startAsDate": { "type": "unknown", "mutable": true, "complexType": { "original": "Date", "resolved": "Date", "references": { "Date": { "location": "global" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Selected start date as full date object" } }, "endAsDate": { "type": "unknown", "mutable": true, "complexType": { "original": "Date", "resolved": "Date", "references": { "Date": { "location": "global" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Selected end date as full date object" } }, "min": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Earliest allowed date (\"yyyy-mm-dd\")" }, "attribute": "min", "reflect": false }, "max": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Latest allowed date (\"yyyy-mm-dd\")" }, "attribute": "max", "reflect": false }, "intlPrevMonth": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Localized string for \"previous month\" (used for aria label)" }, "attribute": "intl-prev-month", "reflect": false, "defaultValue": "TEXT.prevMonth" }, "intlNextMonth": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Localized string for \"next month\" (used for aria label)" }, "attribute": "intl-next-month", "reflect": false, "defaultValue": "TEXT.nextMonth" }, "locale": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "BCP 47 language tag for desired language and country format" }, "attribute": "locale", "reflect": false, "defaultValue": "document.documentElement.lang || \"en-US\"" }, "scale": { "type": "string", "mutable": false, "complexType": { "original": "\"s\" | \"m\" | \"l\"", "resolved": "\"l\" | \"m\" | \"s\"", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "specify the scale of the date picker" }, "attribute": "scale", "reflect": true, "defaultValue": "\"m\"" }, "range": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Range mode activation" }, "attribute": "range", "reflect": true, "defaultValue": "false" }, "start": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Selected start date" }, "attribute": "start", "reflect": false }, "end": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Selected end date" }, "attribute": "end", "reflect": false }, "proximitySelectionDisabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Disables the default behaviour on the third click of narrowing or extending the range and instead starts a new range." }, "attribute": "proximity-selection-disabled", "reflect": false, "defaultValue": "false" } }; } static get states() { return { "activeDate": {}, "activeStartDate": {}, "activeEndDate": {}, "localeData": {}, "hoverRange": {} }; } static get events() { return [{ "method": "calciteDatePickerChange", "name": "calciteDatePickerChange", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Trigger calcite date change when a user changes the date." }, "complexType": { "original": "Date", "resolved": "Date", "references": { "Date": { "location": "global" } } } }, { "method": "calciteDatePickerRangeChange", "name": "calciteDatePickerRangeChange", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Trigger calcite date change when a user changes the date range." }, "complexType": { "original": "DateRangeChange", "resolved": "DateRangeChange", "references": { "DateRangeChange": { "location": "import", "path": "./interfaces" } } } }]; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "valueAsDate", "methodName": "handleValueAsDate" }, { "propName": "startAsDate", "methodName": "handleRangeChange" }, { "propName": "endAsDate", "methodName": "handleRangeChange" }, { "propName": "value", "methodName": "valueWatcher" }, { "propName": "start", "methodName": "startWatcher" }, { "propName": "end", "methodName": "endWatcher" }, { "propName": "locale", "methodName": "loadLocaleData" }]; } static get listeners() { return [{ "name": "focusin", "method": "focusInHandler", "target": "window", "capture": false, "passive": false }]; } }