datepickerdate
Version:
A lightweight datepicker component for Preact
3 lines (2 loc) • 8.77 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("preact");function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function r(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),e}function o(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function a(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&u(e,t)}function c(e){return(c=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function u(e,t){return(u=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function l(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function s(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?l(e):t}function h(e){return function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}var f=(new Date).getFullYear(),d=(new Date).getMonth()+1,p=h(Array(7).keys()).map((function(e){return new Date(2017,9,e+1).toLocaleString(void 0,{weekday:"short"})})),y=h(Array(12).keys()).map((function(e){return new Date(2017,e,1).toLocaleString(void 0,{month:"long"})})),g=function(e,t){return"".concat(e).padStart(t,"0")},v=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:d,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f,n=[4,6,9,11],r=t%4==0;return 2===e?r?29:28:n.includes(e)?30:31},w=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:d,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f;return new Date("".concat(t,"-").concat(g(e,2),"-01")).getDay()+1},b=function(e){var t="[object Date]"===Object.prototype.toString.call(e),n=e&&!Number.isNaN(e.valueOf());return t&&n},m=function(e,t){if(!b(e)||!b(t))throw new Error("Value was not a Date object");var n=t.getDate(),r=t.getMonth()+1,o=t.getFullYear(),a=e.getDate(),i=e.getMonth()+1,c=e.getFullYear();return n===a&&r===i&&o===c},D=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date;if(!b(e))throw new Error("Invalid date format. Please follow 'ISO 8601' format; 2013-08-30");return[e.getFullYear(),g(e.getMonth()+1,2),g(e.getDate(),2)].join("-")},O=function(e,t){return{month:e>1?e-1:12,year:e>1?t:t-1}},k=function(e,t){return{month:e<12?e+1:1,year:e<12?t:t+1}},j=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:d,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f,n=v(e,t),r=w(e,t),o=r-1,a=o+n,i=35-a,c=42-a,u=i<0?c:i,l=O(e,t),s=l.month,p=l.year,y=k(e,t),b=y.month,m=y.year,D=v(s,p),j=h(new Array(o)).map((function(e,t){var n=t+1+(D-o);return[p,g(s,2),g(n,2)]})),C=h(new Array(n)).map((function(n,r){return[t,g(e,2),g(r+1,2)]})),M=h(new Array(u)).map((function(e,t){return[m,g(b,2),g(t+1,2)]}));return[].concat(h(j),h(C),h(M))},C=function(n){function u(n){var r;return t(this,u),o(l(r=s(this,c(u).call(this,n))),"checkClickOutside",(function(e){"keydown"!==e.type||"Escape"!==e.key?r.calendarRef.current&&(r.calendarRef.current.contains(e.target)||r.props.toggleCalendar()):r.props.toggleCalendar()})),o(l(r),"resolveStateFromDate",(function(e){var t=b(e)?e:new Date;return{current:t,month:t.getMonth()+1,year:t.getFullYear()}})),o(l(r),"getCalendarDates",(function(){var e=r.state,t=e.current,n=e.month,o=e.year,a=n||t.getMonth()+1,i=o||t.getFullYear();return j(a,i)})),o(l(r),"renderMonthAndYear",(function(){var t=r.state,n=t.month,o=t.year,a=y[n-1];return e.h("div",{class:"monthPicker"},e.h("button",{type:"button",class:"prevMonth",onClick:r.handlePrevious,title:"Previous Month"},e.h("svg",{xmlns:"http://www.w3.org/2000/svg",width:"25",height:"25",class:"monthArrow",fill:"none",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"2",viewBox:"0 0 24 24"},e.h("path",{d:"M19 12H5M12 19l-7-7 7-7"}))),e.h("div",{class:"monthYearLabel"},a," ",o),e.h("button",{type:"button",class:"nextMonth",onClick:r.handleNext,title:"Next Month"},e.h("svg",{xmlns:"http://www.w3.org/2000/svg",width:"25",height:"25",fill:"none",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"2",class:"monthArrow",viewBox:"0 0 24 24"},e.h("path",{d:"M5 12h14M12 5l7 7-7 7"}))))})),o(l(r),"renderDayLabels",(function(){return e.h("div",{class:"dayLabels"},p.map((function(t){return e.h("div",{key:t,class:"dayLabel"},t.toUpperCase())})))})),o(l(r),"renderCalendarDate",(function(){return e.h("div",{role:"presentation",class:"dateLabels",onMouseDown:r.gotoDate},j(r.state.month,r.state.year).map((function(t,n){var o=r.state,a=o.current,i=o.month,c=o.year,u=o.today,l=t.join("-"),s=new Date(l),h=m(s,u),f=m(s,a),d=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new Date;if(!b(e)||!b(t))return!1;var n=t.getMonth()+1,r=t.getFullYear(),o=e.getMonth()+1,a=e.getFullYear();return n===o&&r===a}(s,new Date("".concat(c,"-").concat(i,"-01")));return e.h("button",{type:"button",key:"".concat(c,"-").concat(i,"-").concat(n),"data-date":l,class:[!d&&"notInMonth",f&&"currentDay",h&&"today","dateLabel"].filter(Boolean).join(" ")},s.getDate())})))})),o(l(r),"gotoDate",(function(e){var t=e.target.dataset.date,n=r.props.onDateChanged;r.setState(r.resolveStateFromDate(new Date(t))),"function"==typeof n&&n(t)})),o(l(r),"gotoPreviousMonth",(function(){return r.setState((function(e){return O(e.month,e.year)}))})),o(l(r),"gotoNextMonth",(function(){return r.setState((function(e){return k(e.month,e.year)}))})),o(l(r),"gotoPreviousYear",(function(){return r.setState((function(e){return{year:e.year-1}}))})),o(l(r),"gotoNextYear",(function(){return r.setState((function(e){return{year:e.year+1}}))})),o(l(r),"handlePrevious",(function(e){return e.shiftKey?r.gotoPreviousYear():r.gotoPreviousMonth()})),o(l(r),"handleNext",(function(e){return e.shiftKey?r.gotoNextYear():r.gotoNextMonth()})),r.state=function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?a(Object(n),!0).forEach((function(t){o(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):a(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}({},r.resolveStateFromDate(n.date),{today:new Date}),r.calendarRef=e.createRef(),r}return i(u,e.Component),r(u,[{key:"componentDidMount",value:function(){document.addEventListener("keydown",this.checkClickOutside,!0),document.addEventListener("click",this.checkClickOutside,!0)}},{key:"componentWillUnmount",value:function(){document.removeEventListener("keydown",this.checkClickOutside,!0),document.removeEventListener("click",this.checkClickOutside,!0)}},{key:"render",value:function(){return e.h("div",{class:"calendar"},e.h("div",{ref:this.calendarRef},this.renderMonthAndYear(),this.renderDayLabels(),this.renderCalendarDate()))}}]),u}(),M=function(n){function a(){var e,n;t(this,a);for(var r=arguments.length,i=new Array(r),u=0;u<r;u++)i[u]=arguments[u];return o(l(n=s(this,(e=c(a)).call.apply(e,[this].concat(i)))),"state",{date:null,calendarOpen:!1}),o(l(n),"toggleCalendar",(function(){n.setState((function(e){return{calendarOpen:!e.calendarOpen}}))})),o(l(n),"handleDateChange",(function(e){var t=n.props,r=t.onDateChanged,o=t.name;n.setState({date:e}),n.toggleCalendar(),"function"==typeof r&&r(o,e)})),n}return i(a,e.Component),r(a,[{key:"componentDidMount",value:function(){var e=this.props.value;e&&this.setState({date:D(new Date(e))})}},{key:"render",value:function(t,n){var r=t.placeholder,o=t.formatter,a=void 0===o?function(e){return e}:o,i=n.date,c=n.calendarOpen;return e.h("div",{class:"dpd",style:"position:relative"},e.h("input",{type:"text",value:a(i),readOnly:"readonly",placeholder:r,onClick:this.toggleCalendar}),c&&e.h(C,{date:i&&new Date(i),onDateChanged:this.handleDateChange,toggleCalendar:this.toggleCalendar}))}}]),a}();exports.Datepicker=M,exports.WEEK_DAYS=p;
//# sourceMappingURL=index.cjs.js.map