react-native-timer-picker 1.2.11 → 1.4.0
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/README.md +22 -2
- package/dist/commonjs/components/Modal/index.js +13 -14
- package/dist/commonjs/components/Modal/index.js.map +1 -1
- package/dist/commonjs/components/TimerPicker/DurationScroll.js +72 -34
- package/dist/commonjs/components/TimerPicker/DurationScroll.js.map +1 -1
- package/dist/commonjs/components/TimerPicker/TimerPicker.styles.js +23 -3
- package/dist/commonjs/components/TimerPicker/TimerPicker.styles.js.map +1 -1
- package/dist/commonjs/components/TimerPicker/index.js +64 -47
- package/dist/commonjs/components/TimerPicker/index.js.map +1 -1
- package/dist/commonjs/components/index.js +90 -69
- package/dist/commonjs/components/index.js.map +1 -1
- package/dist/commonjs/tests/Modal.test.js +3 -1
- package/dist/commonjs/tests/Modal.test.js.map +1 -1
- package/dist/commonjs/utils/generateNumbers.js +33 -10
- package/dist/commonjs/utils/generateNumbers.js.map +1 -1
- package/dist/commonjs/utils/padNumber.js +15 -0
- package/dist/commonjs/utils/padNumber.js.map +1 -0
- package/dist/module/components/Modal/index.js +13 -14
- package/dist/module/components/Modal/index.js.map +1 -1
- package/dist/module/components/TimerPicker/DurationScroll.js +73 -35
- package/dist/module/components/TimerPicker/DurationScroll.js.map +1 -1
- package/dist/module/components/TimerPicker/TimerPicker.styles.js +23 -3
- package/dist/module/components/TimerPicker/TimerPicker.styles.js.map +1 -1
- package/dist/module/components/TimerPicker/index.js +64 -47
- package/dist/module/components/TimerPicker/index.js.map +1 -1
- package/dist/module/components/index.js +90 -69
- package/dist/module/components/index.js.map +1 -1
- package/dist/module/tests/Modal.test.js +3 -1
- package/dist/module/tests/Modal.test.js.map +1 -1
- package/dist/module/utils/generateNumbers.js +31 -9
- package/dist/module/utils/generateNumbers.js.map +1 -1
- package/dist/module/utils/padNumber.js +8 -0
- package/dist/module/utils/padNumber.js.map +1 -0
- package/dist/typescript/components/TimerPicker/DurationScroll.d.ts +6 -1
- package/dist/typescript/components/TimerPicker/TimerPicker.styles.d.ts +4 -0
- package/dist/typescript/components/TimerPicker/index.d.ts +10 -1
- package/dist/typescript/components/index.d.ts +6 -1
- package/dist/typescript/utils/generateNumbers.d.ts +7 -2
- package/dist/typescript/utils/padNumber.d.ts +3 -0
- package/package.json +2 -2
- package/src/components/Modal/index.tsx +2 -2
- package/src/components/TimerPicker/DurationScroll.tsx +95 -12
- package/src/components/TimerPicker/TimerPicker.styles.ts +32 -1
- package/src/components/TimerPicker/index.tsx +39 -6
- package/src/components/index.tsx +45 -15
- package/src/tests/Modal.test.tsx +1 -1
- package/src/utils/generateNumbers.ts +40 -10
- package/src/utils/padNumber.ts +10 -0
- package/dist/commonjs/utils/padWithZero.js +0 -15
- package/dist/commonjs/utils/padWithZero.js.map +0 -1
- package/dist/module/utils/padWithZero.js +0 -8
- package/dist/module/utils/padWithZero.js.map +0 -1
- package/dist/typescript/utils/padWithZero.d.ts +0 -1
- package/src/utils/padWithZero.ts +0 -7
package/src/components/index.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React, {
|
|
2
|
+
MutableRefObject,
|
|
2
3
|
forwardRef,
|
|
3
4
|
useCallback,
|
|
4
5
|
useImperativeHandle,
|
|
@@ -25,6 +26,11 @@ export interface TimerPickerModalRef {
|
|
|
25
26
|
},
|
|
26
27
|
options?: { animated?: boolean }
|
|
27
28
|
) => void;
|
|
29
|
+
latestDuration: {
|
|
30
|
+
hours: MutableRefObject<number> | undefined;
|
|
31
|
+
minutes: MutableRefObject<number> | undefined;
|
|
32
|
+
seconds: MutableRefObject<number> | undefined;
|
|
33
|
+
};
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
export interface TimerPickerModalProps extends TimerPickerProps {
|
|
@@ -72,11 +78,14 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
72
78
|
hourLimit,
|
|
73
79
|
minuteLimit,
|
|
74
80
|
secondLimit,
|
|
75
|
-
hourLabel
|
|
76
|
-
minuteLabel
|
|
77
|
-
secondLabel
|
|
81
|
+
hourLabel,
|
|
82
|
+
minuteLabel,
|
|
83
|
+
secondLabel,
|
|
78
84
|
padWithNItems = 1,
|
|
79
85
|
disableInfiniteScroll = false,
|
|
86
|
+
use12HourPicker,
|
|
87
|
+
amLabel,
|
|
88
|
+
pmLabel,
|
|
80
89
|
hideCancelButton = false,
|
|
81
90
|
confirmButtonText = "Confirm",
|
|
82
91
|
cancelButtonText = "Cancel",
|
|
@@ -98,6 +107,8 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
98
107
|
): React.ReactElement => {
|
|
99
108
|
const styles = generateStyles(customStyles);
|
|
100
109
|
|
|
110
|
+
const timerPickerRef = useRef<TimerPickerRef>(null);
|
|
111
|
+
|
|
101
112
|
const [selectedDuration, setSelectedDuration] = useState({
|
|
102
113
|
hours: initialHours,
|
|
103
114
|
minutes: initialMinutes,
|
|
@@ -109,7 +120,7 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
109
120
|
seconds: initialSeconds,
|
|
110
121
|
});
|
|
111
122
|
|
|
112
|
-
const
|
|
123
|
+
const hideModalHandler = () => {
|
|
113
124
|
setSelectedDuration({
|
|
114
125
|
hours: confirmedDuration.hours,
|
|
115
126
|
minutes: confirmedDuration.minutes,
|
|
@@ -118,19 +129,29 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
118
129
|
setIsVisible(false);
|
|
119
130
|
};
|
|
120
131
|
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
132
|
+
const confirmHandler = () => {
|
|
133
|
+
const latestDuration = timerPickerRef.current?.latestDuration;
|
|
134
|
+
const newDuration = {
|
|
135
|
+
hours: latestDuration?.hours?.current ?? selectedDuration.hours,
|
|
136
|
+
minutes:
|
|
137
|
+
latestDuration?.minutes?.current ??
|
|
138
|
+
selectedDuration.minutes,
|
|
139
|
+
seconds:
|
|
140
|
+
latestDuration?.seconds?.current ??
|
|
141
|
+
selectedDuration.seconds,
|
|
142
|
+
};
|
|
143
|
+
setConfirmedDuration(newDuration);
|
|
144
|
+
onConfirm(newDuration);
|
|
124
145
|
};
|
|
125
146
|
|
|
126
|
-
const
|
|
147
|
+
const cancelHandler = () => {
|
|
127
148
|
setIsVisible(false);
|
|
128
149
|
setSelectedDuration(confirmedDuration);
|
|
129
150
|
onCancel?.();
|
|
130
151
|
};
|
|
131
152
|
|
|
132
153
|
// wrapped in useCallback to avoid unnecessary re-renders of TimerPicker
|
|
133
|
-
const
|
|
154
|
+
const durationChangeHandler = useCallback(
|
|
134
155
|
(duration: { hours: number; minutes: number; seconds: number }) => {
|
|
135
156
|
setSelectedDuration(duration);
|
|
136
157
|
onDurationChange?.(duration);
|
|
@@ -138,8 +159,6 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
138
159
|
[onDurationChange]
|
|
139
160
|
);
|
|
140
161
|
|
|
141
|
-
const timerPickerRef = useRef<TimerPickerRef>(null);
|
|
142
|
-
|
|
143
162
|
useImperativeHandle(ref, () => ({
|
|
144
163
|
reset: (options) => {
|
|
145
164
|
const initialDuration = {
|
|
@@ -156,12 +175,19 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
156
175
|
setConfirmedDuration(value);
|
|
157
176
|
timerPickerRef.current?.setValue(value, options);
|
|
158
177
|
},
|
|
178
|
+
latestDuration: {
|
|
179
|
+
hours: timerPickerRef.current?.latestDuration?.hours,
|
|
180
|
+
minutes: timerPickerRef.current?.latestDuration?.minutes,
|
|
181
|
+
seconds: timerPickerRef.current?.latestDuration?.seconds,
|
|
182
|
+
},
|
|
159
183
|
}));
|
|
160
184
|
|
|
161
185
|
return (
|
|
162
186
|
<Modal
|
|
163
187
|
isVisible={visible}
|
|
164
|
-
onOverlayPress={
|
|
188
|
+
onOverlayPress={
|
|
189
|
+
closeOnOverlayPress ? hideModalHandler : undefined
|
|
190
|
+
}
|
|
165
191
|
{...modalProps}
|
|
166
192
|
testID="timer-picker-modal">
|
|
167
193
|
<View {...containerProps} style={styles.container}>
|
|
@@ -177,10 +203,11 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
177
203
|
) : null}
|
|
178
204
|
<TimerPicker
|
|
179
205
|
ref={timerPickerRef}
|
|
180
|
-
onDurationChange={
|
|
206
|
+
onDurationChange={durationChangeHandler}
|
|
181
207
|
initialHours={confirmedDuration.hours}
|
|
182
208
|
initialMinutes={confirmedDuration.minutes}
|
|
183
209
|
initialSeconds={confirmedDuration.seconds}
|
|
210
|
+
aggressivelyGetLatestDuration={true}
|
|
184
211
|
hideHours={hideHours}
|
|
185
212
|
hideMinutes={hideMinutes}
|
|
186
213
|
hideSeconds={hideSeconds}
|
|
@@ -192,6 +219,9 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
192
219
|
secondLabel={secondLabel}
|
|
193
220
|
padWithNItems={padWithNItems}
|
|
194
221
|
disableInfiniteScroll={disableInfiniteScroll}
|
|
222
|
+
use12HourPicker={use12HourPicker}
|
|
223
|
+
amLabel={amLabel}
|
|
224
|
+
pmLabel={pmLabel}
|
|
195
225
|
LinearGradient={LinearGradient}
|
|
196
226
|
pickerContainerProps={pickerContainerProps}
|
|
197
227
|
pickerGradientOverlayProps={
|
|
@@ -210,7 +240,7 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
210
240
|
style={styles.buttonContainer}>
|
|
211
241
|
{!hideCancelButton ? (
|
|
212
242
|
<TouchableOpacity
|
|
213
|
-
onPress={
|
|
243
|
+
onPress={cancelHandler}
|
|
214
244
|
{...buttonTouchableOpacityProps}>
|
|
215
245
|
<Text
|
|
216
246
|
style={[
|
|
@@ -222,7 +252,7 @@ const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
|
222
252
|
</TouchableOpacity>
|
|
223
253
|
) : null}
|
|
224
254
|
<TouchableOpacity
|
|
225
|
-
onPress={
|
|
255
|
+
onPress={confirmHandler}
|
|
226
256
|
{...buttonTouchableOpacityProps}>
|
|
227
257
|
<Text
|
|
228
258
|
style={[
|
package/src/tests/Modal.test.tsx
CHANGED
|
@@ -5,7 +5,7 @@ import Modal from "../components/Modal";
|
|
|
5
5
|
|
|
6
6
|
describe("Modal", () => {
|
|
7
7
|
it("renders without crashing", () => {
|
|
8
|
-
const { getByTestId } = render(<Modal />);
|
|
8
|
+
const { getByTestId } = render(<Modal isVisible/>);
|
|
9
9
|
const component = getByTestId("modal");
|
|
10
10
|
expect(component).toBeDefined();
|
|
11
11
|
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { padNumber } from "./padNumber";
|
|
2
2
|
|
|
3
3
|
export const generateNumbers = (
|
|
4
4
|
numberOfItems: number,
|
|
5
5
|
options: {
|
|
6
6
|
repeatNTimes?: number;
|
|
7
|
-
|
|
7
|
+
padNumbersWithZero?: boolean;
|
|
8
8
|
disableInfiniteScroll?: boolean;
|
|
9
9
|
padWithNItems: number;
|
|
10
10
|
}
|
|
@@ -14,15 +14,10 @@ export const generateNumbers = (
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
let numbers: string[] = [];
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
numbers.push(padWithZero(i));
|
|
20
|
-
}
|
|
21
|
-
} else {
|
|
22
|
-
for (let i = 0; i <= numberOfItems; i++) {
|
|
23
|
-
numbers.push(String(i));
|
|
24
|
-
}
|
|
17
|
+
for (let i = 0; i <= numberOfItems; i++) {
|
|
18
|
+
numbers.push(padNumber(i, { padWithZero: options.padNumbersWithZero }));
|
|
25
19
|
}
|
|
20
|
+
|
|
26
21
|
if ((options.repeatNTimes ?? 1) > 1) {
|
|
27
22
|
numbers = Array(options.repeatNTimes).fill(numbers).flat();
|
|
28
23
|
}
|
|
@@ -32,3 +27,38 @@ export const generateNumbers = (
|
|
|
32
27
|
}
|
|
33
28
|
return numbers;
|
|
34
29
|
};
|
|
30
|
+
|
|
31
|
+
export const generate12HourNumbers = (options: {
|
|
32
|
+
repeatNTimes?: number;
|
|
33
|
+
padNumbersWithZero?: boolean;
|
|
34
|
+
disableInfiniteScroll?: boolean;
|
|
35
|
+
padWithNItems: number;
|
|
36
|
+
}) => {
|
|
37
|
+
let numbers: string[] = [];
|
|
38
|
+
|
|
39
|
+
// Generate numbers from 0 to 11 for AM
|
|
40
|
+
for (let i = 0; i <= 11; i++) {
|
|
41
|
+
numbers.push(
|
|
42
|
+
`${padNumber(i, { padWithZero: options.padNumbersWithZero })} AM`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Generate numbers from 12 to 11 for PM
|
|
47
|
+
for (let i = 12; i <= 23; i++) {
|
|
48
|
+
const hour = i > 12 ? i - 12 : i;
|
|
49
|
+
numbers.push(
|
|
50
|
+
`${padNumber(hour, { padWithZero: options.padNumbersWithZero })} PM`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if ((options.repeatNTimes ?? 1) > 1) {
|
|
55
|
+
numbers = Array(options.repeatNTimes).fill(numbers).flat();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (options.disableInfiniteScroll) {
|
|
59
|
+
numbers.push(...Array(options.padWithNItems).fill(""));
|
|
60
|
+
numbers.unshift(...Array(options.padWithNItems).fill(""));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return numbers;
|
|
64
|
+
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.padWithZero = void 0;
|
|
7
|
-
const padWithZero = value => {
|
|
8
|
-
if (value < 10) {
|
|
9
|
-
return "0" + value;
|
|
10
|
-
} else {
|
|
11
|
-
return String(value);
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
exports.padWithZero = padWithZero;
|
|
15
|
-
//# sourceMappingURL=padWithZero.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["padWithZero","value","String","exports"],"sources":["padWithZero.ts"],"sourcesContent":["export const padWithZero = (value: number): string => {\n if (value < 10) {\n return \"0\" + value;\n } else {\n return String(value);\n }\n};"],"mappings":";;;;;;AAAO,MAAMA,WAAW,GAAIC,KAAa,IAAa;EAClD,IAAIA,KAAK,GAAG,EAAE,EAAE;IACZ,OAAO,GAAG,GAAGA,KAAK;EACtB,CAAC,MAAM;IACH,OAAOC,MAAM,CAACD,KAAK,CAAC;EACxB;AACJ,CAAC;AAACE,OAAA,CAAAH,WAAA,GAAAA,WAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["padWithZero","value","String"],"sources":["padWithZero.ts"],"sourcesContent":["export const padWithZero = (value: number): string => {\n if (value < 10) {\n return \"0\" + value;\n } else {\n return String(value);\n }\n};"],"mappings":"AAAA,OAAO,MAAMA,WAAW,GAAIC,KAAa,IAAa;EAClD,IAAIA,KAAK,GAAG,EAAE,EAAE;IACZ,OAAO,GAAG,GAAGA,KAAK;EACtB,CAAC,MAAM;IACH,OAAOC,MAAM,CAACD,KAAK,CAAC;EACxB;AACJ,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const padWithZero: (value: number) => string;
|