@momo-kits/date-picker 0.0.74-beta → 0.72.1

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,300 +1,328 @@
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';
1
+ import React, {Component} from 'react';
2
+ import {Dimensions, View} from 'react-native';
3
+ import {LinearGradient, Text, Colors, ScaleSize} from '@momo-kits/core-v2';
8
4
  import ScrollCustom from './custom/ScrollCustom';
5
+ import DatePickerHelper from './helper/DatePickerHelper';
9
6
 
10
7
  const widthScreen = Dimensions.get('window').width;
11
8
  // var heightScreen = Dimensions.get('window').height;
12
9
 
13
10
  export default class WheelPicker extends Component {
14
- constructor(props) {
15
- super(props);
16
- const {
17
- heightItem, value, numberItem, arrayValue
18
- } = this.props;
19
- this.heightItem = heightItem;
20
- this.currentPosition = value * this.heightItem;
21
- this.prePosition = this.currentPosition;
22
- this.numberItem = this.convertNumberItem(numberItem);
23
- this.isDragging = false;
24
- this.state = {
25
- // height: 100,
26
- width: widthScreen / 3,
27
- value,
28
- items: arrayValue
29
- };
30
- }
11
+ constructor(props) {
12
+ super(props);
13
+ const {heightItem, value: valueItem, numberItem, arrayValue} = this.props;
14
+ const value = valueItem === -1 ? 0 : valueItem;
15
+ this.heightItem = heightItem;
16
+ this.currentPosition = value * this.heightItem;
17
+ this.prePosition = this.currentPosition;
18
+ this.numberItem = this.convertNumberItem(numberItem);
19
+ this.isDragging = false;
20
+ this.state = {
21
+ // height: 100,
22
+ width: widthScreen / 3,
23
+ value,
24
+ items: arrayValue,
25
+ };
26
+ }
31
27
 
32
- componentDidMount() {
33
- const { value } = this.props;
34
- this.scrollToPosition(value);
35
- }
28
+ componentDidMount() {
29
+ const {value} = this.props;
30
+ console.log(value);
31
+ this.scrollToPosition(value);
32
+ }
36
33
 
37
- componentWillUnmounted() {
38
- clearTimeout(this.timer);
39
- clearTimeout(this.timerUpdate);
40
- }
34
+ componentWillUnmounted() {
35
+ clearTimeout(this.timer);
36
+ clearTimeout(this.timerUpdate);
37
+ }
41
38
 
42
- scrollToPosition(position) {
43
- this.currentPosition = position * this.heightItem;
44
- clearTimeout(this.timer);
45
- this.timer = setTimeout(() => {
46
- if (this.ScrollWheelPicker && this.ScrollWheelPicker.scrollTo) {
47
- this.ScrollWheelPicker.scrollTo({ y: this.currentPosition });
48
- }
49
- }, 500);
50
- }
39
+ scrollToPosition(position) {
40
+ this.currentPosition = position * this.heightItem;
41
+ clearTimeout(this.timer);
42
+ this.timer = setTimeout(() => {
43
+ if (this.ScrollWheelPicker && this.ScrollWheelPicker.scrollTo) {
44
+ this.ScrollWheelPicker.scrollTo({y: this.currentPosition});
45
+ }
46
+ }, 500);
47
+ }
51
48
 
52
- update(param) {
53
- this.setState({
54
- items: param.arrayValue,
55
- value: param.value
56
- }, () => {
57
- clearTimeout(this.timerUpdate);
58
- this.timerUpdate = setTimeout(() => {
59
- this.scrollToPosition(param.value);
60
- }, 100);
61
- });
62
- }
49
+ update(param) {
50
+ this.setState(
51
+ {
52
+ items: param.arrayValue,
53
+ value: param.value,
54
+ },
55
+ () => {
56
+ clearTimeout(this.timerUpdate);
57
+ this.timerUpdate = setTimeout(() => {
58
+ this.scrollToPosition(param.value);
59
+ }, 100);
60
+ },
61
+ );
62
+ }
63
63
 
64
- setScrollEnabled(enable) {
65
- if (this.ScrollWheelPicker && this.ScrollWheelPicker.setScrollEnabled && !this.isDragging) {
66
- this.ScrollWheelPicker.setScrollEnabled(enable);
67
- }
64
+ setScrollEnabled(enable) {
65
+ if (
66
+ this.ScrollWheelPicker &&
67
+ this.ScrollWheelPicker.setScrollEnabled &&
68
+ !this.isDragging
69
+ ) {
70
+ this.ScrollWheelPicker.setScrollEnabled(enable);
68
71
  }
72
+ }
69
73
 
70
- onValueChange() {
71
- if (this.currentPosition != null) {
72
- const { value } = this.state;
73
- const { onValueChange, onEndDrag, refName } = this.props;
74
- const position = this.parseInteger(this.currentPosition / this.heightItem);
75
- if (position !== value) {
76
- this.setState({
77
- value: position
78
- }, () => {
79
- this.scrollToPosition(position);
80
- });
81
- }
82
- if (onValueChange) {
83
- onValueChange(position);
84
- }
85
- if (onEndDrag) {
86
- onEndDrag(refName);
87
- }
88
- }
74
+ onValueChange() {
75
+ if (this.currentPosition != null) {
76
+ const {value} = this.state;
77
+ const {onValueChange, onEndDrag, refName} = this.props;
78
+ const position = this.parseInteger(
79
+ this.currentPosition / this.heightItem,
80
+ );
81
+ if (position !== value) {
82
+ this.setState(
83
+ {
84
+ value: position,
85
+ },
86
+ () => {
87
+ this.scrollToPosition(position);
88
+ },
89
+ );
90
+ }
91
+ if (onValueChange) {
92
+ onValueChange(position);
93
+ }
94
+ if (onEndDrag) {
95
+ onEndDrag(refName);
96
+ }
89
97
  }
98
+ }
90
99
 
91
- parseInteger(number) {
92
- const mode = (number * 10) % 10;
93
- if (mode > 5) {
94
- return parseInt(number, 10) + 1;
95
- }
96
- return parseInt(number, 10);
100
+ parseInteger(number) {
101
+ const mode = (number * 10) % 10;
102
+ if (mode > 5) {
103
+ return parseInt(number, 10) + 1;
97
104
  }
105
+ return parseInt(number, 10);
106
+ }
98
107
 
99
- checkEvenNumber(number) {
100
- const mode = number % 2;
101
- if (mode > 0) {
102
- return false;
103
- }
104
- return true;
108
+ checkEvenNumber(number) {
109
+ const mode = number % 2;
110
+ if (mode > 0) {
111
+ return false;
105
112
  }
113
+ return true;
114
+ }
106
115
 
107
- convertNumberItem(number) {
108
- let numberItem = number;
109
- if (this.checkEvenNumber(numberItem)) {
110
- numberItem += 1;
111
- }
112
-
113
- return numberItem;
116
+ convertNumberItem(number) {
117
+ let numberItem = number;
118
+ if (this.checkEvenNumber(numberItem)) {
119
+ numberItem += 1;
114
120
  }
115
121
 
116
- renderHiddenItem(numberItem, heightItem, offset = 0) {
117
- const numberItemTop = parseInt(numberItem / 2, 10) - offset;
118
- const view = [];
119
- // eslint-disable-next-line no-plusplus
120
- for (let i = 0; i < numberItemTop; i++) {
121
- view.push(<View
122
- key={`itemTop${i}`}
123
- style={{
124
- flex: 1,
125
- height: heightItem,
126
- }}
127
- />);
128
- }
122
+ return numberItem;
123
+ }
129
124
 
130
- return view;
125
+ renderHiddenItem(numberItem, heightItem, offset = 0) {
126
+ const numberItemTop = parseInt(numberItem / 2, 10) - offset;
127
+ const view = [];
128
+ // eslint-disable-next-line no-plusplus
129
+ for (let i = 0; i < numberItemTop; i++) {
130
+ view.push(
131
+ <View
132
+ key={`itemTop${i}`}
133
+ style={{
134
+ flex: 1,
135
+ height: heightItem,
136
+ }}
137
+ />,
138
+ );
131
139
  }
132
140
 
133
- onStartDrag(rawPosition) {
134
- this.currentPosition = rawPosition;
135
- const { onBeginDrag, refName } = this.props;
136
- this.isDragging = true;
137
- if (onBeginDrag) {
138
- onBeginDrag(refName);
139
- }
140
- this.createCheckDragging();
141
- }
141
+ return view;
142
+ }
142
143
 
143
- onEndDrag() {
144
- if (this.isDragging) {
145
- this.isDragging = false;
146
- this.onValueChange();
147
- }
148
- if (this.checkEndDrag) {
149
- clearInterval(this.checkEndDrag);
150
- }
144
+ onStartDrag(rawPosition) {
145
+ this.currentPosition = rawPosition;
146
+ const {onBeginDrag, refName} = this.props;
147
+ this.isDragging = true;
148
+ if (onBeginDrag) {
149
+ onBeginDrag(refName);
151
150
  }
151
+ this.createCheckDragging();
152
+ }
152
153
 
153
- createCheckDragging() {
154
- this.checkEndDrag = setInterval(() => {
155
- this.checkPosition();
156
- }, 50);
154
+ onEndDrag() {
155
+ if (this.isDragging) {
156
+ this.isDragging = false;
157
+ this.onValueChange();
157
158
  }
158
-
159
- checkPosition() {
160
- if (this.currentPosition === this.prePosition) {
161
- this.onEndDrag();
162
- } else {
163
- this.prePosition = this.currentPosition;
164
- }
159
+ if (this.checkEndDrag) {
160
+ clearInterval(this.checkEndDrag);
165
161
  }
162
+ }
166
163
 
167
- render() {
168
- const numberHiddenItem = parseInt(this.numberItem / 2, 10);
169
- const positionCenter = parseInt(this.heightItem * numberHiddenItem, 10);
170
- const heightContain = parseInt(this.heightItem * this.numberItem, 10);
171
- const { items, value, width } = this.state;
172
- const { preFix, bottomOffset } = this.props;
173
- return (
174
- <View style={{ flex: 1, height: heightContain + 20 }}>
175
- <Text.SubTitle style={{
176
- color: Colors.black_17, marginLeft: 6, marginBottom: 3
177
- }}
178
- >
179
- {preFix?.toUpperCase()}
180
- </Text.SubTitle>
181
- <View
182
- style={{
183
- flex: 1,
184
- height: heightContain,
185
- // backgroundColor: 'blue',
186
- marginHorizontal: 6,
187
- borderWidth: 1,
188
- borderColor: Colors.black_04,
189
- borderRadius: 8,
190
- overflow: 'hidden'
191
- }}
192
- onLayout={(event) => {
193
- if (event && event.nativeEvent && event.nativeEvent.layout
194
- && event.nativeEvent.layout.height && event.nativeEvent.layout.width) {
195
- this.setState({
196
- // height: event.nativeEvent.layout.height,
197
- width: event.nativeEvent.layout.width
198
- });
199
- }
200
- }}
201
- >
202
- <View style={{
203
- position: 'absolute',
204
- top: positionCenter,
205
- left: 0,
206
- height: this.heightItem, // + 12,
207
- width,
208
- backgroundColor: '#f2f8ff',
209
- }}
210
- />
164
+ createCheckDragging() {
165
+ this.checkEndDrag = setInterval(() => {
166
+ this.checkPosition();
167
+ }, 50);
168
+ }
211
169
 
212
- <ScrollCustom
213
- ref={(refName) => {
214
- this.ScrollWheelPicker = refName;
215
- }}
216
- onScrollEndDrag={(event) => {
217
- if (event && event.nativeEvent && event.nativeEvent.contentOffset
218
- && event.nativeEvent.contentOffset.y != null && !this.isDragging) {
219
- this.onStartDrag(event.nativeEvent.contentOffset.y);
220
- }
221
- }}
222
- onScroll={(event) => {
223
- if (event && event.nativeEvent && event.nativeEvent.contentOffset
224
- && event.nativeEvent.contentOffset.y != null) {
225
- this.currentPosition = event.nativeEvent.contentOffset.y;
226
- }
227
- }}
228
- >
170
+ checkPosition() {
171
+ if (this.currentPosition === this.prePosition) {
172
+ this.onEndDrag();
173
+ } else {
174
+ this.prePosition = this.currentPosition;
175
+ }
176
+ }
229
177
 
230
- {
231
- this.renderHiddenItem(this.numberItem, this.heightItem)
232
- }
233
- {items.map((valueItem, index) => (
234
- <View
235
- // eslint-disable-next-line react/no-array-index-key
236
- key={index}
237
- style={{
238
- flex: 1,
239
- height: this.heightItem,
240
- justifyContent: 'center',
241
- alignItems: 'center',
242
- }}
243
- >
244
- <Text style={{
245
- color: value === index ? Colors.black_17 : '#909090',
246
- fontWeight: value === index ? 'bold' : '700',
247
- fontSize: value === index ? ScaleSize(20) : ScaleSize(16),
248
- }}
249
- >
250
- {valueItem}
251
- {/* {reversePreFix ? `${valueItem} ${preFix}` : `${preFix} ${valueItem}`} */}
252
- </Text>
253
- </View>
254
- ))}
255
- {
256
- this.renderHiddenItem(this.numberItem, this.heightItem, bottomOffset)
257
- }
258
- </ScrollCustom>
259
- <LinearGradient
260
- pointerEvents="none"
261
- locations={[0.2, 0.85]}
262
- colors={['rgba(255, 255, 255, 1)', 'rgba(255, 255, 255, 0)']}
263
- style={{
264
- height: this.heightItem * 2,
265
- width,
266
- position: 'absolute',
267
- top: 0,
268
- overflow: 'hidden'
178
+ render() {
179
+ const numberHiddenItem = parseInt(this.numberItem / 2, 10);
180
+ const positionCenter = parseInt(this.heightItem * numberHiddenItem, 10);
181
+ const heightContain = parseInt(this.heightItem * this.numberItem, 10);
182
+ const {items, value, width} = this.state;
183
+ const {preFix, bottomOffset} = this.props;
184
+ return (
185
+ <View
186
+ style={{
187
+ flex: 1,
188
+ height: heightContain + 40,
189
+ }}>
190
+ <Text.Description2
191
+ weight="semibold"
192
+ style={{
193
+ color: Colors.black_17,
194
+ marginLeft: 6,
195
+ marginBottom: 3,
196
+ }}>
197
+ {DatePickerHelper.capitalizeFirstLetter(preFix || '')}
198
+ </Text.Description2>
199
+ <View
200
+ style={{
201
+ flex: 1,
202
+ height: heightContain,
203
+ marginHorizontal: 6,
204
+ paddingBottom: 20,
205
+ borderWidth: 1,
206
+ borderColor: Colors.black_04,
207
+ borderRadius: 8,
208
+ overflow: 'hidden',
209
+ }}
210
+ onLayout={event => {
211
+ if (
212
+ event &&
213
+ event.nativeEvent &&
214
+ event.nativeEvent.layout &&
215
+ event.nativeEvent.layout.height &&
216
+ event.nativeEvent.layout.width
217
+ ) {
218
+ this.setState({
219
+ // height: event.nativeEvent.layout.height,
220
+ width: event.nativeEvent.layout.width,
221
+ });
222
+ }
223
+ }}>
224
+ <View
225
+ style={{
226
+ position: 'absolute',
227
+ top: positionCenter,
228
+ left: 0,
229
+ height: this.heightItem, // + 12,
230
+ width,
231
+ backgroundColor: '#f2f8ff',
232
+ }}
233
+ />
269
234
 
270
- }}
271
- />
272
- <LinearGradient
273
- pointerEvents="none"
274
- locations={[0, 0.85]}
275
- colors={['rgba(255, 255, 255, 0)', 'rgba(255, 255, 255, 1)']}
276
- style={{
277
- height: this.heightItem * 2,
278
- width,
279
- position: 'absolute',
280
- bottom: 0,
281
- overflow: 'hidden'
282
- }}
283
- />
284
- </View>
285
- </View>
235
+ <ScrollCustom
236
+ ref={refName => {
237
+ this.ScrollWheelPicker = refName;
238
+ }}
239
+ onScrollEndDrag={event => {
240
+ if (
241
+ event &&
242
+ event.nativeEvent &&
243
+ event.nativeEvent.contentOffset &&
244
+ event.nativeEvent.contentOffset.y != null &&
245
+ !this.isDragging
246
+ ) {
247
+ this.onStartDrag(event.nativeEvent.contentOffset.y);
248
+ }
249
+ }}
250
+ onScroll={event => {
251
+ if (
252
+ event &&
253
+ event.nativeEvent &&
254
+ event.nativeEvent.contentOffset &&
255
+ event.nativeEvent.contentOffset.y != null
256
+ ) {
257
+ this.currentPosition = event.nativeEvent.contentOffset.y;
258
+ }
259
+ }}>
260
+ {this.renderHiddenItem(this.numberItem, this.heightItem)}
261
+ {items.map((valueItem, index) => (
262
+ <View
263
+ // eslint-disable-next-line react/no-array-index-key
264
+ key={index}
265
+ style={{
266
+ flex: 1,
267
+ height: this.heightItem,
268
+ justifyContent: 'center',
269
+ alignItems: 'center',
270
+ }}>
271
+ <Text.Action2
272
+ weight={value === index ? 'bold' : 'semibold'}
273
+ style={{
274
+ color: value === index ? Colors.black_17 : Colors.black_12,
286
275
 
287
- );
288
- }
276
+ fontSize: 15,
277
+ }}>
278
+ {valueItem}
279
+ {/* {reversePreFix ? `${valueItem} ${preFix}` : `${preFix} ${valueItem}`} */}
280
+ </Text.Action2>
281
+ </View>
282
+ ))}
283
+ {this.renderHiddenItem(
284
+ this.numberItem,
285
+ this.heightItem,
286
+ bottomOffset,
287
+ )}
288
+ </ScrollCustom>
289
+ <LinearGradient
290
+ pointerEvents="none"
291
+ locations={[0.2, 0.85]}
292
+ colors={['rgba(255, 255, 255, 1)', 'rgba(255, 255, 255, 0)']}
293
+ style={{
294
+ height: this.heightItem * 2,
295
+ width,
296
+ position: 'absolute',
297
+ top: 0,
298
+ overflow: 'hidden',
299
+ }}
300
+ />
301
+ <LinearGradient
302
+ pointerEvents="none"
303
+ locations={[0, 0.85]}
304
+ colors={['rgba(255, 255, 255, 0)', 'rgba(255, 255, 255, 1)']}
305
+ style={{
306
+ height: this.heightItem * 2,
307
+ width,
308
+ position: 'absolute',
309
+ bottom: 0,
310
+ overflow: 'hidden',
311
+ }}
312
+ />
313
+ </View>
314
+ </View>
315
+ );
316
+ }
289
317
  }
290
318
 
291
319
  WheelPicker.defaultProps = {
292
- arrayValue: [],
293
- preFix: '',
294
- value: 0,
295
- heightItem: 42,
296
- numberItem: 5,
297
- bottomOffset: 0
320
+ arrayValue: [],
321
+ preFix: '',
322
+ value: 0,
323
+ heightItem: 42,
324
+ numberItem: 5,
325
+ bottomOffset: 0,
298
326
  };
299
327
 
300
328
  module.exports = WheelPicker;