UNPKG

@progress/kendo-vue-dateinputs

Version:
9 lines (8 loc) 11.2 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 o=require("vue"),u=require("@progress/kendo-date-math"),y=require("@progress/kendo-vue-intl"),T=require("./models/kendo-date.js"),l=require("@progress/kendo-vue-common"),S=require("@progress/kendo-vue-buttons"),B=require("../package-metadata.js"),p=require("./utils.js"),D=require("../utils.js"),r=require("../messages/main.js"),P=require("../timepicker/utils.js"),b=require("../defaults.js"),_=require("@progress/kendo-vue-labels"),v=require("@progress/kendo-svg-icons");function q(e){return typeof e=="function"||Object.prototype.toString.call(e)==="[object Object]"&&!o.isVNode(e)}const E="Please enter a valid value!",A=o.defineComponent({name:"DateInput",model:{event:"changemodel"},inject:{kendoIntlService:{default:null},kendoLocalizationService:{default:null}},emits:{change:null,changemodel:null,"update:modelValue":null,focus:null,blur:null},props:{modelValue:Date,value:Date,defaultValue:Date,format:{type:[String,Object],default:function(){return p.defaultFormat}},formatPlaceholder:{type:[String,Object],default:function(){return p.defaultFormatPlaceholder}},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)}},tabIndex:Number,title:String,steps:Object,placeholder:String,max:{type:Date,default:function(){return u.cloneDate(D.MAX_DATE)}},min:{type:Date,default:function(){return u.cloneDate(D.MIN_DATE)}},maxTime:{type:Date,default:function(){return u.cloneDate(b.MAX_TIME)}},minTime:{type:Date,default:function(){return u.cloneDate(b.MIN_TIME)}},disabled:{type:Boolean,default:!1},spinners:{type:Boolean,default:!1},name:String,dir:String,label:String,id:String,validationMessage:{type:String,default:E},required:{type:Boolean,default:!1},validityStyles:{type:Boolean,default:!0},validate:Boolean,valid:{type:Boolean,default:void 0},ariaRole:{type:String,default:"textbox"},ariaControls:String,ariaLabel:String,ariaExpanded:{type:Boolean,default:void 0},ariaHasPopup:{type:String,default:void 0},inputAttributes:Object},data(){return{kendoDate:null,currentFormat:void 0,valueDuringOnChange:void 0,hasMounted:!1,isEmpty:void 0,lastSelectedSymbol:void 0,isFocused:!1}},created(){l.validatePackage(B.packageMetadata);const{formatPlaceholder:e,format:t,value:a,defaultValue:i}=this.$props;this.kendoDate=new T.KendoDate(this.intl,e,t),this.kendoDate.setValue(null),this._emptyText=this.kendoDate.getTextAndFormat().text,this.kendoDate.setValue(a||i||null),this._element=null,this._inputId=l.guid()},computed:{computedValue(){return this.$data.valueDuringOnChange!==void 0?this.$data.valueDuringOnChange:this.kendoDate&&this.kendoDate.getDateObject()},wrapperClassNames(){const e=!this.$data.hasMounted||!this.$props.validityStyles||this.validity().valid,t=this.$props.disabled,{size:a,fillMode:i,rounded:n}=this.$props;return{"k-dateinput":!0,"k-input":!0,[`k-input-${l.kendoThemeMaps.sizeMap[a]||a}`]:a,[`k-input-${i}`]:i,[`k-rounded-${l.kendoThemeMaps.roundedMap[n]||n}`]:n,"k-disabled":t,"k-invalid":!e&&e!==void 0,"k-focus":this.isFocused,"k-rtl":this.$props.dir==="rtl"}}},methods:{selection(){let e={start:0,end:0};const t=this.element();return t!==null&&t.selectionStart!==void 0&&(e={start:t.selectionStart,end:t.selectionEnd}),e},element(){return this._element},focus(e){this._element&&this._element.focus(e)},handleFocus(e){this.$data.isFocused=!0,this.$emit("focus",e)},handleBlur(e){this.$data.isFocused=!1,this.$emit("blur",e)},intl(){return y.provideIntlService(this)},setValidity(){const e=this.element();e&&e.setCustomValidity&&e.setCustomValidity(this.validity().valid?"":this.$props.validationMessage)},spinnersMouseDown(e){const t=this.element();e.preventDefault(),t&&l.canUseDOM&&document.activeElement!==t&&t.focus({preventScroll:!0})},elementChange(e){const t=this.element();if(!t||!this.kendoDate)return;const{text:a,format:i}=this.kendoDate.getTextAndFormat();this.$data.currentFormat=i;const n=this.computedValue,s=p.approximateStringMatching(a,this.$data.currentFormat,t.value,this.selection().start),c=s.length===1&&s[0][1]==="_";if(!c)for(let d=0;d<s.length;d++)this.kendoDate.parsePart(s[d][0],s[d][1]);s.length&&s[0][0]!=="_"&&this.setSelection(this.selectionBySymbol(s[0][0])),c&&this.switchDateSegment(1),this.triggerChange(e,n)},elementClick(e){this.setSelection(this.selectionByIndex(this.selection().start))},wheel(e){const t=this.element();l.canUseDOM&&document.activeElement!==t||(e.deltaY<0&&(e.preventDefault(),this.increasePart(e)),e.deltaY>0&&(e.preventDefault(),this.decreasePart(e)))},increasePart(e){e.preventDefault(),this.modifyDateSegmentValue(1,e)},decreasePart(e){e.preventDefault(),this.modifyDateSegmentValue(-1,e)},elementKeyDown(e){if(!e.altKey){switch(e.keyCode){case 37:this.switchDateSegment(-1);break;case 38:this.modifyDateSegmentValue(1,e);break;case 39:this.switchDateSegment(1);break;case 40:this.modifyDateSegmentValue(-1,e);break;default:return}e.preventDefault()}},setSelection(e){const t=this.element();this.$data.lastSelectedSymbol=this.$data.currentFormat[e.start],l.canUseDOM&&window.requestAnimationFrame(()=>{t&&l.canUseDOM&&document.activeElement===t&&t.setSelectionRange(e.start,e.end)})},triggerChange(e,t){this.$data.valueDuringOnChange=this.computedValue,u.isEqual(t,this.computedValue)||(this.$emit("changemodel",this.computedValue),this.$emit("update:modelValue",this.computedValue),this.$emit("change",{event:e,value:this.computedValue,component:this,target:{name:this.$props.name,value:this.$data.valueDuringOnChange,valueAsDate:this.$data.valueDuringOnChange},validity:this.validity()})),this.$data.valueDuringOnChange=void 0},selectionBySymbol(e){let t=-1,a=0;for(let i=0;i<this.$data.currentFormat.length;i++)this.$data.currentFormat[i]===e&&(a=i+1,t===-1&&(t=i));return t<0&&(t=0),{start:t,end:a}},selectionByIndex(e){let t={start:e,end:e};for(let a=e,i=e-1;a<this.$data.currentFormat.length||i>=0;a++,i--){if(a<this.$data.currentFormat.length&&this.$data.currentFormat[a]!=="_"){t=this.selectionBySymbol(this.$data.currentFormat[a]);break}if(i>=0&&this.$data.currentFormat[i]!=="_"){t=this.selectionBySymbol(this.$data.currentFormat[i]);break}}return t},switchDateSegment(e){const{start:t,end:a}=this.selection();if(t<a&&this.$data.currentFormat[t]!==this.$data.currentFormat[a-1]){this.setSelection(this.selectionByIndex(e>0?t:a-1));return}const i=this.$data.currentFormat[t];let n=t+e;for(;n>0&&n<this.$data.currentFormat.length&&!(this.$data.currentFormat[n]!==i&&this.$data.currentFormat[n]!=="_");)n+=e;if(this.$data.currentFormat[n]==="_")return;let s=n;for(;s>=0&&s<this.$data.currentFormat.length&&this.$data.currentFormat[s]===this.$data.currentFormat[n];)s+=e;n>s&&(s+1!==t||n+1!==a)?this.setSelection({start:s+1,end:n+1}):n<s&&(n!==t||s!==a)&&this.setSelection({start:n,end:s})},modifyDateSegmentValue(e,t){if(!this.kendoDate)return;const a=this.computedValue,i=this.$data.currentFormat[this.selection().start],n=this.kendoDate.symbolMap(i),s=((this.$props.steps||{})[n]||1)*e;this.kendoDate.modifyPart(i,s),this.setSelection(this.selectionBySymbol(i)),this.triggerChange(t,a)},validity(){const e=p.isInRange(this.computedValue,this.$props.min,this.$props.max)&&P.isInTimeRange(this.computedValue,this.$props.minTime,this.$props.maxTime),t=this.$props.validationMessage!==void 0,a=(!this.$props.required||this.computedValue!==null)&&e,i=this.$props.valid!==void 0?this.$props.valid:a;return{customError:t,rangeOverflow:this.computedValue&&this.$props.max.getTime()<this.computedValue.getTime()||!1,rangeUnderflow:this.computedValue&&this.computedValue.getTime()<this.$props.min.getTime()||!1,valid:i,valueMissing:this.computedValue===null}}},mounted(){this._element=l.getRef(this,"input"),this.setValidity(),this.$data.hasMounted=!0},updated(){this.$data.lastSelectedSymbol&&this.setSelection(this.selectionBySymbol(this.$data.lastSelectedSymbol)),this.setValidity()},setup(){const e=o.ref(null),t=o.inject("kendoIntlService",{}),a=o.inject("kendoLocalizationService",{});return{inputRef:e,kendoIntlService:t,kendoLocalizationService:a}},render(){const e=l.getDefaultSlots(this),t=y.provideLocalizationService(this),{formatPlaceholder:a,format:i,value:n,modelValue:s,name:c,label:d,id:k,ariaLabel:V,ariaExpanded:F,ariaHasPopup:M,ariaRole:I,ariaControls:C,inputAttributes:x}=this.$props,m=n!==void 0?n:s;this.kendoDate.format=i,this.kendoDate.formatPlaceholder=a,this.valueDuringOnChange=this.valueDuringOnChange?this.valueDuringOnChange:void 0,m!==void 0&&this.computedValue!==m&&this.kendoDate.setValue(m);const{text:f,format:w}=this.kendoDate.getTextAndFormat();this.$data.currentFormat=w,this.$data.isEmpty=f===this._emptyText;const g=this.$props.placeholder!==void 0&&this.$data.isEmpty&&!this.$data.isFocused?null:f,$=k||this._inputId,O=!this.$props.validityStyles||this.validity().valid,h=o.createVNode("span",{class:this.wrapperClassNames,dir:this.$props.dir},[o.createVNode("input",o.mergeProps({role:I,tabindex:this.$props.tabIndex,disabled:this.$props.disabled,title:this.$props.title!==void 0?this.$props.title:f,type:"text",spellcheck:!1,autocomplete:"off",autocorrect:"off",class:"k-input-inner",id:$,placeholder:this.$props.placeholder,onWheel:this.wheel,onClick:this.elementClick,onInput:this.elementChange,onKeydown:this.elementKeyDown,onChange:l.noop,onFocusin:this.handleFocus,onFocusout:this.handleBlur,value:g,name:c,"aria-label":V,"aria-expanded":F,"aria-haspopup":M,"aria-controls":C,"aria-disabled":this.$props.disabled,ref:l.setRef(this,"input")},x),null),e,this.$props.spinners&&o.createVNode("span",{class:"k-input-spinner k-spin-button",onMousedown:this.spinnersMouseDown},[o.createVNode(S.Button,{type:"button",tabIndex:-1,icon:"caret-alt-up",svgIcon:v.caretAltUpIcon,size:this.$props.size,fillMode:this.$props.fillMode,class:"k-spinner-increase","aria-label":t.toLanguageString(r.increaseValue,r.messages[r.increaseValue]),title:t.toLanguageString(r.increaseValue,r.messages[r.increaseValue]),onClick:this.increasePart},null),o.createVNode(S.Button,{type:"button",tabIndex:-1,class:"k-spinner-decrease",icon:"caret-alt-down",svgIcon:v.caretAltDownIcon,size:this.$props.size,fillMode:this.$props.fillMode,"aria-label":t.toLanguageString(r.decreaseValue,r.messages[r.decreaseValue]),title:t.toLanguageString(r.decreaseValue,r.messages[r.decreaseValue]),onClick:this.decreasePart},null)])]);return d?o.createVNode(_.FloatingLabel,{label:d,editorId:$,editorValue:g,editorPlaceholder:this.$props.placeholder,editorValid:O,editorDisabled:this.$props.disabled},q(h)?h:{default:()=>[h]}):h}});exports.DateInput=A;