@momo-kits/date-picker 0.0.32-beta → 0.0.32
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.
- package/datePicker/DatePicker.js +612 -1
- package/datePicker/DatePickerInput.js +108 -1
- package/datePicker/ListPicker.js +193 -0
- package/datePicker/WheelPicker.js +303 -1
- package/datePicker/custom/ScrollCustom.js +60 -1
- package/datePicker/helper/DatePickerHelper.js +205 -1
- package/index.js +9 -1
- package/package.json +5 -12
- package/publish.sh +2 -2
- package/babel.config.js +0 -1
|
@@ -1 +1,108 @@
|
|
|
1
|
-
|
|
1
|
+
import PropTypes from 'prop-types';
|
|
2
|
+
import React, { useState } from 'react';
|
|
3
|
+
import { StyleSheet } from 'react-native';
|
|
4
|
+
import { isEmpty } from 'lodash';
|
|
5
|
+
import {
|
|
6
|
+
IconSource, TouchableOpacity, Text, Colors, ValueUtil, Image
|
|
7
|
+
} from '@momo-kits/core';
|
|
8
|
+
import DatePicker from './DatePicker';
|
|
9
|
+
|
|
10
|
+
const DatePickerInput = ({
|
|
11
|
+
navigator, defaultDate, maxDate, minDate, format = 'dd/MM/YYYY', onSelected, onClose,
|
|
12
|
+
style, textStyle, icon, iconStyle, error, disabled, minuteArray, title, showRightIcon
|
|
13
|
+
}) => {
|
|
14
|
+
const [selected, setSelected] = useState(defaultDate || '');
|
|
15
|
+
|
|
16
|
+
const onPress = () => {
|
|
17
|
+
navigator?.showBottom?.({
|
|
18
|
+
screen: DatePicker,
|
|
19
|
+
params: {
|
|
20
|
+
dragDisabled: true,
|
|
21
|
+
minDate,
|
|
22
|
+
maxDate,
|
|
23
|
+
format,
|
|
24
|
+
title,
|
|
25
|
+
selectedDate: selected,
|
|
26
|
+
onClose,
|
|
27
|
+
minuteArray,
|
|
28
|
+
onSelected: (dateSelected) => {
|
|
29
|
+
onSelected?.(dateSelected);
|
|
30
|
+
setSelected(dateSelected);
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
let otherStyle = {};
|
|
36
|
+
if (error) otherStyle = styles.error;
|
|
37
|
+
if (disabled) otherStyle = styles.disabled;
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<TouchableOpacity
|
|
41
|
+
disabled={disabled}
|
|
42
|
+
onPress={onPress}
|
|
43
|
+
style={[styles.container, ValueUtil.extractStyle(style), otherStyle]}
|
|
44
|
+
>
|
|
45
|
+
<Text.Title style={[styles.defaultText, ValueUtil.extractStyle(textStyle), isEmpty(selected) ? { color: Colors.black_09 } : {}]}>{isEmpty(selected) ? format : selected}</Text.Title>
|
|
46
|
+
{showRightIcon ? (
|
|
47
|
+
<Image
|
|
48
|
+
source={icon || IconSource.ic_calendar}
|
|
49
|
+
style={[styles.icon, ValueUtil.extractStyle(iconStyle)]}
|
|
50
|
+
/>
|
|
51
|
+
) : <View />}
|
|
52
|
+
</TouchableOpacity>
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
DatePickerInput.propTypes = {
|
|
57
|
+
defaultDate: PropTypes.string,
|
|
58
|
+
format: PropTypes.oneOf(['hh:mm', 'MM/YYYY', 'dd/MM', 'dd/MM/YYYY']),
|
|
59
|
+
icon: PropTypes.any,
|
|
60
|
+
maxDate: PropTypes.string,
|
|
61
|
+
minDate: PropTypes.string,
|
|
62
|
+
navigator: PropTypes.object,
|
|
63
|
+
onClose: PropTypes.func,
|
|
64
|
+
onSelected: PropTypes.func,
|
|
65
|
+
iconStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
|
66
|
+
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
|
67
|
+
textStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
|
68
|
+
error: PropTypes.bool,
|
|
69
|
+
disabled: PropTypes.bool,
|
|
70
|
+
minuteArray: PropTypes.arrayOf(PropTypes.string),
|
|
71
|
+
showRightIcon: PropTypes.bool,
|
|
72
|
+
title: PropTypes.string
|
|
73
|
+
};
|
|
74
|
+
DatePickerInput.defaultProps = {
|
|
75
|
+
showRightIcon: true
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export default DatePickerInput;
|
|
79
|
+
|
|
80
|
+
const styles = StyleSheet.create({
|
|
81
|
+
icon: {
|
|
82
|
+
width: 16,
|
|
83
|
+
height: 16,
|
|
84
|
+
resizeMode: 'contain',
|
|
85
|
+
tintColor: Colors.black_09
|
|
86
|
+
},
|
|
87
|
+
container: {
|
|
88
|
+
flexDirection: 'row',
|
|
89
|
+
height: 42,
|
|
90
|
+
borderRadius: 4,
|
|
91
|
+
borderColor: Colors.black_06,
|
|
92
|
+
borderWidth: 1,
|
|
93
|
+
alignItems: 'center',
|
|
94
|
+
justifyContent: 'space-between',
|
|
95
|
+
paddingHorizontal: 12,
|
|
96
|
+
backgroundColor: Colors.black_01
|
|
97
|
+
},
|
|
98
|
+
error: {
|
|
99
|
+
borderColor: Colors.red_05
|
|
100
|
+
},
|
|
101
|
+
defaultText: {
|
|
102
|
+
// fontSize: 14
|
|
103
|
+
},
|
|
104
|
+
disabled: {
|
|
105
|
+
backgroundColor: Colors.black_05,
|
|
106
|
+
borderWidth: 0
|
|
107
|
+
}
|
|
108
|
+
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import React, { Component } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Dimensions, StyleSheet, View
|
|
4
|
+
} from 'react-native';
|
|
5
|
+
import {
|
|
6
|
+
Button, IconSource, TouchableOpacity, Text, Colors, Image, DeviceUtils, SwitchLanguage
|
|
7
|
+
} from '@momo-kits/core';
|
|
8
|
+
import WheelPicker from './WheelPicker';
|
|
9
|
+
|
|
10
|
+
const { isIphoneX } = DeviceUtils;
|
|
11
|
+
|
|
12
|
+
const widthScreen = Dimensions.get('window').width;
|
|
13
|
+
|
|
14
|
+
export default class ListPicker extends Component {
|
|
15
|
+
constructor(props) {
|
|
16
|
+
super(props);
|
|
17
|
+
this.data = 0;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
hide = () => {
|
|
21
|
+
const { onClose, requestClose } = this.props;
|
|
22
|
+
if (requestClose && typeof requestClose === 'function') {
|
|
23
|
+
requestClose(() => {
|
|
24
|
+
if (onClose && typeof onClose === 'function') {
|
|
25
|
+
onClose();
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
onBeginDrag = () => {
|
|
32
|
+
if (this.refs.picker) {
|
|
33
|
+
this.refs.picker.setScrollEnabled(false);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
onEndDrag = () => {
|
|
38
|
+
if (this.refs.picker) {
|
|
39
|
+
this.refs.picker.setScrollEnabled(true);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
onChangeValue = (value) => {
|
|
44
|
+
const { callback } = this.props;
|
|
45
|
+
if (typeof callback === 'function') {
|
|
46
|
+
callback(value);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
onPressClear = () => {
|
|
51
|
+
this.hide();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
onPressChoose = () => {
|
|
55
|
+
this.onChangeValue(this.data);
|
|
56
|
+
this.hide();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
renderRight = () => (
|
|
60
|
+
<TouchableOpacity
|
|
61
|
+
onPress={this.onPressChoose}
|
|
62
|
+
>
|
|
63
|
+
<Text.Title style={{ fontWeight: 'bold', color: Colors.pink_05 }}>{SwitchLanguage.save}</Text.Title>
|
|
64
|
+
</TouchableOpacity>
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
renderLeftIcon = () => (
|
|
68
|
+
<TouchableOpacity onPress={() => this.hide()}>
|
|
69
|
+
<Image
|
|
70
|
+
source={IconSource.ic_close_x_24}
|
|
71
|
+
style={{ width: 25, height: 25, tintColor: Colors.black_17 }}
|
|
72
|
+
/>
|
|
73
|
+
</TouchableOpacity>
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
renderHeader() {
|
|
77
|
+
const { title = '' } = this.props;
|
|
78
|
+
return (
|
|
79
|
+
<View style={styles.header}>
|
|
80
|
+
{this.renderLeftIcon()}
|
|
81
|
+
<Text style={styles.title}>
|
|
82
|
+
{title}
|
|
83
|
+
</Text>
|
|
84
|
+
{this.props.typeStyle === 'aboveAction' ? this.renderRight() : <View style={{ width: 25 }} />}
|
|
85
|
+
</View>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
renderContent() {
|
|
90
|
+
const { arrData = [], stylePicker, initValue } = this.props;
|
|
91
|
+
return (
|
|
92
|
+
<View style={styles.contentStyle}>
|
|
93
|
+
<WheelPicker
|
|
94
|
+
stylePicker={stylePicker}
|
|
95
|
+
bottomOffset={1}
|
|
96
|
+
value={initValue}
|
|
97
|
+
key="picker"
|
|
98
|
+
ref="picker"
|
|
99
|
+
arrayValue={arrData}
|
|
100
|
+
onBeginDrag={this.onBeginDrag}
|
|
101
|
+
onEndDrag={this.onEndDrag}
|
|
102
|
+
onValueChange={(value) => {
|
|
103
|
+
this.data = value;
|
|
104
|
+
}}
|
|
105
|
+
/>
|
|
106
|
+
|
|
107
|
+
</View>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
render() {
|
|
112
|
+
return (
|
|
113
|
+
<View style={styles.view}>
|
|
114
|
+
{this.renderHeader()}
|
|
115
|
+
{this.renderContent()}
|
|
116
|
+
{
|
|
117
|
+
this.props.typeStyle === 'aboveAction'
|
|
118
|
+
? null
|
|
119
|
+
: (
|
|
120
|
+
<View style={styles.btnView}>
|
|
121
|
+
<Button
|
|
122
|
+
style={{ flex: 1 }}
|
|
123
|
+
title={SwitchLanguage.cancel}
|
|
124
|
+
buttonStyle={{ backgroundColor: Colors.second_text_color }}
|
|
125
|
+
onPress={this.onPressClear}
|
|
126
|
+
/>
|
|
127
|
+
<Button
|
|
128
|
+
style={{ flex: 1, marginLeft: 15 }}
|
|
129
|
+
title={SwitchLanguage.choose}
|
|
130
|
+
type="primary"
|
|
131
|
+
onPress={this.onPressChoose}
|
|
132
|
+
/>
|
|
133
|
+
</View>
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
</View>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const styles = StyleSheet.create({
|
|
142
|
+
container: {
|
|
143
|
+
flexDirection: 'row',
|
|
144
|
+
width: widthScreen,
|
|
145
|
+
alignItems: 'center',
|
|
146
|
+
justifyContent: 'center'
|
|
147
|
+
},
|
|
148
|
+
contentStyle: {
|
|
149
|
+
height: 230,
|
|
150
|
+
},
|
|
151
|
+
column: {
|
|
152
|
+
borderWidth: 2,
|
|
153
|
+
borderColor: 'red',
|
|
154
|
+
},
|
|
155
|
+
header: {
|
|
156
|
+
flexDirection: 'row',
|
|
157
|
+
height: 50,
|
|
158
|
+
width: widthScreen,
|
|
159
|
+
paddingHorizontal: 16,
|
|
160
|
+
alignItems: 'center',
|
|
161
|
+
justifyContent: 'space-between',
|
|
162
|
+
backgroundColor: '#fff',
|
|
163
|
+
borderBottomWidth: 1,
|
|
164
|
+
borderColor: '#DADADA',
|
|
165
|
+
},
|
|
166
|
+
view: {
|
|
167
|
+
backgroundColor: 'white',
|
|
168
|
+
paddingBottom: 10 + (isIphoneX() ? 10 : 0)
|
|
169
|
+
},
|
|
170
|
+
title: {
|
|
171
|
+
fontWeight: 'bold',
|
|
172
|
+
color: Colors.black_17,
|
|
173
|
+
flex: 1,
|
|
174
|
+
fontSize: 16,
|
|
175
|
+
textAlign: 'center'
|
|
176
|
+
},
|
|
177
|
+
titleCancel: {
|
|
178
|
+
fontSize: 17,
|
|
179
|
+
color: '#4A90E2'
|
|
180
|
+
},
|
|
181
|
+
titleConfirm: {
|
|
182
|
+
fontSize: 17,
|
|
183
|
+
color: '#4A90E2',
|
|
184
|
+
textAlign: 'right'
|
|
185
|
+
},
|
|
186
|
+
btnView: {
|
|
187
|
+
flexDirection: 'row',
|
|
188
|
+
justifyContent: 'space-between',
|
|
189
|
+
marginHorizontal: 15,
|
|
190
|
+
height: 60,
|
|
191
|
+
alignItems: 'center'
|
|
192
|
+
}
|
|
193
|
+
});
|
|
@@ -1 +1,303 @@
|
|
|
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 _core=require("@momo-kits/core");var _ScrollCustom=_interopRequireDefault(require("./custom/ScrollCustom"));var _jsxFileName="/Users/trinh.ho2/momo-folk/kits/src/libs/datePicker/dist/datePicker/WheelPicker.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 widthScreen=_reactNative.Dimensions.get('window').width;var WheelPicker=function(_Component){(0,_inherits2.default)(WheelPicker,_Component);var _super=_createSuper(WheelPicker);function WheelPicker(props){var _this;(0,_classCallCheck2.default)(this,WheelPicker);_this=_super.call(this,props);var _this$props=_this.props,heightItem=_this$props.heightItem,value=_this$props.value,numberItem=_this$props.numberItem,arrayValue=_this$props.arrayValue;_this.heightItem=heightItem;_this.currentPosition=value*_this.heightItem;_this.prePosition=_this.currentPosition;_this.numberItem=_this.convertNumberItem(numberItem);_this.isDragging=false;_this.state={width:widthScreen/3,value:value,items:arrayValue};return _this;}(0,_createClass2.default)(WheelPicker,[{key:"componentDidMount",value:function componentDidMount(){var value=this.props.value;this.scrollToPosition(value);}},{key:"componentWillUnmounted",value:function componentWillUnmounted(){clearTimeout(this.timer);clearTimeout(this.timerUpdate);}},{key:"scrollToPosition",value:function scrollToPosition(position){var _this2=this;this.currentPosition=position*this.heightItem;clearTimeout(this.timer);this.timer=setTimeout(function(){if(_this2.ScrollWheelPicker&&_this2.ScrollWheelPicker.scrollTo){_this2.ScrollWheelPicker.scrollTo({y:_this2.currentPosition});}},500);}},{key:"update",value:function update(param){var _this3=this;this.setState({items:param.arrayValue,value:param.value},function(){clearTimeout(_this3.timerUpdate);_this3.timerUpdate=setTimeout(function(){_this3.scrollToPosition(param.value);},100);});}},{key:"setScrollEnabled",value:function setScrollEnabled(enable){if(this.ScrollWheelPicker&&this.ScrollWheelPicker.setScrollEnabled&&!this.isDragging){this.ScrollWheelPicker.setScrollEnabled(enable);}}},{key:"onValueChange",value:function onValueChange(){var _this4=this;if(this.currentPosition!=null){var value=this.state.value;var _this$props2=this.props,onValueChange=_this$props2.onValueChange,onEndDrag=_this$props2.onEndDrag,refName=_this$props2.refName;var position=this.parseInteger(this.currentPosition/this.heightItem);if(position!==value){this.setState({value:position},function(){_this4.scrollToPosition(position);});}if(onValueChange){onValueChange(position);}if(onEndDrag){onEndDrag(refName);}}}},{key:"parseInteger",value:function parseInteger(number){var mode=number*10%10;if(mode>5){return parseInt(number,10)+1;}return parseInt(number,10);}},{key:"checkEvenNumber",value:function checkEvenNumber(number){var mode=number%2;if(mode>0){return false;}return true;}},{key:"convertNumberItem",value:function convertNumberItem(number){var numberItem=number;if(this.checkEvenNumber(numberItem)){numberItem+=1;}return numberItem;}},{key:"renderHiddenItem",value:function renderHiddenItem(numberItem,heightItem){var offset=arguments.length>2&&arguments[2]!==undefined?arguments[2]:0;var numberItemTop=parseInt(numberItem/2,10)-offset;var view=[];for(var i=0;i<numberItemTop;i++){view.push(_react.default.createElement(_reactNative.View,{key:"itemTop"+i,style:{flex:1,height:heightItem},__source:{fileName:_jsxFileName,lineNumber:121,columnNumber:23}}));}return view;}},{key:"onStartDrag",value:function onStartDrag(rawPosition){this.currentPosition=rawPosition;var _this$props3=this.props,onBeginDrag=_this$props3.onBeginDrag,refName=_this$props3.refName;this.isDragging=true;if(onBeginDrag){onBeginDrag(refName);}this.createCheckDragging();}},{key:"onEndDrag",value:function onEndDrag(){if(this.isDragging){this.isDragging=false;this.onValueChange();}if(this.checkEndDrag){clearInterval(this.checkEndDrag);}}},{key:"createCheckDragging",value:function createCheckDragging(){var _this5=this;this.checkEndDrag=setInterval(function(){_this5.checkPosition();},50);}},{key:"checkPosition",value:function checkPosition(){if(this.currentPosition===this.prePosition){this.onEndDrag();}else{this.prePosition=this.currentPosition;}}},{key:"render",value:function render(){var _this6=this;var numberHiddenItem=parseInt(this.numberItem/2,10);var positionCenter=parseInt(this.heightItem*numberHiddenItem,10);var heightContain=parseInt(this.heightItem*this.numberItem,10);var _this$state=this.state,items=_this$state.items,value=_this$state.value,width=_this$state.width;var _this$props4=this.props,preFix=_this$props4.preFix,bottomOffset=_this$props4.bottomOffset;return _react.default.createElement(_reactNative.View,{style:{flex:1,height:heightContain+20},__source:{fileName:_jsxFileName,lineNumber:174,columnNumber:13}},_react.default.createElement(_core.Text.SubTitle,{style:{color:_core.Colors.black_17,marginLeft:6,marginBottom:3},__source:{fileName:_jsxFileName,lineNumber:175,columnNumber:17}},preFix==null?void 0:preFix.toUpperCase()),_react.default.createElement(_reactNative.View,{style:{flex:1,height:heightContain,marginHorizontal:6,borderWidth:1,borderColor:_core.Colors.black_04,borderRadius:8,overflow:'hidden'},onLayout:function onLayout(event){if(event&&event.nativeEvent&&event.nativeEvent.layout&&event.nativeEvent.layout.height&&event.nativeEvent.layout.width){_this6.setState({width:event.nativeEvent.layout.width});}},__source:{fileName:_jsxFileName,lineNumber:181,columnNumber:17}},_react.default.createElement(_reactNative.View,{style:{position:'absolute',top:positionCenter,left:0,height:this.heightItem,width:width,backgroundColor:'#f2f8ff'},__source:{fileName:_jsxFileName,lineNumber:202,columnNumber:21}}),_react.default.createElement(_ScrollCustom.default,{ref:function ref(refName){_this6.ScrollWheelPicker=refName;},onScrollEndDrag:function onScrollEndDrag(event){if(event&&event.nativeEvent&&event.nativeEvent.contentOffset&&event.nativeEvent.contentOffset.y!=null&&!_this6.isDragging){_this6.onStartDrag(event.nativeEvent.contentOffset.y);}},onScroll:function onScroll(event){if(event&&event.nativeEvent&&event.nativeEvent.contentOffset&&event.nativeEvent.contentOffset.y!=null){_this6.currentPosition=event.nativeEvent.contentOffset.y;}},__source:{fileName:_jsxFileName,lineNumber:212,columnNumber:21}},this.renderHiddenItem(this.numberItem,this.heightItem),items.map(function(valueItem,index){return _react.default.createElement(_reactNative.View,{key:index,style:{flex:1,height:_this6.heightItem,justifyContent:'center',alignItems:'center'},__source:{fileName:_jsxFileName,lineNumber:234,columnNumber:29}},_react.default.createElement(_core.Text,{style:{color:value===index?_core.Colors.black_17:'#909090',fontWeight:value===index?'bold':'700',fontSize:value===index?(0,_core.ScaleSize)(20):(0,_core.ScaleSize)(16)},__source:{fileName:_jsxFileName,lineNumber:244,columnNumber:33}},valueItem));}),this.renderHiddenItem(this.numberItem,this.heightItem,bottomOffset)),_react.default.createElement(_core.LinearGradient,{pointerEvents:"none",locations:[0.2,0.85],colors:['rgba(255, 255, 255, 1)','rgba(255, 255, 255, 0)'],style:{height:this.heightItem*2,width:width,position:'absolute',top:0,overflow:'hidden'},__source:{fileName:_jsxFileName,lineNumber:259,columnNumber:21}}),_react.default.createElement(_core.LinearGradient,{pointerEvents:"none",locations:[0,0.85],colors:['rgba(255, 255, 255, 0)','rgba(255, 255, 255, 1)'],style:{height:this.heightItem*2,width:width,position:'absolute',bottom:0,overflow:'hidden'},__source:{fileName:_jsxFileName,lineNumber:272,columnNumber:21}})));}}]);return WheelPicker;}(_react.Component);exports.default=WheelPicker;WheelPicker.defaultProps={arrayValue:[],preFix:'',value:0,heightItem:42,numberItem:5,bottomOffset:0};module.exports=WheelPicker;
|
|
1
|
+
import React, { Component } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Dimensions, View
|
|
4
|
+
} from 'react-native';
|
|
5
|
+
import {
|
|
6
|
+
LinearGradient, Text, Colors, ScaleSize
|
|
7
|
+
} from '@momo-kits/core';
|
|
8
|
+
import ScrollCustom from './custom/ScrollCustom';
|
|
9
|
+
import DatePickerHelper from './helper/DatePickerHelper';
|
|
10
|
+
|
|
11
|
+
const widthScreen = Dimensions.get('window').width;
|
|
12
|
+
// var heightScreen = Dimensions.get('window').height;
|
|
13
|
+
|
|
14
|
+
export default class WheelPicker extends Component {
|
|
15
|
+
constructor(props) {
|
|
16
|
+
super(props);
|
|
17
|
+
const {
|
|
18
|
+
heightItem, value: valueItem, numberItem, arrayValue
|
|
19
|
+
} = this.props;
|
|
20
|
+
const value = valueItem === -1 ? 0 : valueItem;
|
|
21
|
+
this.heightItem = heightItem;
|
|
22
|
+
this.currentPosition = value * this.heightItem;
|
|
23
|
+
this.prePosition = this.currentPosition;
|
|
24
|
+
this.numberItem = this.convertNumberItem(numberItem);
|
|
25
|
+
this.isDragging = false;
|
|
26
|
+
this.state = {
|
|
27
|
+
// height: 100,
|
|
28
|
+
width: widthScreen / 3,
|
|
29
|
+
value,
|
|
30
|
+
items: arrayValue
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
componentDidMount() {
|
|
35
|
+
const { value } = this.props;
|
|
36
|
+
this.scrollToPosition(value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
componentWillUnmounted() {
|
|
40
|
+
clearTimeout(this.timer);
|
|
41
|
+
clearTimeout(this.timerUpdate);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
scrollToPosition(position) {
|
|
45
|
+
this.currentPosition = position * this.heightItem;
|
|
46
|
+
clearTimeout(this.timer);
|
|
47
|
+
this.timer = setTimeout(() => {
|
|
48
|
+
if (this.ScrollWheelPicker && this.ScrollWheelPicker.scrollTo) {
|
|
49
|
+
this.ScrollWheelPicker.scrollTo({ y: this.currentPosition });
|
|
50
|
+
}
|
|
51
|
+
}, 500);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
update(param) {
|
|
55
|
+
this.setState({
|
|
56
|
+
items: param.arrayValue,
|
|
57
|
+
value: param.value
|
|
58
|
+
}, () => {
|
|
59
|
+
clearTimeout(this.timerUpdate);
|
|
60
|
+
this.timerUpdate = setTimeout(() => {
|
|
61
|
+
this.scrollToPosition(param.value);
|
|
62
|
+
}, 100);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
setScrollEnabled(enable) {
|
|
67
|
+
if (this.ScrollWheelPicker && this.ScrollWheelPicker.setScrollEnabled && !this.isDragging) {
|
|
68
|
+
this.ScrollWheelPicker.setScrollEnabled(enable);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
onValueChange() {
|
|
73
|
+
if (this.currentPosition != null) {
|
|
74
|
+
const { value } = this.state;
|
|
75
|
+
const { onValueChange, onEndDrag, refName } = this.props;
|
|
76
|
+
const position = this.parseInteger(this.currentPosition / this.heightItem);
|
|
77
|
+
if (position !== value) {
|
|
78
|
+
this.setState({
|
|
79
|
+
value: position
|
|
80
|
+
}, () => {
|
|
81
|
+
this.scrollToPosition(position);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (onValueChange) {
|
|
85
|
+
onValueChange(position);
|
|
86
|
+
}
|
|
87
|
+
if (onEndDrag) {
|
|
88
|
+
onEndDrag(refName);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
parseInteger(number) {
|
|
94
|
+
const mode = (number * 10) % 10;
|
|
95
|
+
if (mode > 5) {
|
|
96
|
+
return parseInt(number, 10) + 1;
|
|
97
|
+
}
|
|
98
|
+
return parseInt(number, 10);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
checkEvenNumber(number) {
|
|
102
|
+
const mode = number % 2;
|
|
103
|
+
if (mode > 0) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
convertNumberItem(number) {
|
|
110
|
+
let numberItem = number;
|
|
111
|
+
if (this.checkEvenNumber(numberItem)) {
|
|
112
|
+
numberItem += 1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return numberItem;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
renderHiddenItem(numberItem, heightItem, offset = 0) {
|
|
119
|
+
const numberItemTop = parseInt(numberItem / 2, 10) - offset;
|
|
120
|
+
const view = [];
|
|
121
|
+
// eslint-disable-next-line no-plusplus
|
|
122
|
+
for (let i = 0; i < numberItemTop; i++) {
|
|
123
|
+
view.push(<View
|
|
124
|
+
key={`itemTop${i}`}
|
|
125
|
+
style={{
|
|
126
|
+
flex: 1,
|
|
127
|
+
height: heightItem,
|
|
128
|
+
}}
|
|
129
|
+
/>);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return view;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
onStartDrag(rawPosition) {
|
|
136
|
+
this.currentPosition = rawPosition;
|
|
137
|
+
const { onBeginDrag, refName } = this.props;
|
|
138
|
+
this.isDragging = true;
|
|
139
|
+
if (onBeginDrag) {
|
|
140
|
+
onBeginDrag(refName);
|
|
141
|
+
}
|
|
142
|
+
this.createCheckDragging();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
onEndDrag() {
|
|
146
|
+
if (this.isDragging) {
|
|
147
|
+
this.isDragging = false;
|
|
148
|
+
this.onValueChange();
|
|
149
|
+
}
|
|
150
|
+
if (this.checkEndDrag) {
|
|
151
|
+
clearInterval(this.checkEndDrag);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
createCheckDragging() {
|
|
156
|
+
this.checkEndDrag = setInterval(() => {
|
|
157
|
+
this.checkPosition();
|
|
158
|
+
}, 50);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
checkPosition() {
|
|
162
|
+
if (this.currentPosition === this.prePosition) {
|
|
163
|
+
this.onEndDrag();
|
|
164
|
+
} else {
|
|
165
|
+
this.prePosition = this.currentPosition;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
render() {
|
|
170
|
+
const numberHiddenItem = parseInt(this.numberItem / 2, 10);
|
|
171
|
+
const positionCenter = parseInt(this.heightItem * numberHiddenItem, 10);
|
|
172
|
+
const heightContain = parseInt(this.heightItem * this.numberItem, 10);
|
|
173
|
+
const { items, value, width } = this.state;
|
|
174
|
+
const { preFix, bottomOffset } = this.props;
|
|
175
|
+
return (
|
|
176
|
+
<View style={{ flex: 1, height: heightContain + 40 }}>
|
|
177
|
+
<Text.SubTitle weight='bold' style={{
|
|
178
|
+
color: Colors.black_17, marginLeft: 6, marginBottom: 3
|
|
179
|
+
}}
|
|
180
|
+
>
|
|
181
|
+
{DatePickerHelper.capitalizeFirstLetter(preFix || '')}
|
|
182
|
+
</Text.SubTitle>
|
|
183
|
+
<View
|
|
184
|
+
style={{
|
|
185
|
+
flex: 1,
|
|
186
|
+
height: heightContain,
|
|
187
|
+
//backgroundColor: 'blue',
|
|
188
|
+
marginHorizontal: 6,
|
|
189
|
+
paddingBottom: 20,
|
|
190
|
+
borderWidth: 1,
|
|
191
|
+
borderColor: Colors.black_04,
|
|
192
|
+
borderRadius: 8,
|
|
193
|
+
overflow: 'hidden'
|
|
194
|
+
}}
|
|
195
|
+
onLayout={(event) => {
|
|
196
|
+
if (event && event.nativeEvent && event.nativeEvent.layout
|
|
197
|
+
&& event.nativeEvent.layout.height && event.nativeEvent.layout.width) {
|
|
198
|
+
this.setState({
|
|
199
|
+
// height: event.nativeEvent.layout.height,
|
|
200
|
+
width: event.nativeEvent.layout.width
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}}
|
|
204
|
+
>
|
|
205
|
+
<View style={{
|
|
206
|
+
position: 'absolute',
|
|
207
|
+
top: positionCenter,
|
|
208
|
+
left: 0,
|
|
209
|
+
height: this.heightItem, // + 12,
|
|
210
|
+
width,
|
|
211
|
+
backgroundColor: '#f2f8ff',
|
|
212
|
+
}}
|
|
213
|
+
/>
|
|
214
|
+
|
|
215
|
+
<ScrollCustom
|
|
216
|
+
ref={(refName) => {
|
|
217
|
+
this.ScrollWheelPicker = refName;
|
|
218
|
+
}}
|
|
219
|
+
onScrollEndDrag={(event) => {
|
|
220
|
+
if (event && event.nativeEvent && event.nativeEvent.contentOffset
|
|
221
|
+
&& event.nativeEvent.contentOffset.y != null && !this.isDragging) {
|
|
222
|
+
this.onStartDrag(event.nativeEvent.contentOffset.y);
|
|
223
|
+
}
|
|
224
|
+
}}
|
|
225
|
+
onScroll={(event) => {
|
|
226
|
+
if (event && event.nativeEvent && event.nativeEvent.contentOffset
|
|
227
|
+
&& event.nativeEvent.contentOffset.y != null) {
|
|
228
|
+
this.currentPosition = event.nativeEvent.contentOffset.y;
|
|
229
|
+
}
|
|
230
|
+
}}
|
|
231
|
+
>
|
|
232
|
+
|
|
233
|
+
{
|
|
234
|
+
this.renderHiddenItem(this.numberItem, this.heightItem)
|
|
235
|
+
}
|
|
236
|
+
{items.map((valueItem, index) => (
|
|
237
|
+
<View
|
|
238
|
+
// eslint-disable-next-line react/no-array-index-key
|
|
239
|
+
key={index}
|
|
240
|
+
style={{
|
|
241
|
+
flex: 1,
|
|
242
|
+
height: this.heightItem,
|
|
243
|
+
justifyContent: 'center',
|
|
244
|
+
alignItems: 'center',
|
|
245
|
+
}}
|
|
246
|
+
>
|
|
247
|
+
<Text style={{
|
|
248
|
+
color: value === index ? Colors.black_17 : '#909090',
|
|
249
|
+
fontWeight: value === index ? 'bold' : '700',
|
|
250
|
+
fontSize: value === index ? ScaleSize(20) : ScaleSize(16),
|
|
251
|
+
}}
|
|
252
|
+
>
|
|
253
|
+
{valueItem}
|
|
254
|
+
{/* {reversePreFix ? `${valueItem} ${preFix}` : `${preFix} ${valueItem}`} */}
|
|
255
|
+
</Text>
|
|
256
|
+
</View>
|
|
257
|
+
))}
|
|
258
|
+
{
|
|
259
|
+
this.renderHiddenItem(this.numberItem, this.heightItem, bottomOffset)
|
|
260
|
+
}
|
|
261
|
+
</ScrollCustom>
|
|
262
|
+
<LinearGradient
|
|
263
|
+
pointerEvents="none"
|
|
264
|
+
locations={[0.2, 0.85]}
|
|
265
|
+
colors={['rgba(255, 255, 255, 1)', 'rgba(255, 255, 255, 0)']}
|
|
266
|
+
style={{
|
|
267
|
+
height: this.heightItem * 2,
|
|
268
|
+
width,
|
|
269
|
+
position: 'absolute',
|
|
270
|
+
top: 0,
|
|
271
|
+
overflow: 'hidden'
|
|
272
|
+
|
|
273
|
+
}}
|
|
274
|
+
/>
|
|
275
|
+
<LinearGradient
|
|
276
|
+
pointerEvents="none"
|
|
277
|
+
locations={[0, 0.85]}
|
|
278
|
+
colors={['rgba(255, 255, 255, 0)', 'rgba(255, 255, 255, 1)']}
|
|
279
|
+
style={{
|
|
280
|
+
height: this.heightItem * 2,
|
|
281
|
+
width,
|
|
282
|
+
position: 'absolute',
|
|
283
|
+
bottom: 0,
|
|
284
|
+
overflow: 'hidden'
|
|
285
|
+
}}
|
|
286
|
+
/>
|
|
287
|
+
</View>
|
|
288
|
+
</View>
|
|
289
|
+
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
WheelPicker.defaultProps = {
|
|
295
|
+
arrayValue: [],
|
|
296
|
+
preFix: '',
|
|
297
|
+
value: 0,
|
|
298
|
+
heightItem: 42,
|
|
299
|
+
numberItem: 5,
|
|
300
|
+
bottomOffset: 0
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
module.exports = WheelPicker;
|