UNPKG

@progress/kendo-react-dateinputs

Version:

React DateInput is a perfect input component for handling quick and efficient date values. KendoReact Date Inputs package

9 lines (8 loc) • 14.4 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 _=require("react"),s=require("prop-types"),F=require("@progress/kendo-react-intl"),f=require("@progress/kendo-react-common"),o=require("@progress/kendo-date-math"),S=require("@progress/kendo-react-buttons"),x=require("@progress/kendo-svg-icons"),I=require("../models/NavigationAction.js"),M=require("../models/CalendarViewEnum.js"),N=require("../models/SelectionRange.js"),K=require("./Header.js"),r=require("../../utils.js"),V=require("../../messages/index.js"),z=require("../services/BusViewService.js"),H=require("../services/NavigationService.js"),E=require("./HorizontalViewList.js"),Y=require("./TodayCommand.js"),W=require("../../package-metadata.js");function j(c){const i=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(c){for(const a in c)if(a!=="default"){const d=Object.getOwnPropertyDescriptor(c,a);Object.defineProperty(i,a,d.get?d:{enumerable:!0,get:()=>c[a]})}}return i.default=c,Object.freeze(i)}const p=j(_),A=(c=m.defaultProps.min,i=m.defaultProps.max,a)=>a instanceof Date&&!Array.isArray(a)&&r.isInRange(o.getDate(a),c,i)?o.getDate(a):null,T=(c=m.defaultProps.min,i=m.defaultProps.max,a)=>Array.isArray(a)?a.filter(d=>r.isInRange(d,c,i)).map(d=>o.getDate(d)):null,P=c=>typeof c=="object"&&!(c instanceof Date)&&c!==null&&!Array.isArray(c)?c:N.EMPTY_SELECTIONRANGE,L=(c,i,a)=>c||i&&i[0]||a&&a.start,U=(c,i)=>c.start===null&&i===null?"start":c.end===null?"end":"start",v=class v extends p.Component{constructor(i){super(i),this.dates=[],this.selectedDate=null,this.selectedMultiple=null,this.selectedRange=N.EMPTY_SELECTIONRANGE,this._focusedDate=new Date,this.cellUID=this.props.id+"-cell-uid",this.activeRangeEnd="start",this._element=null,this.intl=null,this.localization=null,this.service=null,this.calendarViewList=null,this.isActive=!1,this.calculateFocusFromValue=!0,this.showLicenseWatermark=!1,this.focus=()=>{this._element&&this._element.focus()},this.clampRange=e=>({start:e,end:null}),this.rangeWithFocused=(e,t)=>({start:e.start,end:e.end===null&&e.start!==null&&this.isActive?t:e.end}),this.generateRange=(e,t)=>{const{end:h,start:l}=t,u=t.start!==null&&e.getTime()<=t.start.getTime();return!this.props.allowReverse&&u?{start:e,end:this.selectedRange.start}:this.activeRange!=="end"?{start:e,end:h}:{start:l||this.selectedDate,end:e}},this.canNavigate=e=>{if(!this.service)return!1;const t=this.service.move(this.focusedDate,e);return this.min<=t&&t<=this.max||this.service.isInSameView(t,this.min)||this.service.isInSameView(t,this.max)},this.navigate=(e,t)=>{this.calculateFocusFromValue=!1;const h=this.move(e,t);this.setState({navigateDate:h,focusedDate:h})},this.move=(e,t)=>this.clampDate(this.service.move(t,e)),this.clampDate=e=>r.dateInRange(e,this.min,this.max),this.shouldAutoCorrect=(e,t)=>{const{end:h,start:l}=t;return this.activeRange!=="end"?h!==null&&e>h:l!==null&&e<l},this.handleCellEnter=e=>{this.props.mode==="range"&&(this.calculateFocusFromValue=!1,this.setState({focusedDate:e}))},this.handleMouseDown=e=>{e.preventDefault()},this.handleClick=e=>{this._element&&this._element.focus({preventScroll:!0})},this.handleFocus=e=>{if(this.isActive=!0,!this.calendarViewList)return;this.calendarViewList.focusActiveDate();const{onFocus:t}=this.props;t&&t.call(void 0,e)},this.handleBlur=e=>{if(this.isActive=!1,!this.calendarViewList)return;this.calendarViewList.blurActiveDate();const{onBlur:t}=this.props;t&&t.call(void 0,e)},this.handleTodayClick=e=>{this.todayIsInRange&&this.handleDateChange(e)},this.handlePrevButtonClick=()=>{const e=I.Action.PrevView;if(this.state.activeView>0&&this.focusedDate.getFullYear()>this.dates[0].getFullYear())this.navigate(e,this.move(e,this.focusedDate));else{const t=this.isInMonth(this.focusedDate,this.dates[1])?this.move(e,this.focusedDate):this.focusedDate;this.navigate(e,t)}},this.handleNextButtonClick=()=>{this.navigate(I.Action.NextView,this.focusedDate)},this.handleKeyDown=e=>{const{keyCode:h,ctrlKey:l,metaKey:u}=e;if(h===84){const n=r.getToday();this.calculateFocusFromValue=!1,this.setState({focusedDate:n,navigateDate:n})}if((l||u)&&(h===f.Keys.left&&this.handlePrevButtonClick(),h===f.Keys.right&&this.handleNextButtonClick()),h===f.Keys.enter){const n={syntheticEvent:e,nativeEvent:e.nativeEvent,value:this.focusedDate,target:this};this.handleDateChange(n)}else{const n=r.dateInRange(this.navigation.move(this.focusedDate,this.navigation.action(e),this.state.activeView,this.service,e),this.min,this.max);if(o.isEqualDate(this.focusedDate,n))return;this.dates&&this.service&&!this.service.isInArray(n,this.dates)&&this.setState({navigateDate:n}),this.calculateFocusFromValue=!1,this.setState({focusedDate:n})}e.preventDefault()},this.handleViewChange=({view:e})=>{this.calculateFocusFromValue=!1,this.setState(t=>({activeView:e,navigateDate:t.focusedDate}))},this.handleWeekSelection=(e,t,h)=>{if(this.props.mode==="single")return;const l=0,u=6,n=t===l?e:o.addDays(e,-t),g=t===u?e:o.addDays(e,u-t);let D=null;if(this.props.mode==="multiple"){D=[];for(let R=l;R<=u;R++)D.push(o.addDays(n,R));this.setState({value:D,focusedDate:e})}this.props.mode==="range"&&(D={start:n,end:g},this.setState({value:D,focusedDate:e}));const{onChange:k}=this.props;if(k){const R={syntheticEvent:h,nativeEvent:h.nativeEvent,value:D,target:this};k.call(void 0,R)}},this.handleDateChange=e=>{const t=o.cloneDate(e.value),h=this.bus.canMoveDown(this.state.activeView);if(this.props.disabled)return;if(h)if(e.isTodayClick)this.bus.moveToBottom(this.state.activeView);else{this.bus.moveDown(this.state.activeView,e.syntheticEvent),this.setState({focusedDate:t,navigateDate:t});return}this.calculateFocusFromValue=!0;let l;switch(this.props.mode){case"single":l=o.cloneDate(e.value);break;case"multiple":if(Array.isArray(this.selectedMultiple)){const n=this.selectedMultiple.slice();let g=-1;n.forEach((D,k)=>{o.isEqualDate(D,e.value)&&(g=k)}),g!==-1?n.splice(g,1):n.push(o.cloneDate(e.value)),l=n.slice()}else this.selectedDate?l=[o.cloneDate(this.selectedDate),o.cloneDate(e.value)]:l=[o.cloneDate(e.value)];break;case"range":{l=this.selectedRange.start!==null&&this.selectedRange.end!==null&&this.activeRange==="start"?this.clampRange(e.value):this.generateRange(e.value,this.selectedRange),this.activeRangeEnd=this.activeRange!=="end"?"end":"start";break}default:l=o.cloneDate(e.value);break}this.valueDuringOnChange=l,e.isTodayClick&&this.setState({navigateDate:t}),this.setState({value:l,focusedDate:t}),this.valueDuringOnChange=l;const{onChange:u}=this.props;if(u){const n={syntheticEvent:e.syntheticEvent,nativeEvent:e.nativeEvent,value:l,target:this};u.call(void 0,n)}this.valueDuringOnChange=void 0},this.showLicenseWatermark=!f.validatePackage(W.packageMetadata,{component:"MultiViewCalendar"});const a=i.value!==void 0?i.value:i.defaultValue||v.defaultProps.defaultValue,d=A(this.min,this.max,a),w=T(this.min,this.max,a),C=P(a),O=L(d,w,C),b=r.viewInRange(M.CalendarViewEnum[i.defaultActiveView],this.bottomView,this.topView),y=r.dateInRange(i.focusedDate||O||r.getToday(),this.min,this.max);this.state={value:a,activeView:b,focusedDate:y,navigateDate:y},this.activeRangeEnd=U(C,d),this.bus=new z.BusViewService(this.handleViewChange),this.navigation=new H.NavigationService(this.bus),this.calculateFocusFromValue=!1,this.lastView=b,this.lastViewsCount=this.props.views||E.HorizontalViewList.defaultProps.views}get wrapperID(){return this.props.id+"-wrapper-id"}get isRtl(){return this.props.dir==="rtl"}get element(){return this._element}get value(){return this.valueDuringOnChange!==void 0?this.valueDuringOnChange:this.props.value!==void 0?this.props.value:this.state.value}get focusedDate(){return o.cloneDate(this._focusedDate)}get min(){return o.getDate(this.props.min!==void 0?this.props.min:v.defaultProps.min)}get max(){return o.getDate(this.props.max!==void 0?this.props.max:v.defaultProps.max)}get bottomView(){return M.CalendarViewEnum[this.props.bottomView!==void 0?this.props.bottomView:v.defaultProps.bottomView]}get topView(){return M.CalendarViewEnum[this.props.topView!==void 0?this.props.topView:v.defaultProps.topView]}get activeRange(){return this.props.activeRangeEnd!==void 0?this.props.activeRangeEnd:this.activeRangeEnd}get todayIsInRange(){return r.isInRange(r.getToday(),o.getDate(this.min),o.getDate(this.max))}componentDidMount(){this.calculateFocusFromValue=!0}componentDidUpdate(){this.calendarViewList&&(this.isActive?this.calendarViewList.focusActiveDate:this.calendarViewList.blurActiveDate)();const i=A(this.min,this.max,this.value);this.calculateFocusFromValue=!!(this.selectedDate&&i&&this.selectedDate.getTime()&&i.getTime()),this.lastView=this.state.activeView,this.lastViewsCount=this.props.views||E.HorizontalViewList.defaultProps.views}render(){this.props._ref&&this.props._ref(this),this.intl=F.provideIntlService(this),this.localization=F.provideLocalizationService(this),this.bus.configure(this.bottomView,this.topView);const i=r.viewInRange(this.state.activeView,this.bottomView,this.topView);this.service=this.bus.service(i,this.intl),this.selectedDate=A(this.min,this.max,this.value),this.selectedMultiple=T(this.min,this.max,this.value),this.selectedRange=P(this.value);const a=L(this.selectedDate,this.selectedMultiple,this.selectedRange);this._focusedDate=r.dateInRange(this.calculateFocusFromValue&&a!==null?a:this.state.focusedDate,this.min,this.max);const d=f.classNames("k-calendar k-calendar-range k-calendar-md",{"k-disabled":this.props.disabled},this.props.className),w=this.rangeWithFocused(this.selectedRange,this.focusedDate),C=this.localization.toLanguageString(V.prevView,V.messages[V.prevView]),O=this.localization.toLanguageString(V.nextView,V.messages[V.nextView]),b=!this.canNavigate(I.Action.PrevView),y=!this.canNavigate(I.Action.NextView),e={"aria-disabled":b},t={"aria-disabled":y},h=this.lastView!==i,l=this.dates&&this.isInMonth(this.state.navigateDate,this.dates[0]),u=this.lastViewsCount!==this.props.views;(!l||h||u)&&(this.dates=this.service.datesList(this.state.navigateDate,this.props.views||E.HorizontalViewList.defaultProps.views));const n=o.cloneDate(this.dates&&this.dates[0]?this.dates[0]:r.getToday());return p.createElement("div",{ref:g=>{this._element=g},className:d,id:this.props.id||this.wrapperID,"aria-labelledby":this.props.ariaLabelledBy,"aria-describedby":this.props.ariaDescribedBy,tabIndex:this.props.disabled?void 0:this.props.tabIndex,onFocus:this.handleFocus,onBlur:this.handleBlur,onMouseDown:this.handleMouseDown,onClick:this.handleClick,onKeyDown:this.handleKeyDown,"aria-disabled":this.props.disabled,dir:this.props.dir},p.createElement(K.Header,{key:`.kendo.calendar.header.${n.getTime()}`,activeView:i,currentDate:n,min:this.min,max:this.max,rangeLength:this.props.views,bus:this.bus,service:this.service,headerTitle:this.props.headerTitle,verticalView:this.props.mobileMode,commands:p.createElement(p.Fragment,null,p.createElement(S.Button,{type:"button",className:"k-calendar-nav-prev",icon:this.isRtl?"chevron-right":"chevron-left",svgIcon:this.isRtl?x.chevronRightIcon:x.chevronLeftIcon,fillMode:"flat",title:C,disabled:b,onClick:this.handlePrevButtonClick,...e}),p.createElement(Y.TodayCommand,{min:this.min,max:this.max,onClick:this.handleTodayClick,disabled:!this.todayIsInRange}),p.createElement(S.Button,{type:"button",className:"k-calendar-nav-next",icon:this.isRtl?"chevron-left":"chevron-right",svgIcon:this.isRtl?x.chevronLeftIcon:x.chevronRightIcon,fillMode:"flat",title:O,disabled:y,onClick:this.handleNextButtonClick,...t}))}),p.createElement(E.HorizontalViewList,{ref:g=>{this.calendarViewList=g},dates:this.dates,activeView:i,focusedDate:this.focusedDate,weekDaysFormat:this.props.weekDaysFormat,min:this.min,max:this.max,bus:this.bus,service:this.service,selectionRange:w,value:this.selectedMultiple||this.selectedDate,cellUID:this.cellUID,views:this.props.views,onChange:this.handleDateChange,onWeekSelect:this.handleWeekSelection,showWeekNumbers:this.props.weekNumber,onCellEnter:this.handleCellEnter,cell:this.props.cell,weekCell:this.props.weekCell,headerTitle:this.props.headerTitle,verticalView:this.props.mobileMode,showOtherMonthDays:this.props.showOtherMonthDays,allowReverse:this.props.allowReverse}),this.showLicenseWatermark&&p.createElement(f.WatermarkOverlay,null))}isInMonth(i,a){return!!a&&o.firstDayOfMonth(a)<=i&&i<=o.lastDayOfMonth(a)}};v.displayName="MultiViewCalendar",v.propTypes={activeRangeEnd:s.oneOf(["start","end"]),allowReverse:s.bool,bottomView:s.oneOf(["month","year","decade","century"]),className:s.string,defaultActiveView:s.oneOf(["month","year","decade","century"]),defaultValue:s.oneOfType([r.nullable(s.instanceOf(Date)),s.arrayOf(s.instanceOf(Date)),s.shape({start:r.nullable(s.instanceOf(Date)),end:r.nullable(s.instanceOf(Date))})]),disabled:s.bool,focusedDate:s.instanceOf(Date),id:s.string,weekDaysFormat:s.oneOf(["narrow","short","abbreviated"]),ariaLabelledBy:s.string,ariaDescribedBy:s.string,max:s.instanceOf(Date),min:s.instanceOf(Date),mode:s.oneOf(["single","multiple","range"]),onBlur:s.func,onChange:s.func,onFocus:s.func,tabIndex:s.number,topView:s.oneOf(["month","year","decade","century"]),value:s.oneOfType([r.nullable(s.instanceOf(Date)),s.arrayOf(s.instanceOf(Date)),s.shape({start:r.nullable(s.instanceOf(Date).isRequired),end:r.nullable(s.instanceOf(Date).isRequired)})]),views:(i,a,d)=>{const w=i[a];return w!==void 0&&w<1?new Error(`Invalid prop '${a}' supplied to'${d}'. The '${a}' property cannot be less than 1'`):null},weekNumber:s.bool,showOtherMonthDays:s.bool,dir:s.string},v.defaultProps={disabled:!1,min:r.MIN_DATE,max:r.MAX_DATE,navigation:!0,defaultActiveView:"month",defaultValue:null,topView:"century",weekDaysFormat:"short",tabIndex:0,bottomView:"month",views:2,allowReverse:!1,showOtherMonthDays:!1};let m=v;const B=f.createPropsContext(),q=f.withIdHOC(f.withPropsContext(B,m));q.displayName="KendoReactMultiViewCalendar";F.registerForIntl(m);F.registerForLocalization(m);exports.MultiViewCalendar=q;exports.MultiViewCalendarPropsContext=B;exports.MultiViewCalendarWithoutContext=m;