datepickerdate
Version:
A lightweight datepicker component for Preact
3 lines (2 loc) • 8.96 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("preact")):"function"==typeof define&&define.amd?define(["exports","preact"],t):t((e=e||self).DatePickerDate={},e.preact)}(this,(function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(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 o(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(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 c(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&&l(e,t)}function u(e){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function l(e,t){return(l=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function s(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function f(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?s(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 d=(new Date).getFullYear(),p=(new Date).getMonth()+1,y=h(Array(7).keys()).map((function(e){return new Date(2017,9,e+1).toLocaleString(void 0,{weekday:"short"})})),g=h(Array(12).keys()).map((function(e){return new Date(2017,e,1).toLocaleString(void 0,{month:"long"})})),v=function(e,t){return"".concat(e).padStart(t,"0")},w=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:p,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:d,n=[4,6,9,11],r=t%4==0;return 2===e?r?29:28:n.includes(e)?30:31},b=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:p,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:d;return new Date("".concat(t,"-").concat(v(e,2),"-01")).getDay()+1},m=function(e){var t="[object Date]"===Object.prototype.toString.call(e),n=e&&!Number.isNaN(e.valueOf());return t&&n},D=function(e,t){if(!m(e)||!m(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},O=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date;if(!m(e))throw new Error("Invalid date format. Please follow 'ISO 8601' format; 2013-08-30");return[e.getFullYear(),v(e.getMonth()+1,2),v(e.getDate(),2)].join("-")},k=function(e,t){return{month:e>1?e-1:12,year:e>1?t:t-1}},j=function(e,t){return{month:e<12?e+1:1,year:e<12?t:t+1}},C=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:p,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:d,n=w(e,t),r=b(e,t),o=r-1,a=o+n,i=35-a,c=42-a,u=i<0?c:i,l=k(e,t),s=l.month,f=l.year,y=j(e,t),g=y.month,m=y.year,D=w(s,f),O=h(new Array(o)).map((function(e,t){var n=t+1+(D-o);return[f,v(s,2),v(n,2)]})),C=h(new Array(n)).map((function(n,r){return[t,v(e,2),v(r+1,2)]})),M=h(new Array(u)).map((function(e,t){return[m,v(g,2),v(t+1,2)]}));return[].concat(h(O),h(C),h(M))},M=function(e){function r(e){var o;return n(this,r),a(s(o=f(this,u(r).call(this,e))),"checkClickOutside",(function(e){"keydown"!==e.type||"Escape"!==e.key?o.calendarRef.current&&(o.calendarRef.current.contains(e.target)||o.props.toggleCalendar()):o.props.toggleCalendar()})),a(s(o),"resolveStateFromDate",(function(e){var t=m(e)?e:new Date;return{current:t,month:t.getMonth()+1,year:t.getFullYear()}})),a(s(o),"getCalendarDates",(function(){var e=o.state,t=e.current,n=e.month,r=e.year,a=n||t.getMonth()+1,i=r||t.getFullYear();return C(a,i)})),a(s(o),"renderMonthAndYear",(function(){var e=o.state,n=e.month,r=e.year,a=g[n-1];return t.h("div",{class:"monthPicker"},t.h("button",{type:"button",class:"prevMonth",onClick:o.handlePrevious,title:"Previous Month"},t.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"},t.h("path",{d:"M19 12H5M12 19l-7-7 7-7"}))),t.h("div",{class:"monthYearLabel"},a," ",r),t.h("button",{type:"button",class:"nextMonth",onClick:o.handleNext,title:"Next Month"},t.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"},t.h("path",{d:"M5 12h14M12 5l7 7-7 7"}))))})),a(s(o),"renderDayLabels",(function(){return t.h("div",{class:"dayLabels"},y.map((function(e){return t.h("div",{key:e,class:"dayLabel"},e.toUpperCase())})))})),a(s(o),"renderCalendarDate",(function(){return t.h("div",{role:"presentation",class:"dateLabels",onMouseDown:o.gotoDate},C(o.state.month,o.state.year).map((function(e,n){var r=o.state,a=r.current,i=r.month,c=r.year,u=r.today,l=e.join("-"),s=new Date(l),f=D(s,u),h=D(s,a),d=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new Date;if(!m(e)||!m(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 t.h("button",{type:"button",key:"".concat(c,"-").concat(i,"-").concat(n),"data-date":l,class:[!d&&"notInMonth",h&&"currentDay",f&&"today","dateLabel"].filter(Boolean).join(" ")},s.getDate())})))})),a(s(o),"gotoDate",(function(e){var t=e.target.dataset.date,n=o.props.onDateChanged;o.setState(o.resolveStateFromDate(new Date(t))),"function"==typeof n&&n(t)})),a(s(o),"gotoPreviousMonth",(function(){return o.setState((function(e){return k(e.month,e.year)}))})),a(s(o),"gotoNextMonth",(function(){return o.setState((function(e){return j(e.month,e.year)}))})),a(s(o),"gotoPreviousYear",(function(){return o.setState((function(e){return{year:e.year-1}}))})),a(s(o),"gotoNextYear",(function(){return o.setState((function(e){return{year:e.year+1}}))})),a(s(o),"handlePrevious",(function(e){return e.shiftKey?o.gotoPreviousYear():o.gotoPreviousMonth()})),a(s(o),"handleNext",(function(e){return e.shiftKey?o.gotoNextYear():o.gotoNextMonth()})),o.state=function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}({},o.resolveStateFromDate(e.date),{today:new Date}),o.calendarRef=t.createRef(),o}return c(r,e),o(r,[{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 t.h("div",{class:"calendar"},t.h("div",{ref:this.calendarRef},this.renderMonthAndYear(),this.renderDayLabels(),this.renderCalendarDate()))}}]),r}(t.Component),P=function(e){function r(){var e,t;n(this,r);for(var o=arguments.length,i=new Array(o),c=0;c<o;c++)i[c]=arguments[c];return a(s(t=f(this,(e=u(r)).call.apply(e,[this].concat(i)))),"state",{date:null,calendarOpen:!1}),a(s(t),"toggleCalendar",(function(){t.setState((function(e){return{calendarOpen:!e.calendarOpen}}))})),a(s(t),"handleDateChange",(function(e){var n=t.props,r=n.onDateChanged,o=n.name;t.setState({date:e}),t.toggleCalendar(),"function"==typeof r&&r(o,e)})),t}return c(r,e),o(r,[{key:"componentDidMount",value:function(){var e=this.props.value;e&&this.setState({date:O(new Date(e))})}},{key:"render",value:function(e,n){var r=e.placeholder,o=e.formatter,a=void 0===o?function(e){return e}:o,i=n.date,c=n.calendarOpen;return t.h("div",{class:"dpd",style:"position:relative"},t.h("input",{type:"text",value:a(i),readOnly:"readonly",placeholder:r,onClick:this.toggleCalendar}),c&&t.h(M,{date:i&&new Date(i),onDateChanged:this.handleDateChange,toggleCalendar:this.toggleCalendar}))}}]),r}(t.Component);e.Datepicker=P,e.WEEK_DAYS=y,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.js.map