UNPKG

dayzed

Version:

Primitives to build simple, flexible, WAI-ARIA compliant React datepicker components.

3 lines (2 loc) 7.86 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require('preact/hooks'),require("prop-types")):"function"==typeof define&&define.amd?define(["exports","preact","prop-types"],t):t((e=e||self).Dayzed={},e.preact,e.PropTypes)}(this,(function(e,t,n){"use strict";function a(){return(a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var a in n)Object.prototype.hasOwnProperty.call(n,a)&&(e[a]=n[a])}return e}).apply(this,arguments)}function r(e,t){if(null==e)return{};var n,a,r={},o=Object.keys(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}function o(e){if(arguments.length<1)throw new TypeError("1 argument required, but only "+arguments.length+" present");var t=Object.prototype.toString.call(e);return e instanceof Date||"object"==typeof e&&"[object Date]"===t?new Date(e.getTime()):"number"==typeof e||"[object Number]"===t?new Date(e):("string"!=typeof e&&"[object String]"!==t||"undefined"==typeof console||(console.warn("Starting with v2.0.0-beta.1 date-fns doesn't accept strings as arguments. Please use `parseISO` to parse strings. See: https://git.io/fjule"),console.warn((new Error).stack)),new Date(NaN))}function s(e,t){if(arguments.length<2)throw new TypeError("2 arguments required, but only "+arguments.length+" present");var n=o(e),a=function(e){if(null===e||!0===e||!1===e)return NaN;var t=Number(e);return isNaN(t)?t:t<0?Math.ceil(t):Math.floor(t)}(t);return n.setDate(n.getDate()+a),n}function i(e,t){if(arguments.length<2)throw new TypeError("2 arguments required, but only "+arguments.length+" present");var n=o(e),a=o(t);return n.getTime()<a.getTime()}function f(e){if(arguments.length<1)throw new TypeError("1 argument required, but only "+arguments.length+" present");var t=o(e);return t.setHours(0,0,0,0),t}function u(e){if(arguments.length<1)throw new TypeError("1 argument required, but only "+arguments.length+" present");return function(e,t){if(arguments.length<2)throw new TypeError("2 arguments required, but only "+arguments.length+" present");var n=f(e),a=f(t);return n.getTime()===a.getTime()}(e,Date.now())}function l(e,t){if(arguments.length<2)throw new TypeError("2 arguments required, but only "+arguments.length+" present");var n=o(e),a=o(t);return 12*(n.getFullYear()-a.getFullYear())+(n.getMonth()-a.getMonth())}function d(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return function(e){for(var n=arguments.length,a=new Array(n>1?n-1:0),r=1;r<n;r++)a[r-1]=arguments[r];return t.some((function(t){return t&&t.apply(void 0,[e].concat(a)),e.defaultPrevented}))}}function c(e,t){throw new Error('The property "'+t+'" is required in "'+e+'"')}function h(){}function D(e){var t=e.calendars,n=e.minDate;return!!n&&!!i(s(t[0].firstDayOfMonth,-1),n)}function y(e){var t=e.calendars,n=e.maxDate;return!!n&&!!i(n,s(t[t.length-1].lastDayOfMonth,1))}function p(e){for(var t=e.date,n=e.selected,a=e.monthsToDisplay,r=e.offset,o=e.minDate,s=e.maxDate,u=e.firstDayOfWeek,l=e.showOutsideDays,d=[],c=function(e,t,n){var a=f(e);if(t){var r=f(t);i(a,r)&&(a=r)}if(n){var o=f(n);i(o,a)&&(a=o)}return a}(t,o,s),h=0;h<a;h++){var D=g({month:c.getMonth()+h+r,year:c.getFullYear(),selectedDates:n,minDate:o,maxDate:s,firstDayOfWeek:u,showOutsideDays:l});d.push(D)}return d}function g(e){var t=e.month,n=e.year,a=e.selectedDates,r=e.minDate,o=e.maxDate,i=e.firstDayOfWeek,f=e.showOutsideDays,l=function(e,t){var n=new Date(t,e,1);return e=n.getMonth(),t=n.getFullYear(),{daysInMonth:32-new Date(t,e,32).getDate(),month:e,year:t}}(t,n),d=l.daysInMonth;t=l.month,n=l.year;for(var c=[],h=1;h<=d;h++){var D=new Date(n,t,h),y={date:D,selected:m(a,D),selectable:v(r,o,D),today:u(D),prevMonth:!1,nextMonth:!1};c.push(y)}var p=new Date(n,t,1),g=new Date(n,t,d),O=function(e){var t=e.firstDayOfMonth,n=e.minDate,a=e.maxDate,r=e.selectedDates,o=e.firstDayOfWeek,i=e.showOutsideDays,f=[],u=(t.getDay()+7-o)%7;if(i)for(var l=s(t,-1),d=l.getDate(),c=l.getMonth(),h=l.getFullYear(),D=0;D<u;){var y=new Date(h,c,d-D),p={date:y,selected:m(r,y),selectable:v(n,a,y),today:!1,prevMonth:!0,nextMonth:!1};f.unshift(p),D++}else for(;u>0;)f.unshift(""),u--;return f}({firstDayOfMonth:p,minDate:r,maxDate:o,selectedDates:a,firstDayOfWeek:i,showOutsideDays:f}),w=function(e){var t=e.lastDayOfMonth,n=e.minDate,a=e.maxDate,r=e.selectedDates,o=e.firstDayOfWeek,i=e.showOutsideDays,f=[],u=(t.getDay()+7-o)%7;if(i)for(var l=s(t,1),d=l.getMonth(),c=l.getFullYear(),h=0;h<6-u;){var D=new Date(c,d,1+h),y={date:D,selected:m(r,D),selectable:v(n,a,D),today:!1,prevMonth:!1,nextMonth:!0};f.push(y),h++}else for(;u<6;)f.push(""),u++;return f}({lastDayOfMonth:g,minDate:r,maxDate:o,selectedDates:a,firstDayOfWeek:i,showOutsideDays:f});return c.unshift.apply(c,O),c.push.apply(c,w),{firstDayOfMonth:p,lastDayOfMonth:g,month:t,year:n,weeks:function(e){for(var t=Math.ceil(e.length/7),n=[],a=0;a<t;a++){n[a]=[];for(var r=0;r<7;r++)n[a].push(e[7*a+r])}return n}(c)}}function m(e,t){return(e=Array.isArray(e)?e:[e]).some((function(e){return e instanceof Date&&f(e).getTime()===f(t).getTime()}))}function v(e,t,n){return!(e&&i(n,e)||t&&i(t,n))}function O(e){return void 0!==e}function w(e,t){var n=void 0===t?{}:t,o=n.onClick,s=n.dateObj,i=void 0===s?c("getDateProps","dateObj"):s,f=r(n,["onClick","dateObj"]);return a({onClick:d(o,(function(t){e(i,t)})),disabled:!i.selectable,"aria-label":i.date.toDateString(),"aria-pressed":i.selected,role:"button"},f)}function b(e,t){var n=e.minDate,o=e.offsetMonth,s=e.handleOffsetChanged,i=void 0===t?{}:t,f=i.onClick,u=i.offset,h=void 0===u?1:u,y=i.calendars,p=void 0===y?c("getBackProps","calendars"):y,g=r(i,["onClick","offset","calendars"]);return a({onClick:d(f,(function(){s(o-function(e){var t=e.calendars,n=e.offset,a=e.minDate;if(n>1&&a){var r=l(t[0].firstDayOfMonth,a);r<n&&(n=r)}return n}({calendars:p,offset:h,minDate:n}))})),disabled:D({calendars:p,offset:h,minDate:n}),"aria-label":"Go back "+h+" month"+(1===h?"":"s")},g)}function M(e,t){var n=e.maxDate,o=e.offsetMonth,s=e.handleOffsetChanged,i=void 0===t?{}:t,f=i.onClick,u=i.offset,h=void 0===u?1:u,D=i.calendars,p=void 0===D?c("getForwardProps","calendars"):D,g=r(i,["onClick","offset","calendars"]);return a({onClick:d(f,(function(){s(o+function(e){var t=e.calendars,n=e.offset,a=e.maxDate;if(n>1&&a){var r=l(a,t[t.length-1].lastDayOfMonth);r<n&&(n=r)}return n}({calendars:p,offset:h,maxDate:n}))})),disabled:y({calendars:p,offset:h,maxDate:n}),"aria-label":"Go forward "+h+" month"+(1===h?"":"s")},g)}function k(e){var n,a,r=e.date,o=void 0===r?new Date:r,s=e.maxDate,i=e.minDate,f=e.monthsToDisplay,u=void 0===f?1:f,l=e.firstDayOfWeek,d=void 0===l?0:l,c=e.showOutsideDays,h=void 0!==c&&c,D=e.offset,y=e.onDateSelected,g=e.onOffsetChanged,m=void 0===g?function(){}:g,v=e.selected,k=t.useState(0),x=k[0],T=k[1],C=(a=x,O(n=D)?n:a);function j(e){O(D)||T(e),m(e)}return{calendars:p({date:o,selected:v,monthsToDisplay:u,minDate:i,maxDate:s,offset:C,firstDayOfWeek:d,showOutsideDays:h}),getDateProps:w.bind(null,y),getBackProps:b.bind(null,{minDate:i,offsetMonth:C,handleOffsetChanged:j}),getForwardProps:M.bind(null,{maxDate:s,offsetMonth:C,handleOffsetChanged:j})}}function x(e){var t,n=k(e);return(t=e.render||e.children,(t=Array.isArray(t)?t[0]:t)||h)(n)}n=n&&n.hasOwnProperty("default")?n.default:n,x.defaultProps={date:new Date,monthsToDisplay:1,onOffsetChanged:function(){},firstDayOfWeek:0,showOutsideDays:!1},x.propTypes={render:n.func,children:n.func,date:n.instanceOf(Date),maxDate:n.instanceOf(Date),minDate:n.instanceOf(Date),monthsToDisplay:n.number,firstDayOfWeek:n.number,showOutsideDays:n.bool,offset:n.number,onDateSelected:n.func.isRequired,onOffsetChanged:n.func,selected:n.oneOfType([n.arrayOf(Date),n.instanceOf(Date)])},e.default=x,e.useDayzed=k,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=dayzed.umd.min.js.map