UNPKG

@progress/kendo-vue-dateinputs

Version:
9 lines (8 loc) 11.3 kB
/** * @license *------------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("vue"),N=require("@progress/kendo-vue-popup"),c=require("@progress/kendo-date-math"),n=require("@progress/kendo-vue-common"),h=require("@progress/kendo-vue-intl"),j=require("../package-metadata.js"),m=require("../dateinput/DateInput.js"),x=require("../calendar/components/Calendar.js"),o=require("../calendar/models/SelectionRange.js"),g=require("../defaults.js"),a=require("../messages/main.js"),z=require("@progress/kendo-svg-icons"),P=s.defineComponent({name:"KendoDateRangePicker",emits:{blur:null,change:null,changemodel:null,"update:modelValue":null,focus:null,keydown:null,open:null,close:null},inject:{kendoLocalizationService:{default:null}},model:{event:"changemodel"},props:{allowReverse:{type:Boolean,default:!1},calendarSettings:Object,defaultShow:{type:Boolean,default:!1},defaultValue:{type:Object,default:function(){return o.EMPTY_SELECTIONRANGE}},modelValue:{type:Object,default:void 0},disabled:{type:Boolean,default:!1},popup:[String,Object,Function],calendar:[String,Object,Function],startDateInput:[String,Object,Function],endDateInput:[String,Object,Function],endDateInputSettings:Object,focusedDate:Date,format:{type:[String,Object],default:function(){return"d"}},id:String,ariaLabelledBy:String,ariaDescribedBy:String,max:{type:Date,default:function(){return g.MAX_DATE}},min:{type:Date,default:function(){return g.MIN_DATE}},rounded:{type:String,default:"medium",validator:function(e){return["small","medium","large","full"].includes(e)}},fillMode:{type:String,default:"solid",validator:function(e){return["solid","flat","outline"].includes(e)}},size:{type:String,default:"medium",validator:function(e){return["small","medium","large"].includes(e)}},popupSettings:{type:Object,default:function(){return{}}},show:{type:Boolean,default:void 0},startDateInputSettings:Object,swapButton:{type:Boolean,default:!1},tabIndex:Number,value:{type:Object,default:function(){}},inputAttributes:Object},created(){this._element=null,this._wrapper=null,this._calendar=null,this._startDateInput=null,this._endDateInput=null,this._popupId=n.guid(),this._anchor=n.guid(),this._startInputId=n.guid(),this._endInputId=n.guid(),this._calendarId=n.guid(),n.validatePackage(j.packageMetadata),this.currentShow=this.$props.show||this.$props.defaultShow,this.currentValue=this.$props.value||this.$props.defaultValue,this.initialAnimation=this.currentShow},mounted(){this._calendar=this.calendarRef,this._startDateInput=this.startDateInputRef,this._endDateInput=this.endDateInputRef,this.computedShow&&(this.$forceUpdate(),this.$nextTick(function(){this.initialAnimation=!1}))},updated(){this._calendar=this.calendarRef,this._startDateInput=this.startDateInputRef,this._endDateInput=this.endDateInputRef,this.shouldFocusCalendar&&this.focusCalendarElement(),this.shouldFocusDateInput&&this.focusDateInputElement(),this.shouldFocusCalendar=!1,this.shouldFocusDateInput=!1},data(){return{currentShow:!1,currentValue:null,valueDuringOnChange:void 0,shouldFocusDateInput:!1,shouldFocusCalendar:!1}},computed:{rootClassName(){return{"k-daterangepicker":!0,"k-disabled":this.$props.disabled}},computedValue(){return(this.valueDuringOnChange!==void 0?this.valueDuringOnChange:this.$props.value!==void 0?this.$props.value:this.$props.modelValue!==void 0?this.$props.modelValue:this.currentValue)||o.EMPTY_SELECTIONRANGE},computedShow(){return this.$props.show!==void 0?this.$props.show:this.currentShow},swapButtonClass(){const{size:e,rounded:t}=this.$props;return{"k-button":!0,[`k-button-${n.kendoThemeMaps.sizeMap[e]||e}`]:e,"k-button-flat":!0,"k-button-flat-base":!0,"k-icon-button":!0,[`k-rounded-${n.kendoThemeMaps.roundedMap[t]||t}`]:t}}},setup(){const e=s.ref(null),t=s.inject("kendoLocalizationService",{});return{kendoAnchorRef:e,kendoLocalizationService:t}},render(){const e=this.computedValue||o.EMPTY_SELECTIONRANGE,{size:t,fillMode:i,rounded:r}=this.$props,l=(this.$props.startDateInputSettings||{}).id||this._startInputId,f=(this.$props.endDateInputSettings||{}).id||this._endInputId,d=h.provideLocalizationService(this),I=d.toLanguageString(a.start,a.messages[a.start]),S=d.toLanguageString(a.end,a.messages[a.end]),D=d.toLanguageString(a.separator,a.messages[a.separator]),v=this.$props.startDateInput?n.templateRendering.call(this,this.$props.startDateInput,n.getListeners.call(this)):void 0,b={label:I,format:this.$props.format,min:this.min,max:this.max,id:this._startInputId,disabled:this.$props.disabled,valid:this.$props.valid,size:t,fillMode:i,rounded:r,ariaHasPopup:"grid",ariaExpanded:this.computedShow,value:e.start,ariaRole:"combobox",ariaControls:this._popupId,inputAttributes:this.$props.inputAttributes,...this.$props.startDateInputSettings},w=s.h(m.DateInput,{ref:u=>{this.startDateInputRef=u},...b,onChange:this.handleStartChange}),$=n.getTemplate.call(this,{h:s.h,template:v,defaultRendering:w,additionalListeners:{change:this.handleStartChange}}),C=this.$props.endDateInput?n.templateRendering.call(this,this.$props.endDateInput,n.getListeners.call(this)):void 0,R={label:S,format:this.$props.format,min:this.min,max:this.max,id:this._endInputId,size:t,fillMode:i,rounded:r,disabled:this.$props.disabled,valid:this.$props.valid,ariaHasPopup:"grid",ariaExpanded:this.computedShow,value:e.end,ariaRole:"combobox",ariaControls:this._popupId,inputAttributes:this.$props.inputAttributes,...this.$props.endDateInputSettings},y=s.h(m.DateInput,{ref:u=>{this.endDateInputRef=u},...R,onChange:this.handleEndChange}),E=n.getTemplate.call(this,{h:s.h,template:C,defaultRendering:y,additionalListeners:{change:this.handleEndChange}}),_=this.$props.calendar?n.templateRendering.call(this,this.$props.calendar,n.getListeners.call(this)):void 0,V={id:this._calendarId,min:this.min,max:this.max,views:2,allowReverse:this.$props.allowReverse,mode:"range",focusedDate:this.$props.focusedDate,disabled:this.$props.disabled,value:e,...this.$props.calendarSettings},k=s.h(x.Calendar,{ref:u=>{this.calendarRef=u},onKeydown:this.handleKeyDown,onFocus:this.calendarFocus,onBlur:this.calendarBlur,onChange:this.handleCalendarChange,...V}),p=n.getTemplate.call(this,{h:s.h,template:_,defaultRendering:k,additionalListeners:{change:this.handleCalendarChange,keydown:this.handleKeyDown,focus:this.calendarFocus,blur:this.calendarBlur}}),{popupClass:T,animate:B}=this.$props.popupSettings,L=this.$props.popup?n.templateRendering.call(this,this.$props.popup,n.getListeners.call(this)):void 0,O={show:this.computedShow,anchor:this._anchor,id:this._popupId,anchorAlign:{horizontal:"left",vertical:"bottom"},popupAlign:{horizontal:"left",vertical:"top"},animate:this.initialAnimation?!1:B,...this.$props.popupSettings},A=s.h(N.Popup,{class:T,...O},function(){return[p]}),M=n.getTemplate.call(this,{h:s.h,template:L,defaultRendering:A,defaultSlots:p}),F=s.createVNode("button",{class:this.swapButtonClass,role:"button",title:h.provideLocalizationService(this).toLanguageString(a.swapStartEnd,a.messages[a.swapStartEnd]),onMousedown:this.handleReverseMouseDown,onClick:this.handleReverseClick,"aria-controls":l+" "+f,"aria-label":h.provideLocalizationService(this).toLanguageString(a.swapStartEnd,a.messages[a.swapStartEnd])},[s.createVNode(n.Icon,{name:"arrows-swap",icon:z.arrowsSwapIcon,style:{transform:"rotate(90deg)"},class:"k-button-icon"},null)]);return s.createVNode("span",{class:this.rootClassName,style:this.$attrs.style,id:this.$props.id,"aria-labelledby":this.$props.ariaLabelledBy,"aria-describedby":this.$props.ariaDescribedBy,tabindex:this.$props.tabIndex,onFocusin:this.handleFocus,onFocusout:this.handleBlur,onKeydown:this.handleKeyDown,ref:u=>{this.kendoAnchorRef=u}},[$,(this.$props.allowReverse||this.$props.calendarSettings&&this.$props.calendarSettings.allowReverse)&&this.$props.swapButton?F:D,E,M])},methods:{focus(){const e=this.getStartInput();e&&e.focus()},focusCalendarElement(){const e=this.getCalendar();e&&e.focus({preventScroll:!0})},focusDateInputElement(){const e=this.getStartInput(),t=this.getEndInput();!document||!e||!t||((this.computedValue.start===null||this.computedValue.end!==null)&&document.activeElement!==t?e.focus({preventScroll:!0}):document.activeElement!==e&&t.focus({preventScroll:!0}))},calculateValue(e,t){return(e.value!==void 0?e.value:t.currentValue)||o.EMPTY_SELECTIONRANGE},calculateShow(e,t){return e.show!==void 0?e.show:t.currentShow},setShow(e){this.currentShow!==e&&(this.currentShow=e,this.$emit(e?"open":"close",{component:this}))},handleReverseClick(e){const t={start:this.computedValue.end,end:this.computedValue.start},i={event:e.event};this.handleChange(t,i)},handleReverseMouseDown(e){e.preventDefault()},handleFocus(e){this.shouldFocusDateInput||this.setShow(!0),this.$emit("focus",e)},calendarBlur(){clearTimeout(this._blurTimeout),this.createBlurTimeout()},calendarFocus(){clearTimeout(this._blurTimeout)},createBlurTimeout(){this._blurTimeout=setTimeout(()=>{const e=this.getStartInput(),t=this.getEndInput();e&&t&&n.canUseDOM&&e&&document.activeElement!==t&&this.setShow(!1)},200)},getStartInput(){return this._startDateInput&&this._startDateInput.element?this._startDateInput.element():document.getElementById(this._startInputId)},getEndInput(){return this._endDateInput&&this._endDateInput.element?this._endDateInput.element():document.getElementById(this._endInputId)},getCalendar(){return this._calendar&&this._calendar.element?this._calendar:document.getElementById(this._calendarId)},handleBlur(e){this.createBlurTimeout(),this.$emit("blur",e)},handleEndChange(e){const t={start:this.computedValue.start,end:c.cloneDate(e.value||void 0)};this.handleChange(t,e)},handleStartChange(e){const t={start:c.cloneDate(e.value||void 0),end:this.computedValue.end};this.handleChange(t,e)},extractRangeFromValue(e){if(!Array.isArray(e.value)&&!(e.value instanceof Date))return e.value||o.EMPTY_SELECTIONRANGE;const t=Array.isArray(e.value)?e.value[0]:e.value;return{start:this.computedValue.end!==null?t:this.computedValue.start,end:this.computedValue.start!==null?t:this.computedValue.end}},handleCalendarChange(e){const t=this.extractRangeFromValue(e);this.handleChange(t,e)},handleKeyDown(e){const{keyCode:t,altKey:i}=e,r=this.getEndInput(),l=this.getCalendar();t===n.Keys.esc?(e.preventDefault(),this.shouldFocusDateInput=!0,this.setShow(!1)):i&&t===n.Keys.down?(e.preventDefault(),this.shouldFocusCalendar=!0,this.setShow(!0)):t===n.Keys.tab&&this.computedShow&&l&&r&&document&&document.activeElement===r&&(e.preventDefault(),this.focusCalendarElement()),this.$emit("keydown",e)},handleChange(e,t){this.currentValue=e,this.valueDuringOnChange=e;const i={event:t.event,value:this.computedValue,show:this.computedShow,component:this,target:{name:this.$props.name,value:this.computedValue,show:this.computedShow}};this.$emit("changemodel",this.computedValue),this.$emit("update:modelValue",this.computedValue),this.$emit("change",i),this.valueDuringOnChange=void 0}}});exports.DateRangePicker=P;