@momo-kits/date-picker 0.169.4 → 0.169.6
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 +21 -16
- package/WheelPickerItem.tsx +2 -5
- package/index.tsx +23 -20
- package/package.json +1 -1
- package/types.ts +5 -4
- package/utils.ts +6 -0
package/WheelPicker.tsx
CHANGED
|
@@ -24,6 +24,10 @@ import { debounce, itemSize } from './utils';
|
|
|
24
24
|
import WheelPickerItem from './WheelPickerItem';
|
|
25
25
|
|
|
26
26
|
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
|
|
27
|
+
/**
|
|
28
|
+
* Snapping 5-row wheel; reports the centered item to `onChange` on scroll end.
|
|
29
|
+
* `data` carries two empty padding items at each end so any real row can center.
|
|
30
|
+
*/
|
|
27
31
|
const WheelPicker: FC<WheelPickerProps> = ({
|
|
28
32
|
name,
|
|
29
33
|
data,
|
|
@@ -38,7 +42,7 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
38
42
|
const initItemNum = 15;
|
|
39
43
|
|
|
40
44
|
const onScroll = useAnimatedScrollHandler({
|
|
41
|
-
onScroll:
|
|
45
|
+
onScroll: event => {
|
|
42
46
|
scrollAnimatedValue.value = event.contentOffset.y;
|
|
43
47
|
},
|
|
44
48
|
});
|
|
@@ -86,16 +90,19 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
86
90
|
});
|
|
87
91
|
}, 100);
|
|
88
92
|
|
|
89
|
-
const renderItem = useCallback(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
93
|
+
const renderItem = useCallback(
|
|
94
|
+
({ item, index }: ListRenderItemInfo<any>) => {
|
|
95
|
+
return (
|
|
96
|
+
<WheelPickerItem
|
|
97
|
+
item={item}
|
|
98
|
+
index={index}
|
|
99
|
+
scrollAnimatedValue={scrollAnimatedValue}
|
|
100
|
+
itemSize={itemSize}
|
|
101
|
+
/>
|
|
102
|
+
);
|
|
103
|
+
},
|
|
104
|
+
[scrollAnimatedValue],
|
|
105
|
+
);
|
|
99
106
|
|
|
100
107
|
const renderOverlay = () => {
|
|
101
108
|
return (
|
|
@@ -108,11 +115,9 @@ const WheelPicker: FC<WheelPickerProps> = ({
|
|
|
108
115
|
const getItemLayout = (
|
|
109
116
|
_data: ArrayLike<unknown> | null | undefined,
|
|
110
117
|
index: number,
|
|
111
|
-
) =>
|
|
112
|
-
length: itemSize,
|
|
113
|
-
|
|
114
|
-
index,
|
|
115
|
-
});
|
|
118
|
+
) => {
|
|
119
|
+
return { length: itemSize, offset: itemSize * index, index };
|
|
120
|
+
};
|
|
116
121
|
|
|
117
122
|
const onMomentumScrollBegin = () => {
|
|
118
123
|
canMomentum.current = true;
|
package/WheelPickerItem.tsx
CHANGED
|
@@ -8,6 +8,7 @@ import styles from './styles';
|
|
|
8
8
|
import { exportFontFamily, useScaleSize } from '@momo-kits/foundation';
|
|
9
9
|
import { WheelPickerItemProps } from './types';
|
|
10
10
|
|
|
11
|
+
/** Wheel row that fades and shrinks with its distance from the centered row. */
|
|
11
12
|
const WheelPickerItem: FC<WheelPickerItemProps> = ({
|
|
12
13
|
item,
|
|
13
14
|
index,
|
|
@@ -42,11 +43,7 @@ const WheelPickerItem: FC<WheelPickerItemProps> = ({
|
|
|
42
43
|
|
|
43
44
|
return (
|
|
44
45
|
<Animated.View
|
|
45
|
-
style={[
|
|
46
|
-
styles.wheelItem,
|
|
47
|
-
{ height: itemSize },
|
|
48
|
-
containerStyle,
|
|
49
|
-
]}
|
|
46
|
+
style={[styles.wheelItem, { height: itemSize }, containerStyle]}
|
|
50
47
|
>
|
|
51
48
|
<Animated.Text
|
|
52
49
|
allowFontScaling={false}
|
package/index.tsx
CHANGED
|
@@ -21,6 +21,10 @@ maxDateDefault.setFullYear(maxDateDefault.getFullYear() + 10);
|
|
|
21
21
|
date.setHours(0, 0);
|
|
22
22
|
const screenWidth = Dimensions.get('window').width;
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Wheel date/time picker, one column per `format` token, clamped to min/max.
|
|
26
|
+
* `onChange` also fires on mount with the clamped initial value.
|
|
27
|
+
*/
|
|
24
28
|
const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
25
29
|
format = 'DD-MM-YYYY',
|
|
26
30
|
minuteInterval = 1,
|
|
@@ -61,11 +65,11 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
61
65
|
|
|
62
66
|
const formatParts = format.split(/[^A-Za-z]+/);
|
|
63
67
|
const isOnlyHour = formatParts.length === 1 && formatParts[0] === 'HH';
|
|
64
|
-
const dayData = useMemo(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
);
|
|
68
|
+
const dayData = useMemo(() => {
|
|
69
|
+
const year = +currentDate.year;
|
|
70
|
+
const month = +currentDate.month;
|
|
71
|
+
return getDaysInMonth(year, month, minDate, maxDate);
|
|
72
|
+
}, [currentDate.year, currentDate.month, minDate, maxDate]);
|
|
69
73
|
const monthData = useMemo(
|
|
70
74
|
() => getMonths(minDate, maxDate, +currentDate.year),
|
|
71
75
|
[currentDate.year, minDate, maxDate],
|
|
@@ -135,10 +139,14 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
135
139
|
|
|
136
140
|
// In 12-hour mode the hour and AM/PM wheels both map onto the 24-hour `hour`.
|
|
137
141
|
if (isOnlyHour && (name === 'hour' || name === 'timeMode')) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
+
let hour12 = toHour12(currentDate.hour);
|
|
143
|
+
if (name === 'hour') {
|
|
144
|
+
hour12 = Number(dataItem);
|
|
145
|
+
}
|
|
146
|
+
let mode = toTimeMode(currentDate.hour);
|
|
147
|
+
if (name === 'timeMode') {
|
|
148
|
+
mode = dataItem;
|
|
149
|
+
}
|
|
142
150
|
newDate.hour = (hour12 % 12) + (mode === 'PM' ? 12 : 0);
|
|
143
151
|
}
|
|
144
152
|
|
|
@@ -157,10 +165,9 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
157
165
|
newDate.min,
|
|
158
166
|
);
|
|
159
167
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
) {
|
|
168
|
+
const newValue = JSON.stringify(newDate.valueOf());
|
|
169
|
+
const currentValue = JSON.stringify(currentDate.valueOf());
|
|
170
|
+
if (newValue !== currentValue) {
|
|
164
171
|
if (changedDate < minDate) {
|
|
165
172
|
onChange?.(minDate);
|
|
166
173
|
} else if (changedDate > maxDate) {
|
|
@@ -171,6 +178,8 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
171
178
|
};
|
|
172
179
|
|
|
173
180
|
const paddingArray = ['', ''];
|
|
181
|
+
const columnGaps = Spacing.M * 2 + (numOfColumns - 1) * Spacing.M;
|
|
182
|
+
const columnWidth = (containerWidth - columnGaps) / numOfColumns;
|
|
174
183
|
|
|
175
184
|
const onLayout = (e: LayoutChangeEvent) => {
|
|
176
185
|
if (containerWidth !== e.nativeEvent.layout.width) {
|
|
@@ -193,13 +202,7 @@ const DateTimePicker: FC<DateTimePickerProps> = ({
|
|
|
193
202
|
onChange={onPickerChange}
|
|
194
203
|
data={[...paddingArray, ...dataItem.data, ...paddingArray]}
|
|
195
204
|
name={dataItem.name}
|
|
196
|
-
style={{
|
|
197
|
-
width:
|
|
198
|
-
(containerWidth -
|
|
199
|
-
Spacing.M * 2 -
|
|
200
|
-
(numOfColumns - 1) * Spacing.M) /
|
|
201
|
-
numOfColumns,
|
|
202
|
-
}}
|
|
205
|
+
style={{ width: columnWidth }}
|
|
203
206
|
/>
|
|
204
207
|
</View>
|
|
205
208
|
);
|
package/package.json
CHANGED
package/types.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {ViewStyle} from 'react-native';
|
|
2
|
-
import type {SharedValue} from 'react-native-reanimated';
|
|
1
|
+
import { ViewStyle } from 'react-native';
|
|
2
|
+
import type { SharedValue } from 'react-native-reanimated';
|
|
3
3
|
|
|
4
4
|
export type DateTimePickerProps = {
|
|
5
5
|
/**
|
|
6
|
-
* Optional.
|
|
6
|
+
* Optional. Column tokens `DD`, `MM`, `YYYY`, `HH`, `mm` joined by separators.
|
|
7
|
+
* `HH` alone shows a 12-hour wheel plus an AM/PM wheel.
|
|
7
8
|
*/
|
|
8
9
|
format?: string;
|
|
9
10
|
|
|
@@ -32,7 +33,7 @@ export type DateTimePickerProps = {
|
|
|
32
33
|
*/
|
|
33
34
|
maxDate?: Date;
|
|
34
35
|
/**
|
|
35
|
-
* Optional.
|
|
36
|
+
* Optional. Labels shown above the columns, matched by column index.
|
|
36
37
|
*/
|
|
37
38
|
arrayLabelTime?: string[];
|
|
38
39
|
|
package/utils.ts
CHANGED
|
@@ -3,6 +3,7 @@ const itemSize = 42;
|
|
|
3
3
|
const paddingNum = (num: number) => {
|
|
4
4
|
return num > 9 ? String(num) : `0${num}`;
|
|
5
5
|
};
|
|
6
|
+
/** Zero-padded days of `month` (1-based) in `year`, trimmed to min/max. */
|
|
6
7
|
const getDaysInMonth = (
|
|
7
8
|
year: number,
|
|
8
9
|
month: number,
|
|
@@ -29,6 +30,7 @@ const getDaysInMonth = (
|
|
|
29
30
|
return days;
|
|
30
31
|
};
|
|
31
32
|
|
|
33
|
+
/** Zero-padded months (1–12) of `currentYear`, trimmed to min/max. */
|
|
32
34
|
const getMonths = (minDate: Date, maxDate: Date, currentYear: number) => {
|
|
33
35
|
let startMonth = 1;
|
|
34
36
|
let endMonth = 12;
|
|
@@ -48,6 +50,7 @@ const getMonths = (minDate: Date, maxDate: Date, currentYear: number) => {
|
|
|
48
50
|
return months;
|
|
49
51
|
};
|
|
50
52
|
|
|
53
|
+
/** Years from `minDate` to `maxDate`, inclusive. */
|
|
51
54
|
const getYears = (minDate: Date, maxDate: Date) => {
|
|
52
55
|
const startYear = minDate.getFullYear();
|
|
53
56
|
const endYear = maxDate.getFullYear();
|
|
@@ -59,6 +62,7 @@ const getYears = (minDate: Date, maxDate: Date) => {
|
|
|
59
62
|
return years;
|
|
60
63
|
};
|
|
61
64
|
|
|
65
|
+
/** Zero-padded hours: 01–12 in 12-hour mode, 00–23 otherwise. */
|
|
62
66
|
const getHours = (hourMode: 12 | 24) => {
|
|
63
67
|
const hours = [];
|
|
64
68
|
for (let i = 0; i < hourMode; i++) {
|
|
@@ -71,6 +75,7 @@ const getHours = (hourMode: 12 | 24) => {
|
|
|
71
75
|
return hours;
|
|
72
76
|
};
|
|
73
77
|
|
|
78
|
+
/** Zero-padded minutes from 00 stepping by `interval`. */
|
|
74
79
|
const getMinutes = (interval: number) => {
|
|
75
80
|
const minutes = [];
|
|
76
81
|
for (let min = 0; min < 60; min += interval) {
|
|
@@ -82,6 +87,7 @@ const getMinutes = (interval: number) => {
|
|
|
82
87
|
|
|
83
88
|
const timeMode = ['AM', 'PM'];
|
|
84
89
|
|
|
90
|
+
/** Runs `func` once `delay` ms pass without a new call; `cancel()` drops it. */
|
|
85
91
|
function debounce<T extends (...args: any[]) => any>(func: T, delay: number) {
|
|
86
92
|
let timer: ReturnType<typeof setTimeout>;
|
|
87
93
|
|