UNPKG

aekimena-month-year-picker

Version:

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

3 lines (2 loc) 5.33 kB
import{StyleSheet as t,View as e,FlatList as n,Text as o,TouchableOpacity as i,Modal as r,Pressable as a}from"react-native";import l,{useState as h,useEffect as m}from"react";var d=t.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 u=function(t){var i=t.data,r=t.onScrollToIndex,a=t.yScrollOffset,h=t.highlightedItem,m=t.itemTextStyle,u=t.itemContainerStyle,c=t.highlightedItemStyle;return l.createElement(e,{style:{flex:1}},l.createElement(n,{onScroll:function(t){var e=t.nativeEvent.contentOffset.y;r(Math.round(e/50))},contentOffset:{x:0,y:a},data:i,contentContainerStyle:d.listContainer,keyExtractor:function(t){return t.toString()},renderItem:function(t){var n=t.item;return l.createElement(e,{style:[d.itemContainer,u,{height:50}]},l.createElement(o,{style:[d.itemText,m,h==n&&(c||{fontSize:20,fontWeight:"500"})]},"number"==typeof n&&n<10?"0"+n:n))},getItemLayout:function(t,e){return{length:50,offset:50*e,index:e}},showsVerticalScrollIndicator:!1,snapToInterval:50,decelerationRate:"fast"}))},c=function(t){var n=t.highlighterStyle;return l.createElement(e,{style:[d.highlighter,n,{height:50}]})},g=function(t){var e=t.buttonText,n=t.buttonStyle,r=t.buttonTextStyle,a=t.onConfirm;return l.createElement(i,{onPress:a,style:[d.ConfirmButtonStyle,n],activeOpacity:.5},l.createElement(o,{style:[d.buttonTextStyle,r]},e))},s=[{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"}],y=function(t){var n,o=t.visible,i=void 0!==o&&o,y=t.animationType,f=void 0===y?"fade":y,S=t.transparent,v=void 0===S||S,b=t.onRequestClose,x=t.onBackgroundPress,p=t.opacity,C=void 0===p?.5:p,I=t.containerStyle,T=t.maxDate,D=void 0===T?(new Date).toISOString():T,w=t.minDate,E=void 0===w?"2000-01-01":w,M=t.onConfirm,O=t.showDays,F=void 0===O||O,Y=t.highlighterStyle,k=t.itemContainerStyle,z=t.itemTextStyle,B=t.buttonStyle,N=t.buttonTextStyle,P=t.buttonText,R=void 0===P?"Confirm":P,j=t.highlightedItemStyle,J=new Date(D),q=new Date(E),A=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:D,minDate:E}),V=h(A.length-1),H=V[0],L=V[1],W=h($().length-1),G=W[0],K=W[1],Q=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===(n=$()[G])||void 0===n?void 0:n.id,year:A[H]}),U=h(tt().length-1),X=U[0],Z=U[1];if(isNaN(J.getTime())||isNaN(q.getTime()))throw new Error("Date must be a valid date type");if(J<q)throw new Error("Max date must be greater than Min date");if("number"!=typeof C)throw new Error("Opacity must be of type number");function $(){return J.getFullYear()==q.getFullYear()?s.slice(q.getMonth(),J.getMonth()+1):J.getFullYear()==A[H]?s.slice(0,J.getMonth()+1):q.getFullYear()==A[H]?s.slice(q.getMonth()):s}var _=$().map((function(t){return t.month}));function tt(){var t,e;return J.getFullYear()==A[H]&&J.getMonth()==(null===(t=$()[G])||void 0===t?void 0:t.id)?Q.slice(0,J.getDate()):q.getFullYear()==A[H]&&q.getMonth()==(null===(e=$()[G])||void 0===e?void 0:e.id)?Q.slice(q.getDate()-1):Q}return m((function(){0==i&&(K(J.getMonth()),Z(J.getDate()-1),L(A.length-1))}),[i]),l.createElement(r,{visible:i,animationType:f,transparent:v,statusBarTranslucent:!0,onRequestClose:b},l.createElement(e,{style:[d.modalContainer]},l.createElement(e,{style:[d.containerStyle,I,{height:250}]},l.createElement(e,{style:d.innerContainer},1==F&&l.createElement(u,{data:tt(),onScrollToIndex:Z,highlightedItem:tt()[X],yScrollOffset:50*(tt().length-1),itemContainerStyle:k,itemTextStyle:z,highlightedItemStyle:j}),l.createElement(u,{data:_,onScrollToIndex:function(t){K(t)},highlightedItem:_[G],yScrollOffset:50*(_.length-1),itemContainerStyle:k,itemTextStyle:z,highlightedItemStyle:j}),l.createElement(u,{data:A,onScrollToIndex:L,highlightedItem:A[H],yScrollOffset:50*(A.length-1),itemContainerStyle:k,itemTextStyle:z,highlightedItemStyle:j}),l.createElement(c,{highlighterStyle:Y}))),l.createElement(g,{buttonStyle:B,buttonTextStyle:N,buttonText:R,onConfirm:function(){M(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:A[H],month:$()[G].id,day:0==F?void 0:tt()[X]}))}})),l.createElement(a,{onPress:x,style:[d.absolutePressItem,{backgroundColor:"rgba(0,0,0, ".concat(C>100?1:C<0?0:C,")")}]}))};export{y as default}; //# sourceMappingURL=index.mjs.map