@momo-kits/date-picker 0.79.6 → 0.80.2
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/WheelPicker.tsx +32 -7
- package/index.tsx +17 -6
- package/package.json +1 -1
- package/types.ts +56 -0
package/WheelPicker.tsx
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import React, {FC, useCallback, useContext, useEffect, useRef} from 'react';
|
|
2
2
|
import {Animated, FlatList, View} from 'react-native';
|
|
3
3
|
import styles from './styles';
|
|
4
|
-
import {ApplicationContext,
|
|
4
|
+
import {ApplicationContext, scaleSize} from '@momo-kits/foundation';
|
|
5
5
|
import {WheelPickerProps} from './types';
|
|
6
6
|
import {debounce} from './utils';
|
|
7
7
|
|
|
8
8
|
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
|
|
9
|
-
|
|
10
9
|
const WheelPicker: FC<WheelPickerProps> = ({
|
|
11
10
|
name,
|
|
12
11
|
data,
|
|
@@ -37,7 +36,7 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
37
36
|
return () => {
|
|
38
37
|
debouncedScrollEnd.cancel();
|
|
39
38
|
};
|
|
40
|
-
}, [
|
|
39
|
+
}, []);
|
|
41
40
|
|
|
42
41
|
const debouncedScrollEnd = debounce(() => {
|
|
43
42
|
let selectedIndex = data.findIndex(item => item === selectedData);
|
|
@@ -57,7 +56,8 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
57
56
|
}, 100);
|
|
58
57
|
|
|
59
58
|
const ItemComponent: FC<any> = React.memo(props => {
|
|
60
|
-
const {item, opacity} = props;
|
|
59
|
+
const {item, opacity, scale} = props;
|
|
60
|
+
|
|
61
61
|
return (
|
|
62
62
|
<Animated.View
|
|
63
63
|
style={[
|
|
@@ -66,7 +66,15 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
66
66
|
opacity,
|
|
67
67
|
},
|
|
68
68
|
]}>
|
|
69
|
-
<Text
|
|
69
|
+
<Animated.Text
|
|
70
|
+
style={{
|
|
71
|
+
fontFamily: `${theme.font}-Semibold`,
|
|
72
|
+
transform: [{scale}],
|
|
73
|
+
fontSize: scaleSize(15),
|
|
74
|
+
lineHeight: scaleSize(22),
|
|
75
|
+
}}>
|
|
76
|
+
{item}
|
|
77
|
+
</Animated.Text>
|
|
70
78
|
</Animated.View>
|
|
71
79
|
);
|
|
72
80
|
});
|
|
@@ -90,10 +98,27 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
90
98
|
};
|
|
91
99
|
};
|
|
92
100
|
|
|
101
|
+
const scaleAnimated = (a: number, b: number) => {
|
|
102
|
+
return {
|
|
103
|
+
inputRange: [...data.map((_: any, i: number) => i * itemSize)],
|
|
104
|
+
outputRange: [
|
|
105
|
+
...data.map((_: any, i: number) => {
|
|
106
|
+
const center = i + 2;
|
|
107
|
+
if (center === index) {
|
|
108
|
+
return a;
|
|
109
|
+
} else {
|
|
110
|
+
return b;
|
|
111
|
+
}
|
|
112
|
+
}),
|
|
113
|
+
],
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
93
117
|
return (
|
|
94
118
|
<ItemComponent
|
|
95
119
|
item={item}
|
|
96
120
|
opacity={scrollAnimatedValue.interpolate(opacityAnimated(1, 0.8, 0.4))}
|
|
121
|
+
scale={scrollAnimatedValue.interpolate(scaleAnimated(1, 0.87))}
|
|
97
122
|
/>
|
|
98
123
|
);
|
|
99
124
|
}, []);
|
|
@@ -132,8 +157,8 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
132
157
|
]}>
|
|
133
158
|
<AnimatedFlatList
|
|
134
159
|
windowSize={15}
|
|
135
|
-
initialNumToRender={
|
|
136
|
-
maxToRenderPerBatch={
|
|
160
|
+
initialNumToRender={15}
|
|
161
|
+
maxToRenderPerBatch={15}
|
|
137
162
|
pagingEnabled
|
|
138
163
|
snapToInterval={itemSize}
|
|
139
164
|
decelerationRate={'fast'}
|
package/index.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, {FC, useEffect, useState} from 'react';
|
|
2
|
-
import {Dimensions, View} from 'react-native';
|
|
2
|
+
import {Dimensions, LayoutChangeEvent, View} from 'react-native';
|
|
3
3
|
import {Spacing} from '@momo-kits/foundation';
|
|
4
4
|
import WheelPicker from './WheelPicker';
|
|
5
5
|
import {
|
|
@@ -13,17 +13,21 @@ import {
|
|
|
13
13
|
import {DateObject, DateTimePickerProps, PickerDataObject} from './types';
|
|
14
14
|
import styles from './styles';
|
|
15
15
|
|
|
16
|
-
const screenWidth = Dimensions.get('window').width;
|
|
17
16
|
const date = new Date();
|
|
17
|
+
const minDateDefault = new Date(date);
|
|
18
|
+
const maxDateDefault = new Date(date);
|
|
19
|
+
minDateDefault.setFullYear(minDateDefault.getFullYear() - 10);
|
|
20
|
+
maxDateDefault.setFullYear(maxDateDefault.getFullYear() + 10);
|
|
18
21
|
date.setHours(0, 0);
|
|
22
|
+
const screenWidth = Dimensions.get('window').width;
|
|
19
23
|
|
|
20
24
|
const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
21
25
|
format = 'DD-MM-YYYY',
|
|
22
26
|
minuteInterval = 1,
|
|
23
27
|
onChange,
|
|
24
28
|
selectedValue = date,
|
|
25
|
-
minDate =
|
|
26
|
-
maxDate =
|
|
29
|
+
minDate = minDateDefault,
|
|
30
|
+
maxDate = maxDateDefault,
|
|
27
31
|
}) => {
|
|
28
32
|
const [data, setData] = useState<DateObject[]>([]);
|
|
29
33
|
let [currentDate, setCurrentDate] = useState<PickerDataObject>({
|
|
@@ -33,6 +37,7 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
33
37
|
hour: selectedValue.getHours(),
|
|
34
38
|
min: selectedValue.getMinutes(),
|
|
35
39
|
});
|
|
40
|
+
const [containerWidth, setContainerWidth] = useState(screenWidth);
|
|
36
41
|
|
|
37
42
|
useEffect(() => {
|
|
38
43
|
setupData();
|
|
@@ -112,8 +117,12 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
112
117
|
|
|
113
118
|
const paddingArray = ['', ''];
|
|
114
119
|
|
|
120
|
+
const onLayout = (e: LayoutChangeEvent) => {
|
|
121
|
+
setContainerWidth(e.nativeEvent.layout.width);
|
|
122
|
+
};
|
|
123
|
+
|
|
115
124
|
return (
|
|
116
|
-
<View style={styles.datePicker}>
|
|
125
|
+
<View onLayout={onLayout} style={styles.datePicker}>
|
|
117
126
|
{data.map((dataItem, index) => {
|
|
118
127
|
return (
|
|
119
128
|
<WheelPicker
|
|
@@ -124,7 +133,9 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
124
133
|
name={dataItem.name}
|
|
125
134
|
style={{
|
|
126
135
|
width:
|
|
127
|
-
(
|
|
136
|
+
(containerWidth -
|
|
137
|
+
Spacing.M * 2 -
|
|
138
|
+
(numOfColumns - 1) * Spacing.M) /
|
|
128
139
|
numOfColumns,
|
|
129
140
|
}}
|
|
130
141
|
/>
|
package/package.json
CHANGED
package/types.ts
CHANGED
|
@@ -1,32 +1,88 @@
|
|
|
1
1
|
import {ViewStyle} from 'react-native';
|
|
2
2
|
|
|
3
3
|
export type DateTimePickerProps = {
|
|
4
|
+
/**
|
|
5
|
+
* Optional. Specifies the format for displaying the date and time.
|
|
6
|
+
*/
|
|
4
7
|
format?: string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Optional. The interval between minutes in the picker. For example, '15' would show minute options 0, 15, 30, 45, etc.
|
|
11
|
+
*/
|
|
5
12
|
minuteInterval?: number;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Optional. A callback function that is triggered when the date or time value changes.
|
|
16
|
+
*/
|
|
6
17
|
onChange?: (data: Date) => void;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Optional. The initially selected date and time value.
|
|
21
|
+
*/
|
|
7
22
|
selectedValue?: Date;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Optional. The minimum date that can be selected.
|
|
26
|
+
*/
|
|
8
27
|
minDate?: Date;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Optional. The maximum date that can be selected.
|
|
31
|
+
*/
|
|
9
32
|
maxDate?: Date;
|
|
10
33
|
};
|
|
11
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Represents the data structure for individual date and time components.
|
|
37
|
+
*/
|
|
12
38
|
export type PickerDataObject = {
|
|
13
39
|
day: number;
|
|
14
40
|
month: number;
|
|
15
41
|
year: number;
|
|
16
42
|
hour: number;
|
|
17
43
|
min: number;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Optional. Specifies the mode of time presentation, e.g., 'AM' or 'PM'.
|
|
47
|
+
*/
|
|
18
48
|
timeMode?: string;
|
|
19
49
|
};
|
|
20
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Type representing a date object with a name identifying the component ('day', 'month', etc.) and the associated data.
|
|
53
|
+
*/
|
|
21
54
|
export type DateObject = {
|
|
22
55
|
name: 'day' | 'month' | 'year' | 'hour' | 'min' | 'timeMode';
|
|
23
56
|
data: string[];
|
|
24
57
|
};
|
|
25
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Properties for the WheelPicker component.
|
|
61
|
+
* This component provides a spinning-wheel style picker for selecting from provided data.
|
|
62
|
+
*/
|
|
26
63
|
export type WheelPickerProps = {
|
|
64
|
+
/**
|
|
65
|
+
* The name of the picker, used to identify it.
|
|
66
|
+
*/
|
|
27
67
|
name: string;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The data to be displayed and selected in the picker.
|
|
71
|
+
*/
|
|
28
72
|
data: string[];
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Custom styles to apply to the WheelPicker component.
|
|
76
|
+
*/
|
|
29
77
|
style: ViewStyle;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A callback function that is triggered when the selected data changes.
|
|
81
|
+
*/
|
|
30
82
|
onChange: (name: string, data: string) => void;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The currently selected data in the picker.
|
|
86
|
+
*/
|
|
31
87
|
selectedData: string;
|
|
32
88
|
};
|