UNPKG

aekimena-month-year-picker

Version:

Sleek, customizable month-year-picker for React Native applications

3 lines (2 loc) 5.33 kB
"use strict";var t=require("react-native"),e=require("react"),n=t.StyleSheet.create({modalContainer:{width:"100%",justifyContent:"flex-end",alignItems:"center",zIndex:5,position:"absolute",bottom:30},containerStyle:{width:"90%",backgroundColor:"#fff",borderRadius:15,overflow:"hidden"},ConfirmButtonStyle:{paddingVertical:10,paddingHorizontal:20,alignItems:"center",justifyContent:"center",backgroundColor:"#000",borderRadius:10,top:10},buttonTextStyle:{fontSize:16,color:"#fff"},innerContainer:{flex:1,flexDirection:"row",justifyContent:"center"},highlighter:{height:50,top:100,width:"100%",backgroundColor:"rgba(0,0,0,0.1)",position:"absolute",zIndex:-1},itemContainer:{height:50,justifyContent:"center",alignItems:"center",width:"100%"},itemText:{fontSize:16,color:"#000"},listContainer:{paddingBottom:100,paddingTop:100},absolutePressItem:{position:"absolute",height:"100%",width:"100%"}});var o=function(o){var i=o.data,r=o.onScrollToIndex,a=o.yScrollOffset,l=o.highlightedItem,h=o.itemTextStyle,u=o.itemContainerStyle,d=o.highlightedItemStyle;return e.createElement(t.View,{style:{flex:1}},e.createElement(t.FlatList,{onScroll:function(t){var e=t.nativeEvent.contentOffset.y;r(Math.round(e/50))},contentOffset:{x:0,y:a},data:i,contentContainerStyle:n.listContainer,keyExtractor:function(t){return t.toString()},renderItem:function(o){var i=o.item;return e.createElement(t.View,{style:[n.itemContainer,u,{height:50}]},e.createElement(t.Text,{style:[n.itemText,h,l==i&&(d||{fontSize:20,fontWeight:"500"})]},"number"==typeof i&&i<10?"0"+i:i))},getItemLayout:function(t,e){return{length:50,offset:50*e,index:e}},showsVerticalScrollIndicator:!1,snapToInterval:50,decelerationRate:"fast"}))},i=function(o){var i=o.highlighterStyle;return e.createElement(t.View,{style:[n.highlighter,i,{height:50}]})},r=function(o){var i=o.buttonText,r=o.buttonStyle,a=o.buttonTextStyle,l=o.onConfirm;return e.createElement(t.TouchableOpacity,{onPress:l,style:[n.ConfirmButtonStyle,r],activeOpacity:.5},e.createElement(t.Text,{style:[n.buttonTextStyle,a]},i))},a=[{id:0,month:"Jan"},{id:1,month:"Feb"},{id:2,month:"Mar"},{id:3,month:"Apr"},{id:4,month:"May"},{id:5,month:"Jun"},{id:6,month:"Jul"},{id:7,month:"Aug"},{id:8,month:"Sep"},{id:9,month:"Oct"},{id:10,month:"Nov"},{id:11,month:"Dec"}];module.exports=function(l){var h,u=l.visible,d=void 0!==u&&u,m=l.animationType,c=void 0===m?"fade":m,g=l.transparent,s=void 0===g||g,y=l.onRequestClose,f=l.onBackgroundPress,S=l.opacity,v=void 0===S?.5:S,b=l.containerStyle,x=l.maxDate,C=void 0===x?(new Date).toISOString():x,p=l.minDate,w=void 0===p?"2000-01-01":p,T=l.onConfirm,I=l.showDays,D=void 0===I||I,E=l.highlighterStyle,M=l.itemContainerStyle,O=l.itemTextStyle,F=l.buttonStyle,V=l.buttonTextStyle,Y=l.buttonText,k=void 0===Y?"Confirm":Y,z=l.highlightedItemStyle,P=new Date(C),B=new Date(w),N=function(t){for(var e=t.minDate,n=t.maxDate,o=new Date(e).getFullYear(),i=new Date(n).getFullYear(),r=[],a=o;a<=i;a++)r.push(a);return r}({maxDate:C,minDate:w}),R=e.useState(N.length-1),j=R[0],q=R[1],J=e.useState(Q().length-1),A=J[0],L=J[1],H=function(t){for(var e=t.month,n=t.year,o=new Date(n,e,1),i=[];o.getMonth()===e;)i.push(o.getDate()),o.setDate(o.getDate()+1);return i}({month:null===(h=Q()[A])||void 0===h?void 0:h.id,year:N[j]}),W=e.useState(X().length-1),G=W[0],K=W[1];if(isNaN(P.getTime())||isNaN(B.getTime()))throw new Error("Date must be a valid date type");if(P<B)throw new Error("Max date must be greater than Min date");if("number"!=typeof v)throw new Error("Opacity must be of type number");function Q(){return P.getFullYear()==B.getFullYear()?a.slice(B.getMonth(),P.getMonth()+1):P.getFullYear()==N[j]?a.slice(0,P.getMonth()+1):B.getFullYear()==N[j]?a.slice(B.getMonth()):a}var U=Q().map((function(t){return t.month}));function X(){var t,e;return P.getFullYear()==N[j]&&P.getMonth()==(null===(t=Q()[A])||void 0===t?void 0:t.id)?H.slice(0,P.getDate()):B.getFullYear()==N[j]&&B.getMonth()==(null===(e=Q()[A])||void 0===e?void 0:e.id)?H.slice(B.getDate()-1):H}return e.useEffect((function(){0==d&&(L(P.getMonth()),K(P.getDate()-1),q(N.length-1))}),[d]),e.createElement(t.Modal,{visible:d,animationType:c,transparent:s,statusBarTranslucent:!0,onRequestClose:y},e.createElement(t.View,{style:[n.modalContainer]},e.createElement(t.View,{style:[n.containerStyle,b,{height:250}]},e.createElement(t.View,{style:n.innerContainer},1==D&&e.createElement(o,{data:X(),onScrollToIndex:K,highlightedItem:X()[G],yScrollOffset:50*(X().length-1),itemContainerStyle:M,itemTextStyle:O,highlightedItemStyle:z}),e.createElement(o,{data:U,onScrollToIndex:function(t){L(t)},highlightedItem:U[A],yScrollOffset:50*(U.length-1),itemContainerStyle:M,itemTextStyle:O,highlightedItemStyle:z}),e.createElement(o,{data:N,onScrollToIndex:q,highlightedItem:N[j],yScrollOffset:50*(N.length-1),itemContainerStyle:M,itemTextStyle:O,highlightedItemStyle:z}),e.createElement(i,{highlighterStyle:E}))),e.createElement(r,{buttonStyle:F,buttonTextStyle:V,buttonText:k,onConfirm:function(){T(function(t){var e=t.year,n=t.month,o=t.day;return(void 0!==o?new Date(e,n,o):new Date(e,n,1)).toISOString()}({year:N[j],month:Q()[A].id,day:0==D?void 0:X()[G]}))}})),e.createElement(t.Pressable,{onPress:f,style:[n.absolutePressItem,{backgroundColor:"rgba(0,0,0, ".concat(v>100?1:v<0?0:v,")")}]}))}; //# sourceMappingURL=index.js.map