Skip to content

Commit 903652a

Browse files
authored
Merge pull request #3 from gotnoklu/revert-2-fix/remove-default-props
Revert "Remove default props in `ToastableWrapper` to prevent warning"
2 parents 3deef82 + 2d6f8e3 commit 903652a

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

src/components/ToastableWrapper.tsx

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ import type {
1919
} from '../types';
2020
import { useConstructor } from '../hooks';
2121

22+
const defaultProps: ToastableWrapperProps = {
23+
animationInTiming: 600,
24+
animationOutTiming: 600,
25+
useNativeDriver: false,
26+
isVisible: false,
27+
panResponderThreshold: 4,
28+
swipeThreshold: 100,
29+
onToasterHide: () => null,
30+
onToasterWillHide: () => null,
31+
swipeDirection: ['right', 'left', 'up'],
32+
onSwipeComplete: () => null,
33+
onSwipeStart: () => null,
34+
};
35+
2236
type ToastableWrapperProps = {
2337
animationInTiming: number;
2438
animationOutTiming: number;
@@ -222,21 +236,11 @@ const ANIMATION_MAP: Record<SwipeDirection, AnimatableViewAnimation> = {
222236
};
223237

224238
export const ToastableWrapper = ({
225-
animationInTiming = 600,
226-
animationOutTiming = 600,
227-
isVisible = false,
228-
panResponderThreshold = 4,
229-
swipeThreshold = 100,
230-
onToasterHide = () => null,
231-
onToasterWillHide = () => null,
232-
swipeDirection = ['right', 'left', 'up'],
233-
onSwipeComplete = () => null,
234-
onSwipeStart = () => null,
235239
...props
236240
}: PropsWithChildren<ToastableWrapperProps>) => {
237-
const isSwipeable = !!swipeDirection;
241+
const isSwipeable = !!props.swipeDirection;
238242

239-
const [isShown, setIsShown] = useState(isVisible);
243+
const [isVisible, setIsVisible] = useState(props.isVisible);
240244
const pan = useRef(new Animated.ValueXY()).current;
241245
const contentRef = useRef<AnimatableViewRef>(null);
242246

@@ -260,15 +264,15 @@ export const ToastableWrapper = ({
260264
if (interactionHandle == null) {
261265
interactionHandle = InteractionManager.createInteractionHandle();
262266
}
263-
content.animate('slideInDown', animationInTiming).then(() => {
267+
content.animate('slideInDown', props.animationInTiming).then(() => {
264268
isTransitioning = false;
265269

266270
if (interactionHandle) {
267271
InteractionManager.clearInteractionHandle(interactionHandle);
268272
interactionHandle = null;
269273
}
270274

271-
if (!isVisible) {
275+
if (!props.isVisible) {
272276
close();
273277
}
274278
});
@@ -287,14 +291,14 @@ export const ToastableWrapper = ({
287291
return;
288292
}
289293

290-
onToasterWillHide?.();
294+
props.onToasterWillHide?.();
291295

292296
if (interactionHandle == null) {
293297
interactionHandle = InteractionManager.createInteractionHandle();
294298
}
295299

296300
content
297-
.animate(ANIMATION_MAP[currentSwipingDirection], animationOutTiming)
301+
.animate(ANIMATION_MAP[currentSwipingDirection], props.animationOutTiming)
298302
.then(() => {
299303
isTransitioning = false;
300304

@@ -303,9 +307,9 @@ export const ToastableWrapper = ({
303307
interactionHandle = null;
304308
}
305309

306-
if (!isVisible) {
307-
setIsShown(false);
308-
onToasterHide();
310+
if (!props.isVisible) {
311+
setIsVisible(false);
312+
props.onToasterHide();
309313
return;
310314
}
311315

@@ -315,21 +319,21 @@ export const ToastableWrapper = ({
315319

316320
useConstructor(() => {
317321
buildPanResponder({
318-
onSwipeComplete: onSwipeComplete,
319-
onSwipeStart: onSwipeStart,
322+
onSwipeComplete: props.onSwipeComplete,
323+
onSwipeStart: props.onSwipeStart,
320324
pan,
321-
panResponderThreshold: panResponderThreshold,
322-
swipeDirection: swipeDirection,
323-
swipeThreshold: swipeThreshold,
325+
panResponderThreshold: props.panResponderThreshold,
326+
swipeDirection: props.swipeDirection,
327+
swipeThreshold: props.swipeThreshold,
324328
});
325329
});
326330

327331
useEffect(() => {
328-
if (isShown) {
332+
if (isVisible) {
329333
open();
330334
}
331335
// eslint-disable-next-line react-hooks/exhaustive-deps
332-
}, [isShown]);
336+
}, [isVisible]);
333337

334338
useEffect(() => {
335339
return () => {
@@ -345,16 +349,12 @@ export const ToastableWrapper = ({
345349
useEffect(() => {
346350
const wasVisible = prevIsVisibleRef.current;
347351

348-
if (isVisible !== wasVisible) {
349-
if (isVisible) {
350-
open();
351-
} else {
352-
close();
353-
}
354-
prevIsVisibleRef.current = isVisible;
352+
if (props.isVisible !== wasVisible) {
353+
props.isVisible ? open() : close();
354+
prevIsVisibleRef.current = props.isVisible;
355355
}
356356
// eslint-disable-next-line react-hooks/exhaustive-deps
357-
}, [isVisible]);
357+
}, [props.isVisible]);
358358

359359
// FIXME: this should be placed in the render method, but it causes a bug
360360
// if (!isVisible) {
@@ -375,6 +375,8 @@ export const ToastableWrapper = ({
375375
);
376376
};
377377

378+
ToastableWrapper.defaultProps = defaultProps;
379+
378380
const styles = StyleSheet.create({
379381
container: {
380382
...StyleSheet.absoluteFillObject,

0 commit comments

Comments
 (0)