@terreno/ui 0.0.16 → 0.0.18
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/dist/Button.js +7 -9
- package/dist/Button.js.map +1 -1
- package/dist/Common.d.ts +39 -0
- package/dist/TerrenoProvider.js +1 -1
- package/dist/TerrenoProvider.js.map +1 -1
- package/dist/Toast.js +2 -3
- package/dist/Toast.js.map +1 -1
- package/dist/ToastNotifications.d.ts +144 -0
- package/dist/ToastNotifications.js +387 -0
- package/dist/ToastNotifications.js.map +1 -0
- package/dist/UserInactivity.d.ts +28 -0
- package/dist/UserInactivity.js +100 -0
- package/dist/UserInactivity.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -2
- package/src/Button.tsx +20 -37
- package/src/Common.ts +45 -0
- package/src/DateTimeActionSheet.test.tsx +53 -4
- package/src/MobileAddressAutoComplete.test.tsx +47 -4
- package/src/ModalSheet.test.tsx +37 -5
- package/src/PickerSelect.test.tsx +41 -5
- package/src/Signature.test.tsx +21 -4
- package/src/SignatureField.test.tsx +49 -5
- package/src/SplitPage.test.tsx +71 -4
- package/src/TerrenoProvider.tsx +1 -1
- package/src/Toast.tsx +2 -3
- package/src/ToastNotifications.test.tsx +645 -0
- package/src/ToastNotifications.tsx +746 -0
- package/src/UnifiedAddressAutoComplete.test.tsx +43 -5
- package/src/UserInactivity.test.tsx +96 -0
- package/src/UserInactivity.tsx +129 -0
- package/src/WebAddressAutocomplete.test.tsx +22 -4
- package/src/__snapshots__/Button.test.tsx.snap +0 -347
- package/src/__snapshots__/DateTimeActionSheet.test.tsx.snap +11 -0
- package/src/__snapshots__/MobileAddressAutoComplete.test.tsx.snap +230 -0
- package/src/__snapshots__/ModalSheet.test.tsx.snap +37 -0
- package/src/__snapshots__/PickerSelect.test.tsx.snap +798 -11
- package/src/__snapshots__/Signature.test.tsx.snap +67 -0
- package/src/__snapshots__/SignatureField.test.tsx.snap +129 -21
- package/src/__snapshots__/SplitPage.test.tsx.snap +686 -0
- package/src/__snapshots__/UnifiedAddressAutoComplete.test.tsx.snap +377 -0
- package/src/__snapshots__/UserInactivity.test.tsx.snap +108 -0
- package/src/__snapshots__/WebAddressAutocomplete.test.tsx.snap +238 -0
- package/src/index.tsx +1 -0
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vendored from react-native-toast-notifications v3.4.0
|
|
3
|
+
* https://github.com/arnnis/react-native-toast-notifications
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2020 Alireza Rezania
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
* in the Software without restriction, including without limitation the rights
|
|
12
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
* furnished to do so, subject to the following conditions:
|
|
15
|
+
*
|
|
16
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
* copies or substantial portions of the Software.
|
|
18
|
+
*
|
|
19
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
* SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import React, {
|
|
29
|
+
createContext,
|
|
30
|
+
type FC,
|
|
31
|
+
forwardRef,
|
|
32
|
+
type ReactElement,
|
|
33
|
+
useCallback,
|
|
34
|
+
useContext,
|
|
35
|
+
useEffect,
|
|
36
|
+
useImperativeHandle,
|
|
37
|
+
useMemo,
|
|
38
|
+
useRef,
|
|
39
|
+
useState,
|
|
40
|
+
} from "react";
|
|
41
|
+
import {
|
|
42
|
+
Animated,
|
|
43
|
+
Dimensions,
|
|
44
|
+
KeyboardAvoidingView,
|
|
45
|
+
PanResponder,
|
|
46
|
+
type PanResponderGestureState,
|
|
47
|
+
type PanResponderInstance,
|
|
48
|
+
Platform,
|
|
49
|
+
SafeAreaView,
|
|
50
|
+
type ScaledSize,
|
|
51
|
+
type StyleProp,
|
|
52
|
+
StyleSheet,
|
|
53
|
+
Text,
|
|
54
|
+
type TextStyle,
|
|
55
|
+
TouchableWithoutFeedback,
|
|
56
|
+
View,
|
|
57
|
+
type ViewStyle,
|
|
58
|
+
} from "react-native";
|
|
59
|
+
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// useDimensions hook
|
|
62
|
+
// ============================================================================
|
|
63
|
+
|
|
64
|
+
function useDimensions() {
|
|
65
|
+
const [dimensions, setDimensions] = useState(Dimensions.get("window"));
|
|
66
|
+
|
|
67
|
+
const onChange = useCallback(({window}: {window: ScaledSize}) => {
|
|
68
|
+
setDimensions(window);
|
|
69
|
+
}, []);
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const subscription = Dimensions.addEventListener("change", onChange);
|
|
73
|
+
|
|
74
|
+
return () => {
|
|
75
|
+
subscription.remove();
|
|
76
|
+
};
|
|
77
|
+
}, [onChange]);
|
|
78
|
+
|
|
79
|
+
return dimensions;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ============================================================================
|
|
83
|
+
// Toast Options and Props
|
|
84
|
+
// ============================================================================
|
|
85
|
+
|
|
86
|
+
export interface ToastOptions {
|
|
87
|
+
/**
|
|
88
|
+
* Id is optional, you may provide an id only if you want to update toast later using toast.update()
|
|
89
|
+
*/
|
|
90
|
+
id?: string;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Customize toast icon
|
|
94
|
+
*/
|
|
95
|
+
icon?: ReactElement;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Toast types, You can implement your custom types with JSX using renderType method on ToastContainer.
|
|
99
|
+
*/
|
|
100
|
+
type?: "normal" | "success" | "danger" | "warning" | string;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* In ms, How long toast will stay before it go away
|
|
104
|
+
*/
|
|
105
|
+
duration?: number;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Customize when toast should be placed
|
|
109
|
+
*/
|
|
110
|
+
placement?: "top" | "bottom" | "center";
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Customize style of toast
|
|
114
|
+
*/
|
|
115
|
+
style?: StyleProp<ViewStyle>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Customize style of toast text
|
|
119
|
+
*/
|
|
120
|
+
textStyle?: StyleProp<TextStyle>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Customize how fast toast will show and hide
|
|
124
|
+
*/
|
|
125
|
+
animationDuration?: number;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Customize how toast is animated when added or removed
|
|
129
|
+
*/
|
|
130
|
+
animationType?: "slide-in" | "zoom-in";
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Customize success type icon
|
|
134
|
+
*/
|
|
135
|
+
successIcon?: ReactElement;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Customize danger type icon
|
|
139
|
+
*/
|
|
140
|
+
dangerIcon?: ReactElement;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Customize warning type icon
|
|
144
|
+
*/
|
|
145
|
+
warningIcon?: ReactElement;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Customize success type color. changes toast background color
|
|
149
|
+
*/
|
|
150
|
+
successColor?: string;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Customize danger type color. changes toast background color
|
|
154
|
+
*/
|
|
155
|
+
dangerColor?: string;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Customize warning type color. changes toast background color
|
|
159
|
+
*/
|
|
160
|
+
warningColor?: string;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Customize normal type color. changes toast background color
|
|
164
|
+
*/
|
|
165
|
+
normalColor?: string;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Register event for when toast is pressed. If you're using a custom toast you have to pass this to a Touchable.
|
|
169
|
+
*/
|
|
170
|
+
onPress?(id: string): void;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Execute event after toast is closed
|
|
174
|
+
*/
|
|
175
|
+
onClose?(): void;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Payload data for custom toasts. You can pass whatever you want
|
|
179
|
+
*/
|
|
180
|
+
data?: any;
|
|
181
|
+
|
|
182
|
+
swipeEnabled?: boolean;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface ToastProps extends ToastOptions {
|
|
186
|
+
id: string;
|
|
187
|
+
onDestroy(): void;
|
|
188
|
+
message: string | ReactElement;
|
|
189
|
+
open: boolean;
|
|
190
|
+
renderToast?(toast: ToastProps): ReactElement;
|
|
191
|
+
renderType?: {[type: string]: (toast: ToastProps) => ReactElement};
|
|
192
|
+
onHide(): void;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ============================================================================
|
|
196
|
+
// Toast Component
|
|
197
|
+
// ============================================================================
|
|
198
|
+
|
|
199
|
+
const ToastItem: FC<ToastProps> = (props) => {
|
|
200
|
+
let {
|
|
201
|
+
id,
|
|
202
|
+
onDestroy,
|
|
203
|
+
icon,
|
|
204
|
+
type = "normal",
|
|
205
|
+
message,
|
|
206
|
+
duration = 5000,
|
|
207
|
+
style,
|
|
208
|
+
textStyle,
|
|
209
|
+
animationDuration = 250,
|
|
210
|
+
animationType = "slide-in",
|
|
211
|
+
successIcon,
|
|
212
|
+
dangerIcon,
|
|
213
|
+
warningIcon,
|
|
214
|
+
successColor,
|
|
215
|
+
dangerColor,
|
|
216
|
+
warningColor,
|
|
217
|
+
normalColor,
|
|
218
|
+
placement,
|
|
219
|
+
swipeEnabled,
|
|
220
|
+
onPress,
|
|
221
|
+
} = props;
|
|
222
|
+
|
|
223
|
+
const containerRef = useRef<View>(null);
|
|
224
|
+
const [animation] = useState(new Animated.Value(0));
|
|
225
|
+
const panResponderRef = useRef<PanResponderInstance | undefined>(undefined);
|
|
226
|
+
const panResponderAnimRef = useRef<Animated.ValueXY | undefined>(undefined);
|
|
227
|
+
const closeTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
228
|
+
const dims = useDimensions();
|
|
229
|
+
|
|
230
|
+
const handleClose = useCallback(() => {
|
|
231
|
+
Animated.timing(animation, {
|
|
232
|
+
duration: animationDuration,
|
|
233
|
+
toValue: 0,
|
|
234
|
+
useNativeDriver: Platform.OS !== "web",
|
|
235
|
+
}).start(() => onDestroy());
|
|
236
|
+
}, [animation, animationDuration, onDestroy]);
|
|
237
|
+
|
|
238
|
+
useEffect(() => {
|
|
239
|
+
Animated.timing(animation, {
|
|
240
|
+
duration: animationDuration,
|
|
241
|
+
toValue: 1,
|
|
242
|
+
useNativeDriver: Platform.OS !== "web",
|
|
243
|
+
}).start();
|
|
244
|
+
if (duration !== 0 && typeof duration === "number") {
|
|
245
|
+
closeTimeoutRef.current = setTimeout(() => {
|
|
246
|
+
handleClose();
|
|
247
|
+
}, duration);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return () => {
|
|
251
|
+
closeTimeoutRef.current && clearTimeout(closeTimeoutRef.current);
|
|
252
|
+
};
|
|
253
|
+
}, [duration, animation, animationDuration, handleClose]);
|
|
254
|
+
|
|
255
|
+
// Handles hide & hideAll
|
|
256
|
+
useEffect(() => {
|
|
257
|
+
if (!props.open) {
|
|
258
|
+
// Unregister close timeout
|
|
259
|
+
closeTimeoutRef.current && clearTimeout(closeTimeoutRef.current);
|
|
260
|
+
|
|
261
|
+
// Close animation them remove from stack.
|
|
262
|
+
handleClose();
|
|
263
|
+
}
|
|
264
|
+
}, [
|
|
265
|
+
props.open, // Close animation them remove from stack.
|
|
266
|
+
handleClose,
|
|
267
|
+
]);
|
|
268
|
+
|
|
269
|
+
const panReleaseToLeft = (gestureState: PanResponderGestureState) => {
|
|
270
|
+
Animated.timing(getPanResponderAnim(), {
|
|
271
|
+
duration: 250,
|
|
272
|
+
toValue: {x: (-dims.width / 10) * 9, y: gestureState.dy},
|
|
273
|
+
useNativeDriver: Platform.OS !== "web",
|
|
274
|
+
}).start(() => onDestroy());
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const panReleaseToRight = (gestureState: PanResponderGestureState) => {
|
|
278
|
+
Animated.timing(getPanResponderAnim(), {
|
|
279
|
+
duration: 250,
|
|
280
|
+
toValue: {x: (dims.width / 10) * 9, y: gestureState.dy},
|
|
281
|
+
useNativeDriver: Platform.OS !== "web",
|
|
282
|
+
}).start(() => onDestroy());
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const getPanResponder = () => {
|
|
286
|
+
if (panResponderRef.current) return panResponderRef.current;
|
|
287
|
+
const swipeThreshold = Platform.OS === "android" ? 10 : 0;
|
|
288
|
+
panResponderRef.current = PanResponder.create({
|
|
289
|
+
onMoveShouldSetPanResponder: (_, gestureState) => {
|
|
290
|
+
//return true if user is swiping, return false if it's a single click
|
|
291
|
+
return (
|
|
292
|
+
Math.abs(gestureState.dx) > swipeThreshold || Math.abs(gestureState.dy) > swipeThreshold
|
|
293
|
+
);
|
|
294
|
+
},
|
|
295
|
+
onPanResponderMove: (_, gestureState) => {
|
|
296
|
+
getPanResponderAnim()?.setValue({
|
|
297
|
+
x: gestureState.dx,
|
|
298
|
+
y: gestureState.dy,
|
|
299
|
+
});
|
|
300
|
+
},
|
|
301
|
+
onPanResponderRelease: (_, gestureState) => {
|
|
302
|
+
if (gestureState.dx > 50) {
|
|
303
|
+
panReleaseToRight(gestureState);
|
|
304
|
+
} else if (gestureState.dx < -50) {
|
|
305
|
+
panReleaseToLeft(gestureState);
|
|
306
|
+
} else {
|
|
307
|
+
Animated.spring(getPanResponderAnim(), {
|
|
308
|
+
toValue: {x: 0, y: 0},
|
|
309
|
+
useNativeDriver: Platform.OS !== "web",
|
|
310
|
+
}).start();
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
return panResponderRef.current;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const getPanResponderAnim = () => {
|
|
318
|
+
if (panResponderAnimRef.current) return panResponderAnimRef.current;
|
|
319
|
+
panResponderAnimRef.current = new Animated.ValueXY({x: 0, y: 0});
|
|
320
|
+
return panResponderAnimRef.current;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
if (icon === undefined) {
|
|
324
|
+
switch (type) {
|
|
325
|
+
case "success": {
|
|
326
|
+
if (successIcon) {
|
|
327
|
+
icon = successIcon;
|
|
328
|
+
}
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
case "danger": {
|
|
333
|
+
if (dangerIcon) {
|
|
334
|
+
icon = dangerIcon;
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
case "warning": {
|
|
339
|
+
if (warningIcon) {
|
|
340
|
+
icon = warningIcon;
|
|
341
|
+
}
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
let backgroundColor = "";
|
|
348
|
+
switch (type) {
|
|
349
|
+
case "success":
|
|
350
|
+
backgroundColor = successColor || "rgb(46, 125, 50)";
|
|
351
|
+
break;
|
|
352
|
+
case "danger":
|
|
353
|
+
backgroundColor = dangerColor || "rgb(211, 47, 47)";
|
|
354
|
+
break;
|
|
355
|
+
case "warning":
|
|
356
|
+
backgroundColor = warningColor || "rgb(237, 108, 2)";
|
|
357
|
+
break;
|
|
358
|
+
default:
|
|
359
|
+
backgroundColor = normalColor || "#333";
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Build transform array with proper typing for React Native Animated
|
|
363
|
+
type TransformItem =
|
|
364
|
+
| {translateY?: Animated.AnimatedInterpolation<number>}
|
|
365
|
+
| {translateX?: Animated.Value}
|
|
366
|
+
| {scale?: Animated.AnimatedInterpolation<number>};
|
|
367
|
+
|
|
368
|
+
const baseTransform: TransformItem[] = [
|
|
369
|
+
{
|
|
370
|
+
translateY: animation.interpolate({
|
|
371
|
+
inputRange: [0, 1],
|
|
372
|
+
outputRange: placement === "bottom" ? [20, 0] : [-20, 0],
|
|
373
|
+
}),
|
|
374
|
+
},
|
|
375
|
+
];
|
|
376
|
+
|
|
377
|
+
if (swipeEnabled) {
|
|
378
|
+
const panTransform = getPanResponderAnim().getTranslateTransform()[0];
|
|
379
|
+
baseTransform.push(panTransform as TransformItem);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (animationType === "zoom-in") {
|
|
383
|
+
baseTransform.push({
|
|
384
|
+
scale: animation.interpolate({
|
|
385
|
+
inputRange: [0, 1],
|
|
386
|
+
outputRange: [0.7, 1],
|
|
387
|
+
}),
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const animationStyle = {
|
|
392
|
+
opacity: animation,
|
|
393
|
+
transform: baseTransform as Animated.WithAnimatedObject<ViewStyle>["transform"],
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
return (
|
|
397
|
+
<Animated.View
|
|
398
|
+
pointerEvents={"box-none"}
|
|
399
|
+
ref={containerRef}
|
|
400
|
+
{...(swipeEnabled ? getPanResponder().panHandlers : null)}
|
|
401
|
+
style={[toastStyles.container, animationStyle]}
|
|
402
|
+
>
|
|
403
|
+
{props.renderType?.[type] ? (
|
|
404
|
+
props.renderType[type](props)
|
|
405
|
+
) : props.renderToast ? (
|
|
406
|
+
props.renderToast(props)
|
|
407
|
+
) : (
|
|
408
|
+
<TouchableWithoutFeedback disabled={!onPress} onPress={() => onPress?.(id)}>
|
|
409
|
+
<View
|
|
410
|
+
style={[
|
|
411
|
+
toastStyles.toastContainer,
|
|
412
|
+
{backgroundColor, maxWidth: (dims.width / 10) * 9},
|
|
413
|
+
style,
|
|
414
|
+
]}
|
|
415
|
+
>
|
|
416
|
+
{icon ? <View style={toastStyles.iconContainer}>{icon}</View> : null}
|
|
417
|
+
{React.isValidElement(message) ? (
|
|
418
|
+
message
|
|
419
|
+
) : (
|
|
420
|
+
<Text style={[toastStyles.message, textStyle]}>{message}</Text>
|
|
421
|
+
)}
|
|
422
|
+
</View>
|
|
423
|
+
</TouchableWithoutFeedback>
|
|
424
|
+
)}
|
|
425
|
+
</Animated.View>
|
|
426
|
+
);
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
const toastStyles = StyleSheet.create({
|
|
430
|
+
container: {alignItems: "center", width: "100%"},
|
|
431
|
+
iconContainer: {
|
|
432
|
+
marginRight: 5,
|
|
433
|
+
},
|
|
434
|
+
message: {
|
|
435
|
+
color: "#fff",
|
|
436
|
+
fontWeight: "500",
|
|
437
|
+
},
|
|
438
|
+
toastContainer: {
|
|
439
|
+
alignItems: "center",
|
|
440
|
+
borderRadius: 5,
|
|
441
|
+
flexDirection: "row",
|
|
442
|
+
marginVertical: 5,
|
|
443
|
+
overflow: "hidden",
|
|
444
|
+
paddingHorizontal: 12,
|
|
445
|
+
paddingVertical: 12,
|
|
446
|
+
},
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
// ============================================================================
|
|
450
|
+
// Toast Container
|
|
451
|
+
// ============================================================================
|
|
452
|
+
|
|
453
|
+
const {height, width} = Dimensions.get("window");
|
|
454
|
+
|
|
455
|
+
export interface ToastContainerProps extends ToastOptions {
|
|
456
|
+
renderToast?(toast: ToastProps): ReactElement;
|
|
457
|
+
renderType?: {[type: string]: (toast: ToastProps) => ReactElement};
|
|
458
|
+
offset?: number;
|
|
459
|
+
offsetTop?: number;
|
|
460
|
+
offsetBottom?: number;
|
|
461
|
+
swipeEnabled?: boolean;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export interface ToastContainerRef {
|
|
465
|
+
show: (message: string | ReactElement, toastOptions?: ToastOptions) => string;
|
|
466
|
+
update: (id: string, message: string | ReactElement, toastOptions?: ToastOptions) => void;
|
|
467
|
+
hide: (id: string) => void;
|
|
468
|
+
hideAll: () => void;
|
|
469
|
+
isOpen: (id: string) => boolean;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const ToastContainer = forwardRef<ToastContainerRef, ToastContainerProps>((props, ref) => {
|
|
473
|
+
const {
|
|
474
|
+
offset = 10,
|
|
475
|
+
offsetTop,
|
|
476
|
+
offsetBottom,
|
|
477
|
+
placement = "bottom",
|
|
478
|
+
swipeEnabled = true,
|
|
479
|
+
renderToast,
|
|
480
|
+
renderType,
|
|
481
|
+
duration,
|
|
482
|
+
type,
|
|
483
|
+
style,
|
|
484
|
+
textStyle,
|
|
485
|
+
animationDuration,
|
|
486
|
+
animationType,
|
|
487
|
+
successIcon,
|
|
488
|
+
dangerIcon,
|
|
489
|
+
warningIcon,
|
|
490
|
+
successColor,
|
|
491
|
+
dangerColor,
|
|
492
|
+
warningColor,
|
|
493
|
+
normalColor,
|
|
494
|
+
onPress,
|
|
495
|
+
data,
|
|
496
|
+
} = props;
|
|
497
|
+
|
|
498
|
+
const toastDefaults = useMemo(
|
|
499
|
+
() => ({
|
|
500
|
+
animationDuration,
|
|
501
|
+
animationType,
|
|
502
|
+
dangerColor,
|
|
503
|
+
dangerIcon,
|
|
504
|
+
data,
|
|
505
|
+
duration,
|
|
506
|
+
normalColor,
|
|
507
|
+
onPress,
|
|
508
|
+
renderToast,
|
|
509
|
+
renderType,
|
|
510
|
+
style,
|
|
511
|
+
successColor,
|
|
512
|
+
successIcon,
|
|
513
|
+
textStyle,
|
|
514
|
+
type,
|
|
515
|
+
warningColor,
|
|
516
|
+
warningIcon,
|
|
517
|
+
}),
|
|
518
|
+
[
|
|
519
|
+
renderToast,
|
|
520
|
+
renderType,
|
|
521
|
+
duration,
|
|
522
|
+
type,
|
|
523
|
+
style,
|
|
524
|
+
textStyle,
|
|
525
|
+
animationDuration,
|
|
526
|
+
animationType,
|
|
527
|
+
successIcon,
|
|
528
|
+
dangerIcon,
|
|
529
|
+
warningIcon,
|
|
530
|
+
successColor,
|
|
531
|
+
dangerColor,
|
|
532
|
+
warningColor,
|
|
533
|
+
normalColor,
|
|
534
|
+
onPress,
|
|
535
|
+
data,
|
|
536
|
+
]
|
|
537
|
+
);
|
|
538
|
+
|
|
539
|
+
const [toasts, setToasts] = useState<Array<ToastProps>>([]);
|
|
540
|
+
|
|
541
|
+
const hide = useCallback((id: string) => {
|
|
542
|
+
setToasts((prev) => prev.map((t) => (t.id === id ? {...t, open: false} : t)));
|
|
543
|
+
}, []);
|
|
544
|
+
|
|
545
|
+
const show = useCallback(
|
|
546
|
+
(message: string | ReactElement, toastOptions?: ToastOptions) => {
|
|
547
|
+
const id = toastOptions?.id || Math.random().toString();
|
|
548
|
+
const onDestroy = () => {
|
|
549
|
+
toastOptions?.onClose?.();
|
|
550
|
+
setToasts((prev) => prev.filter((t) => t.id !== id));
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
requestAnimationFrame(() => {
|
|
554
|
+
setToasts((prev) => [
|
|
555
|
+
{
|
|
556
|
+
id,
|
|
557
|
+
message,
|
|
558
|
+
onDestroy,
|
|
559
|
+
onHide: () => hide(id),
|
|
560
|
+
open: true,
|
|
561
|
+
placement,
|
|
562
|
+
swipeEnabled,
|
|
563
|
+
...toastDefaults,
|
|
564
|
+
...toastOptions,
|
|
565
|
+
},
|
|
566
|
+
...prev.filter((t) => t.open),
|
|
567
|
+
]);
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
return id;
|
|
571
|
+
},
|
|
572
|
+
[hide, placement, swipeEnabled, toastDefaults]
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
const update = useCallback(
|
|
576
|
+
(id: string, message: string | ReactElement, toastOptions?: ToastOptions) => {
|
|
577
|
+
setToasts((prev) =>
|
|
578
|
+
prev.map((toast) => (toast.id === id ? {...toast, message, ...toastOptions} : toast))
|
|
579
|
+
);
|
|
580
|
+
},
|
|
581
|
+
[]
|
|
582
|
+
);
|
|
583
|
+
|
|
584
|
+
const hideAll = useCallback(() => {
|
|
585
|
+
setToasts((prev) => prev.map((t) => ({...t, open: false})));
|
|
586
|
+
}, []);
|
|
587
|
+
|
|
588
|
+
const isOpen = useCallback(
|
|
589
|
+
(id: string) => {
|
|
590
|
+
return toasts.some((t) => t.id === id && t.open);
|
|
591
|
+
},
|
|
592
|
+
[toasts]
|
|
593
|
+
);
|
|
594
|
+
|
|
595
|
+
useImperativeHandle(
|
|
596
|
+
ref,
|
|
597
|
+
() => ({
|
|
598
|
+
hide,
|
|
599
|
+
hideAll,
|
|
600
|
+
isOpen,
|
|
601
|
+
show,
|
|
602
|
+
update,
|
|
603
|
+
}),
|
|
604
|
+
[show, update, hide, hideAll, isOpen]
|
|
605
|
+
);
|
|
606
|
+
|
|
607
|
+
const renderBottomToasts = useCallback(() => {
|
|
608
|
+
const style: ViewStyle = {
|
|
609
|
+
bottom: offsetBottom || offset,
|
|
610
|
+
flexDirection: "column",
|
|
611
|
+
justifyContent: "flex-end",
|
|
612
|
+
width: width,
|
|
613
|
+
};
|
|
614
|
+
return (
|
|
615
|
+
<KeyboardAvoidingView
|
|
616
|
+
behavior={Platform.OS === "ios" ? "position" : undefined}
|
|
617
|
+
pointerEvents="box-none"
|
|
618
|
+
style={[containerStyles.container, style]}
|
|
619
|
+
>
|
|
620
|
+
<SafeAreaView>
|
|
621
|
+
{toasts
|
|
622
|
+
.filter((t) => !t.placement || t.placement === "bottom")
|
|
623
|
+
.map((toast) => (
|
|
624
|
+
<ToastItem key={toast.id} {...toast} />
|
|
625
|
+
))}
|
|
626
|
+
</SafeAreaView>
|
|
627
|
+
</KeyboardAvoidingView>
|
|
628
|
+
);
|
|
629
|
+
}, [toasts, offset, offsetBottom]);
|
|
630
|
+
|
|
631
|
+
const renderTopToasts = useCallback(() => {
|
|
632
|
+
const style: ViewStyle = {
|
|
633
|
+
flexDirection: "column-reverse",
|
|
634
|
+
justifyContent: "flex-start",
|
|
635
|
+
top: offsetTop || offset,
|
|
636
|
+
width: width,
|
|
637
|
+
};
|
|
638
|
+
return (
|
|
639
|
+
<KeyboardAvoidingView
|
|
640
|
+
behavior={Platform.OS === "ios" ? "position" : undefined}
|
|
641
|
+
pointerEvents="box-none"
|
|
642
|
+
style={[containerStyles.container, style]}
|
|
643
|
+
>
|
|
644
|
+
<SafeAreaView>
|
|
645
|
+
{toasts
|
|
646
|
+
.filter((t) => t.placement === "top")
|
|
647
|
+
.map((toast) => (
|
|
648
|
+
<ToastItem key={toast.id} {...toast} />
|
|
649
|
+
))}
|
|
650
|
+
</SafeAreaView>
|
|
651
|
+
</KeyboardAvoidingView>
|
|
652
|
+
);
|
|
653
|
+
}, [toasts, offset, offsetTop]);
|
|
654
|
+
|
|
655
|
+
const renderCenterToasts = useCallback(() => {
|
|
656
|
+
const style: ViewStyle = {
|
|
657
|
+
flexDirection: "column-reverse",
|
|
658
|
+
height: height,
|
|
659
|
+
justifyContent: "center",
|
|
660
|
+
top: offsetTop || offset,
|
|
661
|
+
width: width,
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
const data = toasts.filter((t) => t.placement === "center");
|
|
665
|
+
const foundToast = data.length > 0;
|
|
666
|
+
|
|
667
|
+
if (!foundToast) return null;
|
|
668
|
+
|
|
669
|
+
return (
|
|
670
|
+
<KeyboardAvoidingView
|
|
671
|
+
behavior={Platform.OS === "ios" ? "position" : undefined}
|
|
672
|
+
pointerEvents="box-none"
|
|
673
|
+
style={[containerStyles.container, style]}
|
|
674
|
+
>
|
|
675
|
+
{toasts
|
|
676
|
+
.filter((t) => t.placement === "center")
|
|
677
|
+
.map((toast) => (
|
|
678
|
+
<ToastItem key={toast.id} {...toast} />
|
|
679
|
+
))}
|
|
680
|
+
</KeyboardAvoidingView>
|
|
681
|
+
);
|
|
682
|
+
}, [toasts, offset, offsetTop]);
|
|
683
|
+
|
|
684
|
+
return (
|
|
685
|
+
<>
|
|
686
|
+
{renderTopToasts()}
|
|
687
|
+
{renderBottomToasts()}
|
|
688
|
+
{renderCenterToasts()}
|
|
689
|
+
</>
|
|
690
|
+
);
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
ToastContainer.displayName = "ToastContainer";
|
|
694
|
+
|
|
695
|
+
const containerStyles = StyleSheet.create({
|
|
696
|
+
container: {
|
|
697
|
+
alignSelf: "center",
|
|
698
|
+
elevation: 999999,
|
|
699
|
+
flex: 0,
|
|
700
|
+
maxWidth: "100%",
|
|
701
|
+
// @ts-expect-error: fixed is available on web.
|
|
702
|
+
position: Platform.OS === "web" ? "fixed" : "absolute",
|
|
703
|
+
zIndex: 999999,
|
|
704
|
+
...(Platform.OS === "web" ? {overflow: "hidden", userSelect: "none"} : null),
|
|
705
|
+
},
|
|
706
|
+
message: {
|
|
707
|
+
color: "#333",
|
|
708
|
+
},
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
// ============================================================================
|
|
712
|
+
// Toast Context and Hook
|
|
713
|
+
// ============================================================================
|
|
714
|
+
|
|
715
|
+
export type ToastType = ToastContainerRef;
|
|
716
|
+
|
|
717
|
+
const ToastContext = createContext<ToastType>({} as ToastType);
|
|
718
|
+
|
|
719
|
+
export let GlobalToast: ToastType;
|
|
720
|
+
|
|
721
|
+
type ToastProviderProps = ToastContainerProps & {
|
|
722
|
+
children: React.ReactNode;
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
export const ToastProvider: FC<ToastProviderProps> = ({children, ...props}) => {
|
|
726
|
+
const toastRef = useRef<ToastContainerRef>(null);
|
|
727
|
+
const [refState, setRefState] = useState<ToastType>({} as ToastType);
|
|
728
|
+
|
|
729
|
+
useEffect(() => {
|
|
730
|
+
if (toastRef.current) {
|
|
731
|
+
setRefState(toastRef.current);
|
|
732
|
+
GlobalToast = toastRef.current;
|
|
733
|
+
}
|
|
734
|
+
}, []);
|
|
735
|
+
|
|
736
|
+
return (
|
|
737
|
+
<ToastContext.Provider value={refState}>
|
|
738
|
+
{children}
|
|
739
|
+
<ToastContainer ref={toastRef} {...props} />
|
|
740
|
+
</ToastContext.Provider>
|
|
741
|
+
);
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
export const useToastNotifications = (): ToastType => useContext(ToastContext);
|
|
745
|
+
|
|
746
|
+
export default ToastContainer;
|