@dialpad/dialtone
Version:
Dialpad's Dialtone design system monorepo
226 lines (225 loc) • 9.1 kB
JavaScript
"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue2 = require("@dialpad/dialtone-icons/vue2");
const dateFns = require("date-fns");
const utils = require("../utils.cjs");
const datepicker_constants = require("../datepicker_constants.cjs");
const stack = require("../../stack/stack.vue.cjs");
const tooltip = require("../../tooltip/tooltip.vue.cjs");
const button = require("../../button/button.vue.cjs");
const _pluginVue2_normalizer = require("../../../_virtual/_plugin-vue2_normalizer.cjs");
const _sfc_main = {
name: "DtDatepickerMonthYearPicker",
components: {
DtButton: button.default,
DtTooltip: tooltip.default,
DtStack: stack.default,
DtIconChevronLeft: vue2.DtIconChevronLeft,
DtIconChevronsLeft: vue2.DtIconChevronsLeft,
DtIconChevronRight: vue2.DtIconChevronRight,
DtIconChevronsRight: vue2.DtIconChevronsRight
},
props: {
locale: {
type: String,
required: true
},
prevMonthLabel: {
type: String,
required: true
},
nextMonthLabel: {
type: String,
required: true
},
prevYearLabel: {
type: String,
required: true
},
nextYearLabel: {
type: String,
required: true
},
changeToLabel: {
type: String,
required: true
},
selectedDate: {
type: Date,
required: true
}
},
emits: [
/**
* Will retrieve the calendar days of the given date
*
* @event calendar-days
* @type {Array}
*/
"calendar-days",
/**
* Will focus first day in calendar
*
* @event focus-first-day
*/
"focus-first-day",
/**
* Will focus last day in calendar
*
* @event focus-last-day
*/
"focus-last-day",
/**
* Will close the datepicker
*
* @event close-datepicker
*/
"close-datepicker"
],
data() {
return {
selectMonth: dateFns.getMonth(this.selectedDate),
selectYear: dateFns.getYear(this.selectedDate),
highlightedDay: null,
focusPicker: 0,
focusRefs: [],
refNames: ["prevYearButtonRef", "prevMonthButtonRef", "nextMonthButtonRef", "nextYearButtonRef"]
};
},
computed: {
// Get days for the currently selected month and year and highlight the selected day
calendarDays() {
return utils.getCalendarDays(this.selectMonth, this.selectYear, this.highlightedDay);
},
formattedMonth() {
return (month) => utils.formatMonth(month, datepicker_constants.INTL_MONTH_FORMAT, this.locale);
}
},
watch: {
selectMonth: {
handler() {
this.highlightDay();
this.$emit("calendar-days", this.calendarDays);
},
immediate: true
},
selectYear: {
handler() {
this.highlightDay();
this.$emit("calendar-days", this.calendarDays);
},
immediate: true
}
},
mounted() {
this.setButtonsRef();
this.focusMonthYearPicker();
},
methods: {
setButtonsRef() {
this.focusRefs = this.refNames.map((refName) => this.$refs[refName]);
},
focusMonthYearPicker() {
this.focusPicker = 0;
this.focusRefs[0].$el.focus();
},
handleKeyDown(event) {
switch (event.key) {
case "ArrowLeft":
event.preventDefault();
if (this.focusPicker === 0) {
this.focusPicker = 3;
this.focusRefs[this.focusPicker].$el.focus();
} else {
this.focusPicker--;
this.focusRefs[this.focusPicker].$el.focus();
}
break;
case "ArrowRight":
event.preventDefault();
if (this.focusPicker === 3) {
this.focusPicker = 0;
this.focusRefs[this.focusPicker].$el.focus();
} else {
this.focusPicker++;
this.focusRefs[this.focusPicker].$el.focus();
}
break;
case "ArrowDown":
event.preventDefault();
this.$emit("focus-first-day");
break;
case "Tab":
event.preventDefault();
this.$emit("focus-first-day");
break;
case "Escape":
this.$emit("close-datepicker");
break;
}
},
highlightDay() {
const year = dateFns.getYear(this.selectedDate);
const month = dateFns.getMonth(this.selectedDate);
if (year !== this.selectYear || month !== this.selectMonth) {
this.highlightedDay = null;
} else {
this.highlightedDay = dateFns.getDate(this.selectedDate);
}
},
changeMonth(value) {
if (this.selectMonth === 0 && value === -1 || this.selectMonth === 11 && value === 1) {
this.selectYear += value;
}
const initialDate = dateFns.set(this.selectedDate, { month: this.selectMonth, year: this.selectYear });
const newDate = value === 1 ? dateFns.addMonths(initialDate, 1) : dateFns.subMonths(initialDate, 1);
this.selectMonth = dateFns.getMonth(newDate);
},
changeYear(value) {
this.selectYear = this.selectYear + value;
},
goToNextMonth() {
this.changeMonth(1);
},
goToPrevMonth() {
this.changeMonth(-1);
}
}
};
var _sfc_render = function render() {
var _vm = this, _c = _vm._self._c;
return _c("dt-stack", { staticClass: "d-datepicker__month-year", attrs: { "direction": "row", "gap": "300" } }, [_c("dt-stack", { staticClass: "d-datepicker__nav", attrs: { "as": "nav", "direction": "row", "gap": "200" } }, [_c("dt-tooltip", { attrs: { "message": _vm.prevYearLabel, "placement": "top", "fallback-placements": ["top-start", "auto"] }, scopedSlots: _vm._u([{ key: "anchor", fn: function() {
return [_c("dt-button", { ref: _vm.refNames[0], staticClass: "d-datepicker__nav-btn", attrs: { "id": "prevYearButton", "size": "xs", "importance": "clear", "kind": "muted", "circle": true, "type": "button", "aria-label": `${_vm.changeToLabel} ${_vm.prevYearLabel} ${_vm.selectYear - 1}` }, on: { "click": function($event) {
return _vm.changeYear(-1);
}, "keydown": function($event) {
return _vm.handleKeyDown($event);
} } }, [_c("dt-icon-chevrons-left", { attrs: { "size": "200" } })], 1)];
}, proxy: true }]) }), _c("dt-tooltip", { attrs: { "message": _vm.prevMonthLabel, "placement": "top", "fallback-placements": ["top-end", "auto"] }, scopedSlots: _vm._u([{ key: "anchor", fn: function() {
return [_c("dt-button", { ref: _vm.refNames[1], staticClass: "d-datepicker__nav-btn", attrs: { "id": "prevMonthButton", "size": "xs", "importance": "clear", "kind": "muted", "circle": true, "type": "button", "aria-label": `${_vm.changeToLabel} ${_vm.prevMonthLabel} ${_vm.formattedMonth(_vm.selectMonth - 1)}` }, on: { "click": function($event) {
return _vm.changeMonth(-1);
}, "keydown": function($event) {
return _vm.handleKeyDown($event);
} } }, [_c("dt-icon-chevron-left", { attrs: { "size": "200" } })], 1)];
}, proxy: true }]) })], 1), _c("div", { staticClass: "d-datepicker__month-year-title", attrs: { "id": "calendar-heading" } }, [_vm._v(" " + _vm._s(_vm.formattedMonth(_vm.selectMonth)) + " " + _vm._s(_vm.selectYear) + " ")]), _c("dt-stack", { staticClass: "d-datepicker__nav", attrs: { "as": "nav", "direction": "row", "gap": "200" } }, [_c("dt-tooltip", { attrs: { "message": _vm.nextMonthLabel, "placement": "top", "fallback-placements": ["top-start", "auto"] }, scopedSlots: _vm._u([{ key: "anchor", fn: function() {
return [_c("dt-button", { ref: _vm.refNames[2], staticClass: "d-datepicker__nav-btn", attrs: { "id": "nextMonthButton", "size": "xs", "importance": "clear", "circle": true, "kind": "muted", "type": "button", "aria-label": `${_vm.changeToLabel} ${_vm.nextMonthLabel} ${_vm.formattedMonth(_vm.selectMonth + 1)}` }, on: { "click": function($event) {
return _vm.changeMonth(1);
}, "keydown": function($event) {
return _vm.handleKeyDown($event);
} } }, [_c("dt-icon-chevron-right", { attrs: { "size": "200" } })], 1)];
}, proxy: true }]) }), _c("dt-tooltip", { attrs: { "message": _vm.nextYearLabel, "placement": "top", "fallback-placements": ["top-end", "auto"] }, scopedSlots: _vm._u([{ key: "anchor", fn: function() {
return [_c("dt-button", { ref: _vm.refNames[3], staticClass: "d-datepicker__nav-btn", attrs: { "id": "nextYearButton", "size": "xs", "kind": "muted", "circle": true, "importance": "clear", "type": "button", "aria-label": `${_vm.changeToLabel} ${_vm.nextYearLabel} ${_vm.selectYear + 1}` }, on: { "click": function($event) {
return _vm.changeYear(1);
}, "keydown": function($event) {
return _vm.handleKeyDown($event);
} } }, [_c("dt-icon-chevrons-right", { attrs: { "size": "200" } })], 1)];
}, proxy: true }]) })], 1)], 1);
};
var _sfc_staticRenderFns = [];
var __component__ = /* @__PURE__ */ _pluginVue2_normalizer.default(
_sfc_main,
_sfc_render,
_sfc_staticRenderFns
);
const MonthYearPicker = __component__.exports;
exports.default = MonthYearPicker;
//# sourceMappingURL=month-year-picker.vue.cjs.map