react-native-timer-picker 1.9.0 → 1.10.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 +3 -0
- package/dist/commonjs/components/TimerPicker/index.js +6 -3
- package/dist/commonjs/components/TimerPicker/index.js.map +1 -1
- package/dist/commonjs/components/TimerPicker/types.js.map +1 -1
- package/dist/commonjs/tests/TimerPicker.test.js +14 -0
- package/dist/commonjs/tests/TimerPicker.test.js.map +1 -1
- package/dist/module/components/TimerPicker/index.js +6 -3
- package/dist/module/components/TimerPicker/index.js.map +1 -1
- package/dist/module/components/TimerPicker/types.js.map +1 -1
- package/dist/module/tests/TimerPicker.test.js +14 -0
- package/dist/module/tests/TimerPicker.test.js.map +1 -1
- package/dist/typescript/components/TimerPicker/types.d.ts +3 -0
- package/package.json +1 -2
- package/src/components/DurationScroll/index.tsx +0 -459
- package/src/components/DurationScroll/types.ts +0 -75
- package/src/components/Modal/index.tsx +0 -124
- package/src/components/Modal/styles.ts +0 -26
- package/src/components/Modal/types.ts +0 -17
- package/src/components/TimerPicker/index.tsx +0 -196
- package/src/components/TimerPicker/styles.ts +0 -123
- package/src/components/TimerPicker/types.ts +0 -74
- package/src/components/TimerPickerModal/index.tsx +0 -190
- package/src/components/TimerPickerModal/styles.ts +0 -88
- package/src/components/TimerPickerModal/types.ts +0 -52
- package/src/index.ts +0 -13
- package/src/tests/DurationScroll.test.tsx +0 -66
- package/src/tests/Modal.test.tsx +0 -36
- package/src/tests/TimerPicker.test.tsx +0 -29
- package/src/tests/TimerPickerModal.test.tsx +0 -72
- package/src/utils/colorToRgba.ts +0 -49
- package/src/utils/generateNumbers.ts +0 -64
- package/src/utils/getAdjustedLimit.ts +0 -35
- package/src/utils/getScrollIndex.ts +0 -15
- package/src/utils/padNumber.ts +0 -10
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
forwardRef,
|
|
3
|
-
useEffect,
|
|
4
|
-
useImperativeHandle,
|
|
5
|
-
useMemo,
|
|
6
|
-
useRef,
|
|
7
|
-
useState,
|
|
8
|
-
} from "react";
|
|
9
|
-
|
|
10
|
-
import { View } from "react-native";
|
|
11
|
-
|
|
12
|
-
import DurationScroll from "../DurationScroll";
|
|
13
|
-
import type { DurationScrollRef } from "../DurationScroll/types";
|
|
14
|
-
|
|
15
|
-
import { generateStyles } from "./styles";
|
|
16
|
-
import type { TimerPickerProps, TimerPickerRef } from "./types";
|
|
17
|
-
|
|
18
|
-
const TimerPicker = forwardRef<TimerPickerRef, TimerPickerProps>(
|
|
19
|
-
(props, ref) => {
|
|
20
|
-
const {
|
|
21
|
-
aggressivelyGetLatestDuration = false,
|
|
22
|
-
allowFontScaling = false,
|
|
23
|
-
amLabel = "am",
|
|
24
|
-
disableInfiniteScroll = false,
|
|
25
|
-
FlatList,
|
|
26
|
-
hideHours = false,
|
|
27
|
-
hideMinutes = false,
|
|
28
|
-
hideSeconds = false,
|
|
29
|
-
hourLabel,
|
|
30
|
-
hourLimit,
|
|
31
|
-
hoursPickerIsDisabled = false,
|
|
32
|
-
initialValue,
|
|
33
|
-
minuteLabel,
|
|
34
|
-
minuteLimit,
|
|
35
|
-
minutesPickerIsDisabled = false,
|
|
36
|
-
onDurationChange,
|
|
37
|
-
padWithNItems = 1,
|
|
38
|
-
pickerContainerProps,
|
|
39
|
-
pmLabel = "pm",
|
|
40
|
-
secondLabel,
|
|
41
|
-
secondLimit,
|
|
42
|
-
secondsPickerIsDisabled = false,
|
|
43
|
-
styles: customStyles,
|
|
44
|
-
use12HourPicker = false,
|
|
45
|
-
...otherProps
|
|
46
|
-
} = props;
|
|
47
|
-
|
|
48
|
-
const checkedPadWithNItems =
|
|
49
|
-
padWithNItems >= 0 ? Math.round(padWithNItems) : 0;
|
|
50
|
-
|
|
51
|
-
const styles = useMemo(
|
|
52
|
-
() =>
|
|
53
|
-
generateStyles(customStyles, {
|
|
54
|
-
padWithNItems: checkedPadWithNItems,
|
|
55
|
-
}),
|
|
56
|
-
|
|
57
|
-
[checkedPadWithNItems, customStyles]
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
const safeInitialValue = {
|
|
61
|
-
hours: initialValue?.hours ?? 0,
|
|
62
|
-
minutes: initialValue?.minutes ?? 0,
|
|
63
|
-
seconds: initialValue?.seconds ?? 0,
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const [selectedHours, setSelectedHours] = useState(
|
|
67
|
-
safeInitialValue.hours
|
|
68
|
-
);
|
|
69
|
-
const [selectedMinutes, setSelectedMinutes] = useState(
|
|
70
|
-
safeInitialValue.minutes
|
|
71
|
-
);
|
|
72
|
-
const [selectedSeconds, setSelectedSeconds] = useState(
|
|
73
|
-
safeInitialValue.seconds
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
useEffect(() => {
|
|
77
|
-
onDurationChange?.({
|
|
78
|
-
hours: selectedHours,
|
|
79
|
-
minutes: selectedMinutes,
|
|
80
|
-
seconds: selectedSeconds,
|
|
81
|
-
});
|
|
82
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
83
|
-
}, [selectedHours, selectedMinutes, selectedSeconds]);
|
|
84
|
-
|
|
85
|
-
const hoursDurationScrollRef = useRef<DurationScrollRef>(null);
|
|
86
|
-
const minutesDurationScrollRef = useRef<DurationScrollRef>(null);
|
|
87
|
-
const secondsDurationScrollRef = useRef<DurationScrollRef>(null);
|
|
88
|
-
|
|
89
|
-
useImperativeHandle(ref, () => ({
|
|
90
|
-
reset: (options) => {
|
|
91
|
-
setSelectedHours(safeInitialValue.hours);
|
|
92
|
-
setSelectedMinutes(safeInitialValue.minutes);
|
|
93
|
-
setSelectedSeconds(safeInitialValue.seconds);
|
|
94
|
-
hoursDurationScrollRef.current?.reset(options);
|
|
95
|
-
minutesDurationScrollRef.current?.reset(options);
|
|
96
|
-
secondsDurationScrollRef.current?.reset(options);
|
|
97
|
-
},
|
|
98
|
-
setValue: (value, options) => {
|
|
99
|
-
setSelectedHours(value.hours);
|
|
100
|
-
setSelectedMinutes(value.minutes);
|
|
101
|
-
setSelectedSeconds(value.seconds);
|
|
102
|
-
hoursDurationScrollRef.current?.setValue(value.hours, options);
|
|
103
|
-
minutesDurationScrollRef.current?.setValue(
|
|
104
|
-
value.minutes,
|
|
105
|
-
options
|
|
106
|
-
);
|
|
107
|
-
secondsDurationScrollRef.current?.setValue(
|
|
108
|
-
value.seconds,
|
|
109
|
-
options
|
|
110
|
-
);
|
|
111
|
-
},
|
|
112
|
-
latestDuration: {
|
|
113
|
-
hours: hoursDurationScrollRef.current?.latestDuration,
|
|
114
|
-
minutes: minutesDurationScrollRef.current?.latestDuration,
|
|
115
|
-
seconds: secondsDurationScrollRef.current?.latestDuration,
|
|
116
|
-
},
|
|
117
|
-
}));
|
|
118
|
-
|
|
119
|
-
return (
|
|
120
|
-
<View
|
|
121
|
-
{...pickerContainerProps}
|
|
122
|
-
style={styles.pickerContainer}
|
|
123
|
-
testID="timer-picker">
|
|
124
|
-
{!hideHours ? (
|
|
125
|
-
<DurationScroll
|
|
126
|
-
ref={hoursDurationScrollRef}
|
|
127
|
-
aggressivelyGetLatestDuration={
|
|
128
|
-
aggressivelyGetLatestDuration
|
|
129
|
-
}
|
|
130
|
-
allowFontScaling={allowFontScaling}
|
|
131
|
-
amLabel={amLabel}
|
|
132
|
-
disableInfiniteScroll={disableInfiniteScroll}
|
|
133
|
-
initialValue={safeInitialValue.hours}
|
|
134
|
-
is12HourPicker={use12HourPicker}
|
|
135
|
-
isDisabled={hoursPickerIsDisabled}
|
|
136
|
-
label={
|
|
137
|
-
hourLabel ?? (!use12HourPicker ? "h" : undefined)
|
|
138
|
-
}
|
|
139
|
-
limit={hourLimit}
|
|
140
|
-
numberOfItems={23}
|
|
141
|
-
onDurationChange={setSelectedHours}
|
|
142
|
-
padWithNItems={checkedPadWithNItems}
|
|
143
|
-
pmLabel={pmLabel}
|
|
144
|
-
styles={styles}
|
|
145
|
-
testID="duration-scroll-hour"
|
|
146
|
-
{...otherProps}
|
|
147
|
-
/>
|
|
148
|
-
) : null}
|
|
149
|
-
{!hideMinutes ? (
|
|
150
|
-
<DurationScroll
|
|
151
|
-
ref={minutesDurationScrollRef}
|
|
152
|
-
aggressivelyGetLatestDuration={
|
|
153
|
-
aggressivelyGetLatestDuration
|
|
154
|
-
}
|
|
155
|
-
allowFontScaling={allowFontScaling}
|
|
156
|
-
disableInfiniteScroll={disableInfiniteScroll}
|
|
157
|
-
initialValue={safeInitialValue.minutes}
|
|
158
|
-
isDisabled={minutesPickerIsDisabled}
|
|
159
|
-
label={minuteLabel ?? "m"}
|
|
160
|
-
limit={minuteLimit}
|
|
161
|
-
numberOfItems={59}
|
|
162
|
-
onDurationChange={setSelectedMinutes}
|
|
163
|
-
padNumbersWithZero
|
|
164
|
-
padWithNItems={checkedPadWithNItems}
|
|
165
|
-
styles={styles}
|
|
166
|
-
testID="duration-scroll-minute"
|
|
167
|
-
{...otherProps}
|
|
168
|
-
/>
|
|
169
|
-
) : null}
|
|
170
|
-
{!hideSeconds ? (
|
|
171
|
-
<DurationScroll
|
|
172
|
-
ref={secondsDurationScrollRef}
|
|
173
|
-
aggressivelyGetLatestDuration={
|
|
174
|
-
aggressivelyGetLatestDuration
|
|
175
|
-
}
|
|
176
|
-
allowFontScaling={allowFontScaling}
|
|
177
|
-
disableInfiniteScroll={disableInfiniteScroll}
|
|
178
|
-
initialValue={safeInitialValue.seconds}
|
|
179
|
-
isDisabled={secondsPickerIsDisabled}
|
|
180
|
-
label={secondLabel ?? "s"}
|
|
181
|
-
limit={secondLimit}
|
|
182
|
-
numberOfItems={59}
|
|
183
|
-
onDurationChange={setSelectedSeconds}
|
|
184
|
-
padNumbersWithZero
|
|
185
|
-
padWithNItems={checkedPadWithNItems}
|
|
186
|
-
styles={styles}
|
|
187
|
-
testID="duration-scroll-second"
|
|
188
|
-
{...otherProps}
|
|
189
|
-
/>
|
|
190
|
-
) : null}
|
|
191
|
-
</View>
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
);
|
|
195
|
-
|
|
196
|
-
export default React.memo(TimerPicker);
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { StyleSheet } from "react-native";
|
|
2
|
-
import type { TextStyle, ViewStyle } from "react-native";
|
|
3
|
-
|
|
4
|
-
export interface CustomTimerPickerStyles {
|
|
5
|
-
backgroundColor?: string;
|
|
6
|
-
disabledPickerContainer?: ViewStyle;
|
|
7
|
-
disabledPickerItem?: TextStyle;
|
|
8
|
-
pickerAmPmContainer?: ViewStyle;
|
|
9
|
-
pickerAmPmLabel?: TextStyle;
|
|
10
|
-
pickerContainer?: ViewStyle & { backgroundColor?: string };
|
|
11
|
-
pickerGradientOverlay?: ViewStyle;
|
|
12
|
-
pickerItem?: TextStyle;
|
|
13
|
-
pickerItemContainer?: ViewStyle & { height?: number };
|
|
14
|
-
pickerLabel?: TextStyle;
|
|
15
|
-
pickerLabelContainer?: ViewStyle;
|
|
16
|
-
text?: TextStyle;
|
|
17
|
-
theme?: "light" | "dark";
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const DARK_MODE_BACKGROUND_COLOR = "#232323";
|
|
21
|
-
const DARK_MODE_TEXT_COLOR = "#E9E9E9";
|
|
22
|
-
const LIGHT_MODE_BACKGROUND_COLOR = "#F1F1F1";
|
|
23
|
-
const LIGHT_MODE_TEXT_COLOR = "#1B1B1B";
|
|
24
|
-
|
|
25
|
-
export const generateStyles = (
|
|
26
|
-
customStyles: CustomTimerPickerStyles | undefined,
|
|
27
|
-
options: { padWithNItems: number }
|
|
28
|
-
) =>
|
|
29
|
-
StyleSheet.create({
|
|
30
|
-
pickerContainer: {
|
|
31
|
-
flexDirection: "row",
|
|
32
|
-
marginRight: "8%",
|
|
33
|
-
backgroundColor:
|
|
34
|
-
customStyles?.backgroundColor ??
|
|
35
|
-
(customStyles?.theme === "dark"
|
|
36
|
-
? DARK_MODE_BACKGROUND_COLOR
|
|
37
|
-
: LIGHT_MODE_BACKGROUND_COLOR),
|
|
38
|
-
...customStyles?.pickerContainer,
|
|
39
|
-
},
|
|
40
|
-
pickerLabelContainer: {
|
|
41
|
-
position: "absolute",
|
|
42
|
-
right: 4,
|
|
43
|
-
top: 0,
|
|
44
|
-
bottom: 0,
|
|
45
|
-
justifyContent: "center",
|
|
46
|
-
minWidth:
|
|
47
|
-
(customStyles?.pickerLabel?.fontSize ??
|
|
48
|
-
customStyles?.text?.fontSize ??
|
|
49
|
-
25) * 0.65,
|
|
50
|
-
...customStyles?.pickerLabelContainer,
|
|
51
|
-
},
|
|
52
|
-
pickerLabel: {
|
|
53
|
-
fontSize: 18,
|
|
54
|
-
fontWeight: "bold",
|
|
55
|
-
marginTop:
|
|
56
|
-
(customStyles?.pickerItem?.fontSize ??
|
|
57
|
-
customStyles?.text?.fontSize ??
|
|
58
|
-
25) / 6,
|
|
59
|
-
color:
|
|
60
|
-
customStyles?.theme === "dark"
|
|
61
|
-
? DARK_MODE_TEXT_COLOR
|
|
62
|
-
: LIGHT_MODE_TEXT_COLOR,
|
|
63
|
-
...customStyles?.text,
|
|
64
|
-
...customStyles?.pickerLabel,
|
|
65
|
-
},
|
|
66
|
-
pickerItemContainer: {
|
|
67
|
-
flexDirection: "row",
|
|
68
|
-
height: 50,
|
|
69
|
-
justifyContent: "center",
|
|
70
|
-
alignItems: "center",
|
|
71
|
-
width: (customStyles?.pickerItem?.fontSize ?? 25) * 3.6,
|
|
72
|
-
...customStyles?.pickerItemContainer,
|
|
73
|
-
},
|
|
74
|
-
pickerItem: {
|
|
75
|
-
textAlignVertical: "center",
|
|
76
|
-
fontSize: 25,
|
|
77
|
-
color:
|
|
78
|
-
customStyles?.theme === "dark"
|
|
79
|
-
? DARK_MODE_TEXT_COLOR
|
|
80
|
-
: LIGHT_MODE_TEXT_COLOR,
|
|
81
|
-
...customStyles?.text,
|
|
82
|
-
...customStyles?.pickerItem,
|
|
83
|
-
},
|
|
84
|
-
pickerAmPmContainer: {
|
|
85
|
-
position: "absolute",
|
|
86
|
-
right: 0,
|
|
87
|
-
top: 0,
|
|
88
|
-
bottom: 0,
|
|
89
|
-
justifyContent: "center",
|
|
90
|
-
...customStyles?.pickerLabelContainer,
|
|
91
|
-
...customStyles?.pickerAmPmContainer,
|
|
92
|
-
},
|
|
93
|
-
pickerAmPmLabel: {
|
|
94
|
-
fontSize: 18,
|
|
95
|
-
fontWeight: "bold",
|
|
96
|
-
marginTop: (customStyles?.pickerItem?.fontSize ?? 25) / 6,
|
|
97
|
-
color:
|
|
98
|
-
customStyles?.theme === "dark"
|
|
99
|
-
? DARK_MODE_TEXT_COLOR
|
|
100
|
-
: LIGHT_MODE_TEXT_COLOR,
|
|
101
|
-
...customStyles?.text,
|
|
102
|
-
...customStyles?.pickerLabel,
|
|
103
|
-
...customStyles?.pickerAmPmLabel,
|
|
104
|
-
},
|
|
105
|
-
disabledPickerContainer: {
|
|
106
|
-
opacity: 0.4,
|
|
107
|
-
...customStyles?.disabledPickerContainer,
|
|
108
|
-
},
|
|
109
|
-
disabledPickerItem: {
|
|
110
|
-
opacity: 0.2,
|
|
111
|
-
...customStyles?.disabledPickerItem,
|
|
112
|
-
},
|
|
113
|
-
pickerGradientOverlay: {
|
|
114
|
-
position: "absolute",
|
|
115
|
-
left: 0,
|
|
116
|
-
right: 0,
|
|
117
|
-
height:
|
|
118
|
-
options.padWithNItems === 0
|
|
119
|
-
? "30%"
|
|
120
|
-
: (customStyles?.pickerItemContainer?.height ?? 50) * 0.8,
|
|
121
|
-
...customStyles?.pickerGradientOverlay,
|
|
122
|
-
},
|
|
123
|
-
});
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import type { MutableRefObject } from "react";
|
|
2
|
-
|
|
3
|
-
import type { View } from "react-native";
|
|
4
|
-
|
|
5
|
-
import type {
|
|
6
|
-
LinearGradientProps,
|
|
7
|
-
SoundAssetType,
|
|
8
|
-
LimitType,
|
|
9
|
-
CustomFlatList,
|
|
10
|
-
} from "../DurationScroll/types";
|
|
11
|
-
|
|
12
|
-
import type { CustomTimerPickerStyles } from "./styles";
|
|
13
|
-
|
|
14
|
-
export interface TimerPickerRef {
|
|
15
|
-
latestDuration: {
|
|
16
|
-
hours: MutableRefObject<number> | undefined;
|
|
17
|
-
minutes: MutableRefObject<number> | undefined;
|
|
18
|
-
seconds: MutableRefObject<number> | undefined;
|
|
19
|
-
};
|
|
20
|
-
reset: (options?: { animated?: boolean }) => void;
|
|
21
|
-
setValue: (
|
|
22
|
-
value: {
|
|
23
|
-
hours: number;
|
|
24
|
-
minutes: number;
|
|
25
|
-
seconds: number;
|
|
26
|
-
},
|
|
27
|
-
options?: { animated?: boolean }
|
|
28
|
-
) => void;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface TimerPickerProps {
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
-
Audio?: any;
|
|
34
|
-
FlatList?: CustomFlatList;
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
-
Haptics?: any;
|
|
37
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
-
LinearGradient?: any;
|
|
39
|
-
aggressivelyGetLatestDuration?: boolean;
|
|
40
|
-
allowFontScaling?: boolean;
|
|
41
|
-
amLabel?: string;
|
|
42
|
-
bottomPickerGradientOverlayProps?: Partial<LinearGradientProps>;
|
|
43
|
-
clickSoundAsset?: SoundAssetType;
|
|
44
|
-
disableInfiniteScroll?: boolean;
|
|
45
|
-
hideHours?: boolean;
|
|
46
|
-
hideMinutes?: boolean;
|
|
47
|
-
hideSeconds?: boolean;
|
|
48
|
-
hourLabel?: string | React.ReactElement;
|
|
49
|
-
hourLimit?: LimitType;
|
|
50
|
-
hoursPickerIsDisabled?: boolean;
|
|
51
|
-
initialValue?: {
|
|
52
|
-
hours?: number;
|
|
53
|
-
minutes?: number;
|
|
54
|
-
seconds?: number;
|
|
55
|
-
};
|
|
56
|
-
minuteLabel?: string | React.ReactElement;
|
|
57
|
-
minuteLimit?: LimitType;
|
|
58
|
-
minutesPickerIsDisabled?: boolean;
|
|
59
|
-
onDurationChange?: (duration: {
|
|
60
|
-
hours: number;
|
|
61
|
-
minutes: number;
|
|
62
|
-
seconds: number;
|
|
63
|
-
}) => void;
|
|
64
|
-
padWithNItems?: number;
|
|
65
|
-
pickerContainerProps?: React.ComponentProps<typeof View>;
|
|
66
|
-
pickerGradientOverlayProps?: Partial<LinearGradientProps>;
|
|
67
|
-
pmLabel?: string;
|
|
68
|
-
secondLabel?: string | React.ReactElement;
|
|
69
|
-
secondLimit?: LimitType;
|
|
70
|
-
secondsPickerIsDisabled?: boolean;
|
|
71
|
-
styles?: CustomTimerPickerStyles;
|
|
72
|
-
topPickerGradientOverlayProps?: Partial<LinearGradientProps>;
|
|
73
|
-
use12HourPicker?: boolean;
|
|
74
|
-
}
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
forwardRef,
|
|
3
|
-
useCallback,
|
|
4
|
-
useEffect,
|
|
5
|
-
useImperativeHandle,
|
|
6
|
-
useRef,
|
|
7
|
-
useState,
|
|
8
|
-
} from "react";
|
|
9
|
-
|
|
10
|
-
import { View, Text, TouchableOpacity } from "react-native";
|
|
11
|
-
|
|
12
|
-
import Modal from "../Modal";
|
|
13
|
-
import TimerPicker from "../TimerPicker";
|
|
14
|
-
import type { TimerPickerRef } from "../TimerPicker/types";
|
|
15
|
-
|
|
16
|
-
import { generateStyles } from "./styles";
|
|
17
|
-
import type { TimerPickerModalRef, TimerPickerModalProps } from "./types";
|
|
18
|
-
|
|
19
|
-
const TimerPickerModal = forwardRef<TimerPickerModalRef, TimerPickerModalProps>(
|
|
20
|
-
(props, ref) => {
|
|
21
|
-
const {
|
|
22
|
-
buttonContainerProps,
|
|
23
|
-
buttonTouchableOpacityProps,
|
|
24
|
-
cancelButtonText = "Cancel",
|
|
25
|
-
closeOnOverlayPress,
|
|
26
|
-
confirmButtonText = "Confirm",
|
|
27
|
-
containerProps,
|
|
28
|
-
contentContainerProps,
|
|
29
|
-
hideCancelButton = false,
|
|
30
|
-
initialValue,
|
|
31
|
-
modalProps,
|
|
32
|
-
modalTitle,
|
|
33
|
-
modalTitleProps,
|
|
34
|
-
onCancel,
|
|
35
|
-
onConfirm,
|
|
36
|
-
onDurationChange,
|
|
37
|
-
setIsVisible,
|
|
38
|
-
styles: customStyles,
|
|
39
|
-
visible,
|
|
40
|
-
...otherProps
|
|
41
|
-
} = props;
|
|
42
|
-
|
|
43
|
-
const styles = generateStyles(customStyles);
|
|
44
|
-
|
|
45
|
-
const timerPickerRef = useRef<TimerPickerRef>(null);
|
|
46
|
-
|
|
47
|
-
const safeInitialValue = {
|
|
48
|
-
hours: initialValue?.hours ?? 0,
|
|
49
|
-
minutes: initialValue?.minutes ?? 0,
|
|
50
|
-
seconds: initialValue?.seconds ?? 0,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const [selectedDuration, setSelectedDuration] =
|
|
54
|
-
useState(safeInitialValue);
|
|
55
|
-
const [confirmedDuration, setConfirmedDuration] =
|
|
56
|
-
useState(safeInitialValue);
|
|
57
|
-
|
|
58
|
-
const reset = (options?: { animated?: boolean }) => {
|
|
59
|
-
setSelectedDuration(safeInitialValue);
|
|
60
|
-
setConfirmedDuration(safeInitialValue);
|
|
61
|
-
timerPickerRef.current?.reset(options);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// reset state if the initial value changes
|
|
65
|
-
useEffect(() => {
|
|
66
|
-
reset();
|
|
67
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
68
|
-
}, [
|
|
69
|
-
safeInitialValue.hours,
|
|
70
|
-
safeInitialValue.minutes,
|
|
71
|
-
safeInitialValue.seconds,
|
|
72
|
-
]);
|
|
73
|
-
|
|
74
|
-
const hideModalHandler = () => {
|
|
75
|
-
setSelectedDuration({
|
|
76
|
-
hours: confirmedDuration.hours,
|
|
77
|
-
minutes: confirmedDuration.minutes,
|
|
78
|
-
seconds: confirmedDuration.seconds,
|
|
79
|
-
});
|
|
80
|
-
setIsVisible(false);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const confirmHandler = () => {
|
|
84
|
-
const latestDuration = timerPickerRef.current?.latestDuration;
|
|
85
|
-
|
|
86
|
-
const newDuration = {
|
|
87
|
-
hours: latestDuration?.hours?.current ?? selectedDuration.hours,
|
|
88
|
-
minutes:
|
|
89
|
-
latestDuration?.minutes?.current ??
|
|
90
|
-
selectedDuration.minutes,
|
|
91
|
-
seconds:
|
|
92
|
-
latestDuration?.seconds?.current ??
|
|
93
|
-
selectedDuration.seconds,
|
|
94
|
-
};
|
|
95
|
-
setConfirmedDuration(newDuration);
|
|
96
|
-
onConfirm(newDuration);
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const cancelHandler = () => {
|
|
100
|
-
setIsVisible(false);
|
|
101
|
-
setSelectedDuration(confirmedDuration);
|
|
102
|
-
onCancel?.();
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
// wrapped in useCallback to avoid unnecessary re-renders of TimerPicker
|
|
106
|
-
const durationChangeHandler = useCallback(
|
|
107
|
-
(duration: { hours: number; minutes: number; seconds: number }) => {
|
|
108
|
-
setSelectedDuration(duration);
|
|
109
|
-
onDurationChange?.(duration);
|
|
110
|
-
},
|
|
111
|
-
[onDurationChange]
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
useImperativeHandle(ref, () => ({
|
|
115
|
-
reset,
|
|
116
|
-
setValue: (value, options) => {
|
|
117
|
-
setSelectedDuration(value);
|
|
118
|
-
setConfirmedDuration(value);
|
|
119
|
-
timerPickerRef.current?.setValue(value, options);
|
|
120
|
-
},
|
|
121
|
-
latestDuration: {
|
|
122
|
-
hours: timerPickerRef.current?.latestDuration?.hours,
|
|
123
|
-
minutes: timerPickerRef.current?.latestDuration?.minutes,
|
|
124
|
-
seconds: timerPickerRef.current?.latestDuration?.seconds,
|
|
125
|
-
},
|
|
126
|
-
}));
|
|
127
|
-
|
|
128
|
-
return (
|
|
129
|
-
<Modal
|
|
130
|
-
isVisible={visible}
|
|
131
|
-
onOverlayPress={
|
|
132
|
-
closeOnOverlayPress ? hideModalHandler : undefined
|
|
133
|
-
}
|
|
134
|
-
{...modalProps}
|
|
135
|
-
testID="timer-picker-modal">
|
|
136
|
-
<View {...containerProps} style={styles.container}>
|
|
137
|
-
<View
|
|
138
|
-
{...contentContainerProps}
|
|
139
|
-
style={styles.contentContainer}>
|
|
140
|
-
{modalTitle ? (
|
|
141
|
-
<Text
|
|
142
|
-
{...modalTitleProps}
|
|
143
|
-
style={styles.modalTitle}>
|
|
144
|
-
{modalTitle}
|
|
145
|
-
</Text>
|
|
146
|
-
) : null}
|
|
147
|
-
<TimerPicker
|
|
148
|
-
ref={timerPickerRef}
|
|
149
|
-
initialValue={confirmedDuration}
|
|
150
|
-
{...otherProps}
|
|
151
|
-
aggressivelyGetLatestDuration
|
|
152
|
-
onDurationChange={durationChangeHandler}
|
|
153
|
-
styles={customStyles}
|
|
154
|
-
/>
|
|
155
|
-
<View
|
|
156
|
-
{...buttonContainerProps}
|
|
157
|
-
style={styles.buttonContainer}>
|
|
158
|
-
{!hideCancelButton ? (
|
|
159
|
-
<TouchableOpacity
|
|
160
|
-
{...buttonTouchableOpacityProps}
|
|
161
|
-
onPress={cancelHandler}>
|
|
162
|
-
<Text
|
|
163
|
-
style={[
|
|
164
|
-
styles.button,
|
|
165
|
-
styles.cancelButton,
|
|
166
|
-
]}>
|
|
167
|
-
{cancelButtonText}
|
|
168
|
-
</Text>
|
|
169
|
-
</TouchableOpacity>
|
|
170
|
-
) : null}
|
|
171
|
-
<TouchableOpacity
|
|
172
|
-
{...buttonTouchableOpacityProps}
|
|
173
|
-
onPress={confirmHandler}>
|
|
174
|
-
<Text
|
|
175
|
-
style={[
|
|
176
|
-
styles.button,
|
|
177
|
-
styles.confirmButton,
|
|
178
|
-
]}>
|
|
179
|
-
{confirmButtonText}
|
|
180
|
-
</Text>
|
|
181
|
-
</TouchableOpacity>
|
|
182
|
-
</View>
|
|
183
|
-
</View>
|
|
184
|
-
</View>
|
|
185
|
-
</Modal>
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
);
|
|
189
|
-
|
|
190
|
-
export default React.memo(TimerPickerModal);
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { StyleSheet } from "react-native";
|
|
2
|
-
import type { TextStyle, ViewStyle } from "react-native";
|
|
3
|
-
|
|
4
|
-
import type { CustomTimerPickerStyles } from "../TimerPicker/styles";
|
|
5
|
-
|
|
6
|
-
export interface CustomTimerPickerModalStyles extends CustomTimerPickerStyles {
|
|
7
|
-
button?: TextStyle;
|
|
8
|
-
buttonContainer?: ViewStyle;
|
|
9
|
-
cancelButton?: TextStyle;
|
|
10
|
-
confirmButton?: TextStyle;
|
|
11
|
-
container?: ViewStyle;
|
|
12
|
-
contentContainer?: ViewStyle;
|
|
13
|
-
modalTitle?: TextStyle;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const DARK_MODE_BACKGROUND_COLOR = "#232323";
|
|
17
|
-
const DARK_MODE_TEXT_COLOR = "#E9E9E9";
|
|
18
|
-
const LIGHT_MODE_BACKGROUND_COLOR = "#F1F1F1";
|
|
19
|
-
const LIGHT_MODE_TEXT_COLOR = "#1B1B1B";
|
|
20
|
-
|
|
21
|
-
export const generateStyles = (
|
|
22
|
-
customStyles: CustomTimerPickerModalStyles | undefined
|
|
23
|
-
) =>
|
|
24
|
-
StyleSheet.create({
|
|
25
|
-
container: {
|
|
26
|
-
justifyContent: "center",
|
|
27
|
-
alignItems: "center",
|
|
28
|
-
overflow: "hidden",
|
|
29
|
-
...customStyles?.container,
|
|
30
|
-
},
|
|
31
|
-
contentContainer: {
|
|
32
|
-
backgroundColor:
|
|
33
|
-
customStyles?.backgroundColor ??
|
|
34
|
-
(customStyles?.theme === "dark"
|
|
35
|
-
? DARK_MODE_BACKGROUND_COLOR
|
|
36
|
-
: LIGHT_MODE_BACKGROUND_COLOR),
|
|
37
|
-
justifyContent: "center",
|
|
38
|
-
alignItems: "center",
|
|
39
|
-
borderRadius: 20,
|
|
40
|
-
padding: 20,
|
|
41
|
-
...customStyles?.contentContainer,
|
|
42
|
-
},
|
|
43
|
-
buttonContainer: {
|
|
44
|
-
flexDirection: "row",
|
|
45
|
-
marginTop: 25,
|
|
46
|
-
...customStyles?.buttonContainer,
|
|
47
|
-
},
|
|
48
|
-
button: {
|
|
49
|
-
marginHorizontal: 12,
|
|
50
|
-
paddingVertical: 10,
|
|
51
|
-
paddingHorizontal: 20,
|
|
52
|
-
borderWidth: 1,
|
|
53
|
-
borderRadius: 10,
|
|
54
|
-
fontSize: 16,
|
|
55
|
-
overflow: "hidden",
|
|
56
|
-
...customStyles?.text,
|
|
57
|
-
...customStyles?.button,
|
|
58
|
-
},
|
|
59
|
-
cancelButton: {
|
|
60
|
-
borderColor: "gray",
|
|
61
|
-
color:
|
|
62
|
-
customStyles?.theme === "dark" ? DARK_MODE_TEXT_COLOR : "gray",
|
|
63
|
-
backgroundColor:
|
|
64
|
-
customStyles?.theme === "dark" ? "gray" : undefined,
|
|
65
|
-
...customStyles?.text,
|
|
66
|
-
...customStyles?.cancelButton,
|
|
67
|
-
},
|
|
68
|
-
confirmButton: {
|
|
69
|
-
borderColor: "green",
|
|
70
|
-
color:
|
|
71
|
-
customStyles?.theme === "dark" ? DARK_MODE_TEXT_COLOR : "green",
|
|
72
|
-
backgroundColor:
|
|
73
|
-
customStyles?.theme === "dark" ? "green" : undefined,
|
|
74
|
-
...customStyles?.text,
|
|
75
|
-
...customStyles?.confirmButton,
|
|
76
|
-
},
|
|
77
|
-
modalTitle: {
|
|
78
|
-
fontSize: 24,
|
|
79
|
-
fontWeight: "600",
|
|
80
|
-
marginBottom: 15,
|
|
81
|
-
color:
|
|
82
|
-
customStyles?.theme === "dark"
|
|
83
|
-
? DARK_MODE_TEXT_COLOR
|
|
84
|
-
: LIGHT_MODE_TEXT_COLOR,
|
|
85
|
-
...customStyles?.text,
|
|
86
|
-
...customStyles?.modalTitle,
|
|
87
|
-
},
|
|
88
|
-
});
|