@momo-kits/date-picker 0.0.34-beta → 0.0.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,612 @@
1
- var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _react=_interopRequireWildcard(require("react"));var _reactNative=require("react-native");var _propTypes=_interopRequireDefault(require("prop-types"));var _core=require("@momo-kits/core");var _WheelPicker=_interopRequireDefault(require("./WheelPicker"));var _DatePickerHelper=_interopRequireDefault(require("./helper/DatePickerHelper"));var _jsxFileName="/Users/trinh.ho2/momo-folk/kits/src/libs/datePicker/dist/datePicker/DatePicker.js";function _getRequireWildcardCache(nodeInterop){if(typeof WeakMap!=="function")return null;var cacheBabelInterop=new WeakMap();var cacheNodeInterop=new WeakMap();return(_getRequireWildcardCache=function _getRequireWildcardCache(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop;})(nodeInterop);}function _interopRequireWildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule){return obj;}if(obj===null||typeof obj!=="object"&&typeof obj!=="function"){return{default:obj};}var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj)){return cache.get(obj);}var newObj={};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(key!=="default"&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc);}else{newObj[key]=obj[key];}}}newObj.default=obj;if(cache){cache.set(obj,newObj);}return newObj;}function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function _createSuperInternal(){var Super=(0,_getPrototypeOf2.default)(Derived),result;if(hasNativeReflectConstruct){var NewTarget=(0,_getPrototypeOf2.default)(this).constructor;result=Reflect.construct(Super,arguments,NewTarget);}else{result=Super.apply(this,arguments);}return(0,_possibleConstructorReturn2.default)(this,result);};}function _isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));return true;}catch(e){return false;}}var ifIphoneX=_core.ScreenUtils.ifIphoneX;var widthScreen=_reactNative.Dimensions.get('window').width;var styles=_reactNative.StyleSheet.create({container:{width:widthScreen,alignItems:'center',justifyContent:'center',paddingHorizontal:6},content:{flexDirection:'row',width:widthScreen,alignItems:'center',justifyContent:'center'},column:{borderWidth:2,borderColor:'red'},header:{borderRadius:16,marginBottom:0},view:{backgroundColor:'white',borderTopRightRadius:16,borderTopLeftRadius:16,overflow:'hidden'},titleCancel:{fontSize:17,color:'#4A90E2'},titleConfirm:{fontSize:17,color:'#4A90E2',textAlign:'right'}});var getStrings=function getStrings(strings){return(strings==null?void 0:strings.length)===1?"0"+strings:strings;};var DatePicker=function(_Component){(0,_inherits2.default)(DatePicker,_Component);var _super=_createSuper(DatePicker);function DatePicker(props){var _this;(0,_classCallCheck2.default)(this,DatePicker);_this=_super.call(this,props);_this.onBeginDrag=function(refName){for(var i in _this.arrayRef){if(i&&i!==refName&&_this.arrayRef&&_this.arrayRef[i]&&_this.arrayRef[i].update){_this.arrayRef[i].setScrollEnabled(false);}}};_this.onEndDrag=function(refName){for(var i in _this.arrayRef){if(i&&i!==refName&&_this.arrayRef&&_this.arrayRef[i]&&_this.arrayRef[i].update){_this.arrayRef[i].setScrollEnabled(true);}}};_this.show=function(){};_this.hide=function(){var _this$props=_this.props,onClose=_this$props.onClose,requestClose=_this$props.requestClose;if(requestClose&&typeof requestClose==='function'){requestClose(function(){if(onClose&&typeof onClose==='function'){onClose();}});}};_this.finish=function(){var _this$props2=_this.props,format=_this$props2.format,onSelected=_this$props2.onSelected,isRanged=_this$props2.isRanged,selectedDate=_this$props2.selectedDate;if(isRanged&&format==='hh:mm'){var from=_this.ranged[_this.hourRangeIndex_1];var to=_this.ranged[_this.hourRangeIndex_2];if(onSelected&&typeof onSelected==='function'){onSelected({from:from,to:to});}_this.hide();return;}var day=_this.days[_this.dayIndex];var month=_this.months[_this.monthIndex];var year=_this.years[_this.yearIndex];var hour=_this.hours[_this.hourIndex];var minute=_this.minutes[_this.minuteIndex];var selectedDateFormatted=_DatePickerHelper.default.toString(day,month,year,hour,minute,format);if(onSelected&&typeof onSelected==='function'){onSelected(selectedDateFormatted||selectedDate||_DatePickerHelper.default.getCurrentDate(format));}_this.hide();};_this.renderDescription=function(){var _this$props3=_this.props,description=_this$props3.description,descriptionStyle=_this$props3.descriptionStyle,renderDescription=_this$props3.renderDescription;if(renderDescription)return renderDescription;return description?_react.default.createElement(_core.Text.Title,{style:[{marginBottom:_core.Spacing.S},descriptionStyle],__source:{fileName:_jsxFileName,lineNumber:471,columnNumber:13}},description):null;};_this.initDefault(props);return _this;}(0,_createClass2.default)(DatePicker,[{key:"componentDidMount",value:function componentDidMount(){this.setStateMonth(this.dayIndex,this.monthIndex,this.yearIndex);var onCreateRef=this.props.onCreateRef;if(typeof onCreateRef==='function')onCreateRef(this);}},{key:"componentWillUnmount",value:function componentWillUnmount(){}},{key:"initDefault",value:function initDefault(props){if(props){var propsMinDate=props.minDate?props.minDate:props.minValue;var propsMaxDate=props.maxDate?props.maxDate:props.maxValue;var propsSelectedDate=props.selectedDate?props.selectedDate:props.value;if(propsMinDate){this.minDate=_DatePickerHelper.default.formatDate(propsMinDate,props.format);}else if(props.maxAge){this.minDate=_DatePickerHelper.default.calculateDateSinceYearAgo(props.maxAge);}else{this.minDate=null;}if(propsMaxDate){this.maxDate=_DatePickerHelper.default.formatDate(propsMaxDate,props.format);}else if(props.minAge){this.maxDate=_DatePickerHelper.default.calculateDateSinceYearAgo(props.minAge);}else{this.maxDate=null;}if(this.maxDate&&this.minDate&&this.maxDate.getTime()<this.minDate.getTime()){this.maxDate=null;this.minDate=null;}if(propsSelectedDate){this.selectedDate=_DatePickerHelper.default.formatDate(propsSelectedDate,props.format);if(this.selectedDate){if(this.minDate&&this.selectedDate.getTime()<this.minDate.getTime()){this.selectedDate=null;}else if(this.maxDate&&this.maxDate.getTime()<this.selectedDate.getTime()){this.selectedDate=null;}}}else{this.selectedDate=null;}if(props.format==='hh:mm'){this.selectedDate=props.isRanged?{from:_DatePickerHelper.default.formatDate(propsSelectedDate.from,props.format),to:_DatePickerHelper.default.formatDate(propsSelectedDate.to,props.format)}:_DatePickerHelper.default.formatDate(propsSelectedDate,props.format);}}this.createDataDate(this.minDate,this.maxDate,props.minuteArray);this.selectedIndexDate();}},{key:"createDataDate",value:function createDataDate(minDate,maxDate,minuteArray){this.minDay=1;this.minMonth=1;this.minYear=1970;this.maxDay=31;this.maxMonth=12;this.maxYear=2100;if(minDate){this.minDay=minDate.getDate();this.minMonth=minDate.getMonth()+1;this.minYear=minDate.getFullYear();}if(maxDate){this.maxDay=maxDate.getDate();this.maxMonth=maxDate.getMonth()+1;this.maxYear=maxDate.getFullYear();}this.days=_DatePickerHelper.default.makeRange(1,31);this.months=_DatePickerHelper.default.makeRange(1,12);this.years=_DatePickerHelper.default.makeRange(this.minYear,this.maxYear);this.hours=_DatePickerHelper.default.makeRange(0,23,true);if(Array.isArray(minuteArray)){this.minutes=minuteArray;}else{this.minutes=_DatePickerHelper.default.makeRange(0,59,true);}this.ranged=_DatePickerHelper.default.arrayRange;}},{key:"selectedIndexDate",value:function selectedIndexDate(){var _this$props4=this.props,format=_this$props4.format,isRanged=_this$props4.isRanged;var current=new Date();if(this.minDate&&current.getTime()<this.minDate.getTime()){current=this.minDate;}else if(this.maxDate&&current.getTime()>this.maxDate.getTime()){current=this.maxDate;}if(format==='hh:mm'&&isRanged){var _this$selectedDate,_this$selectedDate$fr,_this$selectedDate$fr2,_this$selectedDate2,_this$selectedDate2$f,_this$selectedDate2$f2,_this$selectedDate3,_this$selectedDate3$t,_this$selectedDate3$t2,_this$selectedDate4,_this$selectedDate4$t,_this$selectedDate4$t2;var valueHours_1=this.selectedDate?getStrings((_this$selectedDate=this.selectedDate)==null?void 0:(_this$selectedDate$fr=_this$selectedDate.from)==null?void 0:(_this$selectedDate$fr2=_this$selectedDate$fr.getHours())==null?void 0:_this$selectedDate$fr2.toString())+":"+getStrings((_this$selectedDate2=this.selectedDate)==null?void 0:(_this$selectedDate2$f=_this$selectedDate2.from)==null?void 0:(_this$selectedDate2$f2=_this$selectedDate2$f.getMinutes())==null?void 0:_this$selectedDate2$f2.toString()):'';var valueHours_2=this.selectedDate?getStrings((_this$selectedDate3=this.selectedDate)==null?void 0:(_this$selectedDate3$t=_this$selectedDate3.to)==null?void 0:(_this$selectedDate3$t2=_this$selectedDate3$t.getHours())==null?void 0:_this$selectedDate3$t2.toString())+":"+getStrings((_this$selectedDate4=this.selectedDate)==null?void 0:(_this$selectedDate4$t=_this$selectedDate4.to)==null?void 0:(_this$selectedDate4$t2=_this$selectedDate4$t.getMinutes())==null?void 0:_this$selectedDate4$t2.toString()):'';this.hourRangeIndex_1=this.selectedDate?this.ranged.indexOf(valueHours_1):this.ranged.indexOf(current.getHours().toString()+":"+current.getMinutes().toString());this.hourRangeIndex_2=this.selectedDate?this.ranged.indexOf(valueHours_2):this.ranged.indexOf(current.getHours().toString()+":"+current.getMinutes().toString());return;}this.dayIndex=this.selectedDate?this.days.indexOf(this.selectedDate.getDate().toString()):this.days.indexOf(current.getDate().toString());this.monthIndex=this.selectedDate?this.months.indexOf((this.selectedDate.getMonth()+1).toString()):this.months.indexOf((current.getMonth()+1).toString());this.yearIndex=this.selectedDate?this.years.indexOf(this.selectedDate.getFullYear().toString()):this.years.indexOf(current.getFullYear().toString());if(format==='hh:mm'&&!isRanged){var _this$selectedDate5,_this$selectedDate5$g,_this$selectedDate5$g2,_this$selectedDate6,_this$selectedDate6$g,_this$selectedDate7,_this$selectedDate7$g,_this$selectedDate7$g2,_this$selectedDate8,_this$selectedDate8$g;var valueHours=this.selectedDate?((_this$selectedDate5=this.selectedDate)==null?void 0:(_this$selectedDate5$g=_this$selectedDate5.getHours())==null?void 0:(_this$selectedDate5$g2=_this$selectedDate5$g.toString())==null?void 0:_this$selectedDate5$g2.length)===1?"0"+this.selectedDate.getHours().toString():((_this$selectedDate6=this.selectedDate)==null?void 0:(_this$selectedDate6$g=_this$selectedDate6.getHours())==null?void 0:_this$selectedDate6$g.toString())||'':'';var valueMinute=this.selectedDate?((_this$selectedDate7=this.selectedDate)==null?void 0:(_this$selectedDate7$g=_this$selectedDate7.getMinutes())==null?void 0:(_this$selectedDate7$g2=_this$selectedDate7$g.toString())==null?void 0:_this$selectedDate7$g2.length)===1?"0"+this.selectedDate.getMinutes().toString():((_this$selectedDate8=this.selectedDate)==null?void 0:(_this$selectedDate8$g=_this$selectedDate8.getMinutes())==null?void 0:_this$selectedDate8$g.toString())||'':'';this.hourIndex=this.selectedDate?this.hours.indexOf(valueHours):this.hours.indexOf(current.getHours().toString());this.minuteIndex=this.selectedDate?this.minutes.indexOf(valueMinute):this.minutes.indexOf(current.getMinutes().toString());}}},{key:"renderUpdate",value:function renderUpdate(param){if(param&&param.day!=null){this.dayIndex=param.day;this.updateWheelPiker('DayView',{arrayValue:this.days,value:param.day});}if(param&&param.month!=null){this.monthIndex=param.month;this.updateWheelPiker('MonthView',{arrayValue:this.months,value:param.month});}if(param&&param.year!=null){this.yearIndex=param.year;this.updateWheelPiker('YearView',{arrayValue:this.years,value:param.year});}if(param&&param.hour!=null){this.hourIndex=param.hour;this.updateWheelPiker('HourView',{arrayValue:this.hours,value:param.hour});}if(param&&param.minute!=null){this.minuteIndex=param.minute;this.updateWheelPiker('MinuteView',{arrayValue:this.minutes,value:param.minute});}if(param&&param.hourRange_1!=null){this.hourRangeIndex_1=param.hourRange_1;this.updateWheelPiker('HourRange1',{arrayValue:this.ranged,value:param.hourRange_1});}if(param&&param.hourRange_2!=null){this.hourRangeIndex_2=param.hourRange_2;this.updateWheelPiker('HourRange2',{arrayValue:this.ranged,value:param.hourRange_2});}}},{key:"setStateDate",value:function setStateDate(dayIndex,monthIndex,yearIndex){var _this$days;var day=parseInt(this.days[dayIndex]);var month=parseInt(this.months[monthIndex]);var year=parseInt(this.years[yearIndex]);var isUpDate=false;var minDay=1;var maxDay=_DatePickerHelper.default.getMaxDate(month,year);if(this.maxDay&&year===this.maxYear&&this.maxMonth===month){maxDay=this.maxDay;}if(this.minDay&&year===this.minYear&&this.minMonth===month){minDay=this.minDay;}if(maxDay!==((_this$days=this.days)==null?void 0:_this$days.length)||minDay!==parseInt(this.days[0])){isUpDate=true;}var index=dayIndex;if(isUpDate){var _this$days2;this.days=_DatePickerHelper.default.makeRange(minDay,maxDay);index=day>maxDay?((_this$days2=this.days)==null?void 0:_this$days2.length)-1:day<minDay?0:this.days.indexOf(day.toString());}this.renderUpdate({day:index,month:monthIndex,year:yearIndex});}},{key:"setStateMonth",value:function setStateMonth(dayIndex,monthIndex,yearIndex){var _this$months;var month=parseInt(this.months[monthIndex]);var year=parseInt(this.years[yearIndex]);var isUpDate=false;var minMonth=1;var maxMonth=12;if(this.maxMonth&&year===this.maxYear){maxMonth=this.maxMonth;}if(this.minMonth&&year===this.minYear){minMonth=this.minMonth;}if(maxMonth!==((_this$months=this.months)==null?void 0:_this$months.length)||minMonth!==parseInt(this.months[0])){isUpDate=true;}var index=monthIndex;if(isUpDate){var _this$months2,_this$months3;this.months=_DatePickerHelper.default.makeRange(minMonth,maxMonth);index=month>maxMonth?((_this$months2=this.months)==null?void 0:_this$months2.length)-1:month<minMonth?0:(_this$months3=this.months)==null?void 0:_this$months3.indexOf(month.toString());}this.setStateDate(dayIndex,index,yearIndex);}},{key:"updateWheelPiker",value:function updateWheelPiker(refName,param){var refWheelPicker;if(this.arrayRef){refWheelPicker=this.arrayRef[refName];if(refWheelPicker&&refWheelPicker.update){this.arrayRef[refName].update(param);}}}},{key:"renderColumn",value:function renderColumn(refName,valueInput,arrayValue,label,bottomOffset,callback,reversePreFix){var _this2=this;if(!this.arrayRef){this.arrayRef={};}return _react.default.createElement(_WheelPicker.default,{bottomOffset:bottomOffset,key:refName,refName:refName,ref:function ref(_ref){_this2.arrayRef[refName]=_ref;},value:valueInput,arrayValue:arrayValue,reversePreFix:reversePreFix,preFix:label,onBeginDrag:this.onBeginDrag,onEndDrag:this.onEndDrag,onValueChange:function onValueChange(value){if(callback){callback(value);}},__source:{fileName:_jsxFileName,lineNumber:408,columnNumber:13}});}},{key:"renderHeader",value:function renderHeader(){var _this$props5=this.props,title=_this$props5.title,placeholder=_this$props5.placeholder,body=_this$props5.body,buttonTitle=_this$props5.buttonTitle,onButtonPress=_this$props5.onButtonPress,headerStyle=_this$props5.headerStyle,_this$props5$iconClos=_this$props5.iconClosePosition,iconClosePosition=_this$props5$iconClos===void 0?'right':_this$props5$iconClos;return _react.default.createElement(_core.BottomPopupHeader,{iconClosePosition:iconClosePosition,body:body,onClose:this.hide,title:title||placeholder,buttonTitle:buttonTitle,onButtonPress:onButtonPress,style:[styles.header,headerStyle],__source:{fileName:_jsxFileName,lineNumber:435,columnNumber:13}});}},{key:"renderContent",value:function renderContent(){var _this3=this;var _this$props6=this.props,format=_this$props6.format,labelDay=_this$props6.labelDay,labelMonth=_this$props6.labelMonth,labelYear=_this$props6.labelYear,bottomOffset=_this$props6.bottomOffset,labelMinute=_this$props6.labelMinute,labelHour=_this$props6.labelHour,isRanged=_this$props6.isRanged;if(isRanged&&format==='hh:mm'){return _react.default.createElement(_reactNative.View,{style:styles.container,__source:{fileName:_jsxFileName,lineNumber:481,columnNumber:17}},this.renderDescription(),_react.default.createElement(_reactNative.View,{style:styles.content,__source:{fileName:_jsxFileName,lineNumber:483,columnNumber:21}},this.renderColumn('HourRange1',this.hourRangeIndex_1,this.ranged,_core.SwitchLanguage.from,bottomOffset,function(value){_this3.renderUpdate({hourRange_1:value});},true),this.renderColumn('HourRange2',this.hourRangeIndex_2,this.ranged,_core.SwitchLanguage.to,bottomOffset,function(value){_this3.renderUpdate({hourRange_2:value});},true)));}return _react.default.createElement(_reactNative.View,{style:styles.container,__source:{fileName:_jsxFileName,lineNumber:495,columnNumber:13}},this.renderDescription(),_react.default.createElement(_reactNative.View,{style:styles.content,__source:{fileName:_jsxFileName,lineNumber:497,columnNumber:17}},format.indexOf('dd')!==-1?this.renderColumn('DayView',this.dayIndex,this.days,labelDay||_core.SwitchLanguage.day,bottomOffset,function(value){_this3.renderUpdate({day:value});}):null,format.indexOf('MM')!==-1?this.renderColumn('MonthView',this.monthIndex,this.months,labelMonth||_core.SwitchLanguage.month,bottomOffset,function(value){_this3.setStateDate(_this3.dayIndex,value,_this3.yearIndex);}):null,format.indexOf('YYYY')!==-1?this.renderColumn('YearView',this.yearIndex,this.years,labelYear||_core.SwitchLanguage.year,bottomOffset,function(value){_this3.setStateMonth(_this3.dayIndex,_this3.monthIndex,value);}):null,format.indexOf('hh')!==-1?this.renderColumn('HourView',this.hourIndex,this.hours,labelHour||_core.SwitchLanguage.hour,bottomOffset,function(value){_this3.renderUpdate({hour:value});},true):null,format.indexOf('mm')!==-1?this.renderColumn('MinuteView',this.minuteIndex,this.minutes,labelMinute||_core.SwitchLanguage.minute,bottomOffset,function(value){_this3.renderUpdate({minute:value});},true):null));}},{key:"renderFooter",value:function renderFooter(){var _this$props7=this.props,titleFooter=_this$props7.titleFooter,renderFooter=_this$props7.renderFooter;return renderFooter?renderFooter():_react.default.createElement(_reactNative.View,{style:{paddingHorizontal:12,marginBottom:ifIphoneX(29,11),marginTop:28},__source:{fileName:_jsxFileName,lineNumber:542,columnNumber:13}},_react.default.createElement(_core.Button,{onPress:this.finish,title:titleFooter||_core.SwitchLanguage.confirm,type:"primary",__source:{fileName:_jsxFileName,lineNumber:543,columnNumber:17}}));}},{key:"render",value:function render(){var style=this.props.style;return _react.default.createElement(_reactNative.View,{style:[styles.view,style],__source:{fileName:_jsxFileName,lineNumber:556,columnNumber:13}},this.renderHeader(),this.renderContent(),this.renderFooter());}}]);return DatePicker;}(_react.Component);DatePicker.propTypes={selectedDate:_propTypes.default.any,format:_propTypes.default.string,placeholder:_propTypes.default.string,minDate:_propTypes.default.string,maxDate:_propTypes.default.string,titleFooter:_propTypes.default.string,title:_propTypes.default.string,labelMonth:_propTypes.default.string,labelHour:_propTypes.default.string,labelMinute:_propTypes.default.string,labelDay:_propTypes.default.string,labelYear:_propTypes.default.string,onSelected:_propTypes.default.func,bottomOffset:_propTypes.default.number,renderDescription:_propTypes.default.any,description:_propTypes.default.string,renderFooter:_propTypes.default.any,style:_propTypes.default.any};DatePicker.defaultProps={format:'dd/MM/YYYY',placeholder:_core.SwitchLanguage.chooseDate,minDate:'1/1/1970',bottomOffset:0};var _default=DatePicker;exports.default=_default;
1
+ /* eslint-disable max-len */
2
+ /* eslint-disable react/no-unused-prop-types */
3
+ import React, { Component } from 'react';
4
+ import {
5
+ View, StyleSheet, Dimensions
6
+ } from 'react-native';
7
+ import PropTypes from 'prop-types';
8
+ import {
9
+ BottomPopupHeader, Button, Text, ScreenUtils, SwitchLanguage, Spacing, Colors
10
+ } from '@momo-kits/core';
11
+ import WheelPicker from './WheelPicker';
12
+ import DatePickerHelper from './helper/DatePickerHelper';
13
+
14
+ const { ifIphoneX } = ScreenUtils;
15
+
16
+ const widthScreen = Dimensions.get('window').width;
17
+ const styles = StyleSheet.create({
18
+ container: {
19
+ width: widthScreen,
20
+ alignItems: 'center',
21
+ justifyContent: 'center',
22
+ paddingHorizontal: 6,
23
+ },
24
+ content: {
25
+ flexDirection: 'row',
26
+ width: widthScreen,
27
+ alignItems: 'center',
28
+ justifyContent: 'center',
29
+ paddingHorizontal: 21,
30
+ paddingTop: 12
31
+ },
32
+ column: {
33
+ borderWidth: 2,
34
+ borderColor: 'red',
35
+ },
36
+ header: {
37
+ borderTopLeftRadius: 12,
38
+ borderTopRightRadius: 12,
39
+ //backgroundColor:'red',
40
+ height: 64,
41
+ borderBottomColor: Colors.black_04,
42
+ borderBottomWidth: 1,
43
+ alignItems:'center',
44
+ justifyContent:'center',
45
+ marginBottom: 0, // Spacing.S,
46
+ },
47
+ view: {
48
+ backgroundColor: 'white',
49
+ borderTopRightRadius: 16,
50
+ borderTopLeftRadius: 16,
51
+ overflow: 'hidden'
52
+ },
53
+ titleCancel: {
54
+ fontSize: 17,
55
+ color: '#4A90E2'
56
+ },
57
+ titleConfirm: {
58
+ fontSize: 17,
59
+ color: '#4A90E2',
60
+ textAlign: 'right'
61
+ }
62
+ });
63
+
64
+ const getStrings = (strings) => (strings?.length === 1 ? `0${strings}` : strings);
65
+ class DatePicker extends Component {
66
+ constructor(props) {
67
+ super(props);
68
+ this.initDefault(props);
69
+ }
70
+
71
+ // onBackPress = () => {
72
+ // this.hide();
73
+ // return true;
74
+ // }
75
+
76
+ componentDidMount() {
77
+ // BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
78
+ // BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
79
+
80
+ // setTimeout(() => {
81
+ // BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
82
+ // BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
83
+ // }, 200);
84
+
85
+ this.setStateMonth(this.dayIndex, this.monthIndex, this.yearIndex);
86
+ const { onCreateRef } = this.props;
87
+ if (typeof onCreateRef === 'function') onCreateRef(this);
88
+ }
89
+
90
+ componentWillUnmount() {
91
+ // BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
92
+ }
93
+
94
+ initDefault(props) {
95
+ if (props) {
96
+ const propsMinDate = props.minDate ? props.minDate : props.minValue;
97
+ const propsMaxDate = props.maxDate ? props.maxDate : props.maxValue;
98
+ const propsSelectedDate = props.selectedDate ? props.selectedDate : props.value;
99
+
100
+ if (propsMinDate) {
101
+ this.minDate = DatePickerHelper.formatDate(propsMinDate, props.format);
102
+ } else if (props.maxAge) {
103
+ this.minDate = DatePickerHelper.calculateDateSinceYearAgo(props.maxAge);
104
+ } else {
105
+ this.minDate = null;
106
+ }
107
+
108
+ if (propsMaxDate) {
109
+ this.maxDate = DatePickerHelper.formatDate(propsMaxDate, props.format);
110
+ } else if (props.minAge) {
111
+ this.maxDate = DatePickerHelper.calculateDateSinceYearAgo(props.minAge);
112
+ } else {
113
+ this.maxDate = null;
114
+ }
115
+
116
+ if (this.maxDate && this.minDate && this.maxDate.getTime() < this.minDate.getTime()) {
117
+ this.maxDate = null;
118
+ this.minDate = null;
119
+ }
120
+
121
+ if (propsSelectedDate) {
122
+ this.selectedDate = DatePickerHelper.formatDate(propsSelectedDate, props.format);
123
+ if (this.selectedDate) {
124
+ if (this.minDate && this.selectedDate.getTime() < this.minDate.getTime()) {
125
+ this.selectedDate = null;
126
+ } else if (this.maxDate && this.maxDate.getTime() < this.selectedDate.getTime()) {
127
+ this.selectedDate = null;
128
+ }
129
+ }
130
+ } else {
131
+ this.selectedDate = null;
132
+ }
133
+ if (props.format === 'hh:mm') {
134
+ this.selectedDate = props.isRanged ? {
135
+ from: DatePickerHelper.formatDate(propsSelectedDate.from, props.format),
136
+ to: DatePickerHelper.formatDate(propsSelectedDate.to, props.format)
137
+ } : DatePickerHelper.formatDate(propsSelectedDate, props.format);
138
+ }
139
+ }
140
+ this.createDataDate(this.minDate, this.maxDate, props.minuteArray, props.rangeHour);
141
+ this.selectedIndexDate();
142
+ }
143
+
144
+ createDataDate(minDate, maxDate, minuteArray, rangeHour) {
145
+ this.minDay = 1;
146
+ this.minMonth = 1;
147
+ this.minYear = 1970;
148
+
149
+ this.maxDay = 31;
150
+ this.maxMonth = 12;
151
+ this.maxYear = 2100;
152
+ if (minDate) {
153
+ this.minDay = minDate.getDate();
154
+ this.minMonth = minDate.getMonth() + 1;
155
+ this.minYear = minDate.getFullYear();
156
+ }
157
+ if (maxDate) {
158
+ this.maxDay = maxDate.getDate();
159
+ this.maxMonth = maxDate.getMonth() + 1;
160
+ this.maxYear = maxDate.getFullYear();
161
+ }
162
+ this.days = DatePickerHelper.makeRange(1, 31);
163
+ this.months = DatePickerHelper.makeRange(1, 12);
164
+ this.years = DatePickerHelper.makeRange(this.minYear, this.maxYear);
165
+ this.hours = DatePickerHelper.makeRange(0, 23, true);
166
+ if (Array.isArray(minuteArray)) {
167
+ this.minutes = minuteArray;
168
+ } else {
169
+ this.minutes = DatePickerHelper.makeRange(0, 59, true);
170
+ }
171
+ this.ranged = rangeHour || DatePickerHelper.arrayRange;
172
+ }
173
+
174
+ selectedIndexDate() {
175
+ const { format, isRanged } = this.props;
176
+ let current = new Date();
177
+ if (this.minDate && current.getTime() < this.minDate.getTime()) {
178
+ current = this.minDate;
179
+ } else if (this.maxDate && current.getTime() > this.maxDate.getTime()) {
180
+ current = this.maxDate;
181
+ }
182
+ if (format === 'hh:mm' && isRanged) {
183
+ const valueHours_1 = this.selectedDate?.from ? `${getStrings(this.selectedDate?.from?.getHours()?.toString())}:${getStrings(this.selectedDate?.from?.getMinutes()?.toString())}` : '00:00';
184
+ const valueHours_2 = this.selectedDate?.to ? `${getStrings(this.selectedDate?.to?.getHours()?.toString())}:${getStrings(this.selectedDate?.to?.getMinutes()?.toString())}` : '00:00';
185
+ this.hourRangeIndex_1 = valueHours_1
186
+ ? this.ranged.indexOf(valueHours_1)
187
+ : this.ranged.indexOf(`${current.getHours().toString()}:${current.getMinutes().toString()}`);
188
+
189
+ this.hourRangeIndex_2 = valueHours_2
190
+ ? this.ranged.indexOf(valueHours_2)
191
+ : this.ranged.indexOf(`${current.getHours().toString()}:${current.getMinutes().toString()}`);
192
+ return;
193
+ }
194
+ this.dayIndex = this.selectedDate
195
+ ? this.days.indexOf(this.selectedDate.getDate().toString())
196
+ : this.days.indexOf(current.getDate().toString());
197
+
198
+ this.monthIndex = this.selectedDate
199
+ ? this.months.indexOf((this.selectedDate.getMonth() + 1).toString())
200
+ : this.months.indexOf((current.getMonth() + 1).toString());
201
+
202
+ this.yearIndex = this.selectedDate
203
+ ? this.years.indexOf(this.selectedDate.getFullYear().toString())
204
+ : this.years.indexOf(current.getFullYear().toString());
205
+
206
+ if (format === 'hh:mm' && !isRanged) {
207
+ const valueHours = this.selectedDate ? this.selectedDate?.getHours()?.toString()?.length === 1 ? `0${this.selectedDate.getHours().toString()}` : this.selectedDate?.getHours()?.toString() || '' : '';
208
+ const valueMinute = this.selectedDate ? this.selectedDate?.getMinutes()?.toString()?.length === 1 ? `0${this.selectedDate.getMinutes().toString()}` : this.selectedDate?.getMinutes()?.toString() || '' : '';
209
+
210
+ this.hourIndex = this.selectedDate
211
+ ? this.hours.indexOf(valueHours)
212
+ : this.hours.indexOf(current.getHours().toString().length === 1 ? `0${current.getHours().toString()}` : current.getHours().toString());
213
+
214
+ this.minuteIndex = this.selectedDate
215
+ ? this.minutes.indexOf(valueMinute)
216
+ : this.minutes.indexOf(current.getMinutes().toString().length === 1 ? `0${current.getMinutes().toString()}` : current.getMinutes().toString());
217
+ }
218
+ }
219
+
220
+ onBeginDrag = (refName) => {
221
+ // eslint-disable-next-line no-restricted-syntax
222
+ for (const i in this.arrayRef) {
223
+ if (i && i !== refName && this.arrayRef && this.arrayRef[i] && this.arrayRef[i].update) {
224
+ this.arrayRef[i].setScrollEnabled(false);
225
+ }
226
+ }
227
+ }
228
+
229
+ onEndDrag = (refName) => {
230
+ // eslint-disable-next-line no-restricted-syntax
231
+ for (const i in this.arrayRef) {
232
+ if (i && i !== refName && this.arrayRef && this.arrayRef[i] && this.arrayRef[i].update) {
233
+ this.arrayRef[i].setScrollEnabled(true);
234
+ }
235
+ }
236
+ }
237
+
238
+ show = () => {
239
+
240
+ }
241
+
242
+ hide = () => {
243
+ const { onClose, requestClose } = this.props;
244
+ if (requestClose && typeof requestClose === 'function') {
245
+ requestClose(() => {
246
+ if (onClose && typeof onClose === 'function') {
247
+ onClose();
248
+ }
249
+ });
250
+ }
251
+ }
252
+
253
+ finish = () => {
254
+ const {
255
+ format, onSelected, isRanged, selectedDate
256
+ } = this.props;
257
+ if (isRanged && format === 'hh:mm') {
258
+ const from = this.ranged[this.hourRangeIndex_1];
259
+ const to = this.ranged[this.hourRangeIndex_2];
260
+ if (onSelected && typeof onSelected === 'function') {
261
+ onSelected({ from, to });
262
+ }
263
+ this.hide();
264
+ return;
265
+ }
266
+ const day = this.days[this.dayIndex];
267
+ const month = this.months[this.monthIndex];
268
+ const year = this.years[this.yearIndex];
269
+ const hour = this.hours[this.hourIndex] || '00';
270
+ const minute = this.minutes[this.minuteIndex] || '00';
271
+ const selectedDateFormatted = DatePickerHelper.toString(day, month, year, hour, minute, format);
272
+ if (onSelected && typeof onSelected === 'function') {
273
+ onSelected(selectedDateFormatted || selectedDate || DatePickerHelper.getCurrentDate(format));
274
+ }
275
+ this.hide();
276
+ }
277
+
278
+ renderUpdate(param) {
279
+ if (param && param.day != null) {
280
+ this.dayIndex = param.day;
281
+ this.updateWheelPiker('DayView', {
282
+ arrayValue: this.days,
283
+ value: param.day
284
+ });
285
+ }
286
+
287
+ if (param && param.month != null) {
288
+ this.monthIndex = param.month;
289
+ this.updateWheelPiker('MonthView', {
290
+ arrayValue: this.months,
291
+ value: param.month
292
+ });
293
+ }
294
+
295
+ if (param && param.year != null) {
296
+ this.yearIndex = param.year;
297
+ this.updateWheelPiker('YearView', {
298
+ arrayValue: this.years,
299
+ value: param.year
300
+ });
301
+ }
302
+
303
+ if (param && param.hour != null) {
304
+ this.hourIndex = param.hour;
305
+ this.updateWheelPiker('HourView', {
306
+ arrayValue: this.hours,
307
+ value: param.hour
308
+ });
309
+ }
310
+
311
+ if (param && param.minute != null) {
312
+ this.minuteIndex = param.minute;
313
+ this.updateWheelPiker('MinuteView', {
314
+ arrayValue: this.minutes,
315
+ value: param.minute
316
+ });
317
+ }
318
+
319
+ if (param && param.hourRange_1 != null) {
320
+ this.hourRangeIndex_1 = param.hourRange_1;
321
+ this.updateWheelPiker('HourRange1', {
322
+ arrayValue: this.ranged,
323
+ value: param.hourRange_1
324
+ });
325
+ }
326
+
327
+ if (param && param.hourRange_2 != null) {
328
+ this.hourRangeIndex_2 = param.hourRange_2;
329
+ this.updateWheelPiker('HourRange2', {
330
+ arrayValue: this.ranged,
331
+ value: param.hourRange_2
332
+ });
333
+ }
334
+ }
335
+
336
+ setStateDate(dayIndex, monthIndex, yearIndex) {
337
+ const day = parseInt(this.days[dayIndex]);
338
+ const month = parseInt(this.months[monthIndex]);
339
+ const year = parseInt(this.years[yearIndex]);
340
+
341
+ let isUpDate = false;
342
+ let minDay = 1;
343
+ let maxDay = DatePickerHelper.getMaxDate(month, year);
344
+
345
+ if (this.maxDay && year === this.maxYear && this.maxMonth === month) {
346
+ maxDay = this.maxDay;
347
+ }
348
+
349
+ if (this.minDay && year === this.minYear && this.minMonth === month) {
350
+ minDay = this.minDay;
351
+ }
352
+
353
+ if (maxDay !== this.days?.length || minDay !== parseInt(this.days[0])) {
354
+ isUpDate = true;
355
+ }
356
+
357
+ let index = dayIndex;
358
+
359
+ if (isUpDate) {
360
+ this.days = DatePickerHelper.makeRange(minDay, maxDay);
361
+ index = day > maxDay ? this.days?.length - 1 : (day < minDay ? 0 : this.days.indexOf(day.toString()));
362
+ }
363
+
364
+ this.renderUpdate({
365
+ day: index,
366
+ month: monthIndex,
367
+ year: yearIndex
368
+ });
369
+ }
370
+
371
+ setStateMonth(dayIndex, monthIndex, yearIndex) {
372
+ const month = parseInt(this.months[monthIndex]);
373
+ const year = parseInt(this.years[yearIndex]);
374
+
375
+ let isUpDate = false;
376
+ let minMonth = 1;
377
+ let maxMonth = 12;
378
+
379
+ if (this.maxMonth && year === this.maxYear) {
380
+ maxMonth = this.maxMonth;
381
+ }
382
+ if (this.minMonth && year === this.minYear) {
383
+ minMonth = this.minMonth;
384
+ }
385
+ if (maxMonth !== this.months?.length || minMonth !== parseInt(this.months[0])) {
386
+ isUpDate = true;
387
+ }
388
+ let index = monthIndex;
389
+ if (isUpDate) {
390
+ this.months = DatePickerHelper.makeRange(minMonth, maxMonth);
391
+ index = month > maxMonth
392
+ ? this.months?.length - 1
393
+ : (month < minMonth ? 0 : this.months?.indexOf(month.toString()));
394
+ }
395
+ this.setStateDate(dayIndex, index, yearIndex);
396
+ }
397
+
398
+ updateWheelPiker(refName, param) {
399
+ let refWheelPicker;
400
+ if (this.arrayRef) {
401
+ refWheelPicker = this.arrayRef[refName];
402
+ if (refWheelPicker && refWheelPicker.update) {
403
+ this.arrayRef[refName].update(param);
404
+ }
405
+ }
406
+ }
407
+
408
+ renderColumn(refName, valueInput, arrayValue, label, bottomOffset, callback, reversePreFix) {
409
+ if (!this.arrayRef) {
410
+ this.arrayRef = {};
411
+ }
412
+ return (
413
+ <WheelPicker
414
+ bottomOffset={bottomOffset}
415
+ key={refName}
416
+ refName={refName}
417
+ ref={(ref) => {
418
+ this.arrayRef[refName] = ref;
419
+ }}
420
+ value={valueInput}
421
+ arrayValue={arrayValue}
422
+ reversePreFix={reversePreFix}
423
+ preFix={label}
424
+ onBeginDrag={this.onBeginDrag}
425
+ onEndDrag={this.onEndDrag}
426
+ onValueChange={(value) => {
427
+ if (callback) {
428
+ callback(value);
429
+ }
430
+ }}
431
+ />
432
+ );
433
+ }
434
+
435
+ renderHeader() {
436
+ const {
437
+ title, placeholder, body, buttonTitle, onButtonPress, headerStyle, iconClosePosition = 'right', iconType
438
+ } = this.props;
439
+ return (
440
+ <BottomPopupHeader
441
+ iconType={iconType}
442
+ iconClosePosition={iconClosePosition}
443
+ body={body}
444
+ onClose={this.hide}
445
+ title={title || placeholder}
446
+ buttonTitle={buttonTitle}
447
+ onButtonPress={onButtonPress}
448
+ style={[styles.header, headerStyle]}
449
+ />
450
+ );
451
+ // return (
452
+ // <View style={styles.header}>
453
+ // <TouchableOpacity
454
+ // onPress={this.hide}
455
+ // >
456
+ // <Text style={styles.titleCancel}>
457
+ // {titleCancel}
458
+ // </Text>
459
+ // </TouchableOpacity>
460
+ // <Text style={styles.title}>
461
+ // {title || placeholder}
462
+ // </Text>
463
+ // <TouchableOpacity onPress={this.finish}>
464
+ // <Text style={styles.titleConfirm}>
465
+ // {titleConfirm}
466
+
467
+ // </Text>
468
+ // </TouchableOpacity>
469
+ // </View>
470
+ // );
471
+ }
472
+
473
+ renderDescription = () => {
474
+ const { description, descriptionStyle, renderDescription } = this.props;
475
+ if (renderDescription) return renderDescription;
476
+ return description ? (
477
+ <Text.Title style={[{ marginBottom: Spacing.S }, descriptionStyle]}>{description}</Text.Title>
478
+ ) : null;
479
+ };
480
+
481
+ renderContent() {
482
+ const {
483
+ format, labelDay, labelMonth, labelYear, bottomOffset, labelMinute, labelHour, isRanged
484
+ } = this.props;
485
+ if (isRanged && format === 'hh:mm') {
486
+ return (
487
+ <View style={styles.container}>
488
+ {this.renderDescription()}
489
+ <View style={styles.content}>
490
+ {this.renderColumn('HourRange1', this.hourRangeIndex_1, this.ranged, SwitchLanguage.from, bottomOffset, (value) => {
491
+ this.renderUpdate({ hourRange_1: value });
492
+ }, true)}
493
+ {this.renderColumn('HourRange2', this.hourRangeIndex_2, this.ranged, SwitchLanguage.to, bottomOffset, (value) => {
494
+ this.renderUpdate({ hourRange_2: value });
495
+ }, true)}
496
+ </View>
497
+ </View>
498
+ );
499
+ }
500
+ return (
501
+ <View style={styles.container}>
502
+ {this.renderDescription()}
503
+ <View style={styles.content}>
504
+ {
505
+ format.indexOf('dd') !== -1
506
+ ? this.renderColumn('DayView', this.dayIndex, this.days, labelDay || SwitchLanguage.day, bottomOffset, (value) => {
507
+ this.renderUpdate({ day: value });
508
+ })
509
+ : null
510
+ }
511
+ {
512
+ format.indexOf('MM') !== -1
513
+ ? this.renderColumn('MonthView', this.monthIndex, this.months, labelMonth || SwitchLanguage.month, bottomOffset, (value) => {
514
+ this.setStateDate(this.dayIndex, value, this.yearIndex);
515
+ })
516
+ : null
517
+ }
518
+ {
519
+ format.indexOf('YYYY') !== -1
520
+ ? this.renderColumn('YearView', this.yearIndex, this.years, labelYear || SwitchLanguage.year, bottomOffset, (value) => {
521
+ this.setStateMonth(this.dayIndex, this.monthIndex, value);
522
+ })
523
+ : null
524
+ }
525
+ {
526
+ format.indexOf('hh') !== -1
527
+ ? this.renderColumn('HourView', this.hourIndex, this.hours, labelHour || SwitchLanguage.hour, bottomOffset, (value) => {
528
+ this.renderUpdate({ hour: value });
529
+ }, true)
530
+ : null
531
+ }
532
+ {
533
+ format.indexOf('mm') !== -1
534
+ ? this.renderColumn('MinuteView', this.minuteIndex, this.minutes, labelMinute || SwitchLanguage.minute, bottomOffset, (value) => {
535
+ this.renderUpdate({ minute: value });
536
+ }, true)
537
+ : null
538
+ }
539
+ </View>
540
+
541
+ </View>
542
+ );
543
+ }
544
+
545
+ renderFooter() {
546
+ const { titleFooter, renderFooter } = this.props;
547
+ return renderFooter ? renderFooter() : (
548
+ <View style={{ paddingHorizontal: 12, marginBottom: ifIphoneX(35, 12), marginTop: 16 }}>
549
+ <Button
550
+ onPress={this.finish}
551
+ title={titleFooter || SwitchLanguage.confirm}
552
+ type="primary"
553
+ />
554
+ </View>
555
+
556
+ );
557
+ }
558
+
559
+ render() {
560
+ const { style } = this.props;
561
+ return (
562
+ <View style={[styles.view, style]}>
563
+ {this.renderHeader()}
564
+ {this.renderContent()}
565
+ {this.renderFooter()}
566
+ </View>
567
+ );
568
+ }
569
+ }
570
+
571
+ DatePicker.propTypes = {
572
+ selectedDate: PropTypes.any,
573
+ format: PropTypes.string,
574
+ placeholder: PropTypes.string,
575
+ minDate: PropTypes.string,
576
+ maxDate: PropTypes.string,
577
+ titleFooter: PropTypes.string,
578
+ title: PropTypes.string,
579
+ // titleCancel: PropTypes.string,
580
+ // titleConfirm: PropTypes.string,
581
+ labelMonth: PropTypes.string,
582
+ labelHour: PropTypes.string,
583
+ labelMinute: PropTypes.string,
584
+ labelDay: PropTypes.string,
585
+ labelYear: PropTypes.string,
586
+ onSelected: PropTypes.func,
587
+ bottomOffset: PropTypes.number,
588
+ renderDescription: PropTypes.any,
589
+ description: PropTypes.string,
590
+ renderFooter: PropTypes.any,
591
+ style: PropTypes.any,
592
+ rangeHour: PropTypes.array,
593
+ iconClosePosition: PropTypes.oneOf(['left', 'right']),
594
+ iconType: PropTypes.oneOf(['icon', 'text']),
595
+ };
596
+
597
+ DatePicker.defaultProps = {
598
+ format: 'dd/MM/YYYY',
599
+ placeholder: SwitchLanguage.chooseDate,
600
+ minDate: '1/1/1970',
601
+ // titleCancel: SwitchLanguage.cancel,
602
+ // titleConfirm: SwitchLanguage.done,
603
+ // labelMonth: SwitchLanguage.month,
604
+ // labelDay: SwitchLanguage.day,
605
+ // labelYear: SwitchLanguage.year,
606
+ // labelHour: SwitchLanguage.hour,
607
+ // labelMinute: SwitchLanguage.minute,
608
+ bottomOffset: 0,
609
+ };
610
+
611
+ export default DatePicker;
612
+