From 835f008221dc9ebf559a910013135dd79a170875 Mon Sep 17 00:00:00 2001 From: Wayne <75321423+WayneKim92@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:20:52 +0900 Subject: [PATCH] feat: show or hide by visible prop (#8) --- example/src/App.tsx | 71 +++++++++++++++++++++++++++++++++++++-------- src/index.tsx | 29 ++++++++---------- 2 files changed, 71 insertions(+), 29 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index e14c119..473bb21 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -3,7 +3,13 @@ import Tooltip from 'react-native-good-tooltip'; import { useState } from 'react'; export default function App() { - const [inputText, setInputText] = useState('초기 텍스트'); + // Example - TextInput + const [inputText, setInputText] = useState(''); + const [tooltipMessage, setTooltipMessage] = useState('Hello'); + const [isFocused, setIsFocused] = useState(false); + + // Example - FlatList + // const data = [1, 2, 3]; return ( @@ -66,27 +72,68 @@ export default function App() { - + { + setInputText(text); + if (text.toLowerCase() === 'world') { + setTooltipMessage('Correct!'); + setTimeout(() => { + setIsFocused(false); + }, 1500); + } + }} + style={{ padding: 16, backgroundColor: 'gray', color: 'white' }} + onFocus={() => setIsFocused(true)} + onBlur={() => { + setIsFocused(false); + }} /> {/* {*/} + {/* data={data}*/} + {/* keyExtractor={(item) => item.toString()}*/} + {/* CellRendererComponent={({ index, style, ...props }) => {*/} {/* return (*/} - {/* */} {/* );*/} {/* }}*/} + {/* renderItem={() => {*/} + {/* console.log('####################################');*/} + {/* console.log('리렌더링?');*/} + {/* console.log('####################################');*/} + {/* return (*/} + {/* */} + {/* */} + {/* */} + {/* );*/} + {/* }}*/} {/*/>*/} ); diff --git a/src/index.tsx b/src/index.tsx index da128c3..bc3a300 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,10 +1,4 @@ -import React, { - useCallback, - useEffect, - useMemo, - useRef, - useState, -} from 'react'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { Animated, Dimensions, @@ -65,6 +59,7 @@ interface ToolTipProps { onVisibleChange?: (isVisible: boolean) => void; delayShowTime?: number; autoHideTime?: number; + disableAutoHide?: boolean; } const Tooltip = ({ @@ -84,9 +79,8 @@ const Tooltip = ({ onVisibleChange, delayShowTime = 0, autoHideTime = 5000, + disableAutoHide, }: ToolTipProps) => { - const showAnimationRef = useRef(false); - const hideAnimationRef = useRef(false); const animatedValue = useMemo(() => new Animated.Value(0), []); const [tooltipPosition, setTooltipPosition] = useState({ @@ -120,8 +114,6 @@ const Tooltip = ({ const runAnimation = useCallback( (isShowAnimation: boolean) => { if (isShowAnimation) { - if (showAnimationRef.current) return; - Animated.spring(animatedValue, { toValue: 1, speed: 6, @@ -129,10 +121,7 @@ const Tooltip = ({ }).start(() => { onVisibleChange && onVisibleChange(true); }); - showAnimationRef.current = true; } else { - if (hideAnimationRef.current) return; - Animated.timing(animatedValue, { toValue: 0, duration: 300, @@ -140,7 +129,6 @@ const Tooltip = ({ }).start(() => { onVisibleChange && onVisibleChange(false); }); - hideAnimationRef.current = true; } }, [animatedValue, onVisibleChange] @@ -159,12 +147,19 @@ const Tooltip = ({ // hide animation useEffect(() => { - if (onPress) return; + if (onPress || disableAutoHide) return; setTimeout(() => { runAnimation(false); }, delayShowTime + autoHideTime); - }, [autoHideTime, delayShowTime, onPress, runAnimation]); + }, [autoHideTime, delayShowTime, disableAutoHide, onPress, runAnimation]); + + // hide by props.visible + useEffect(() => { + if (visible === false) { + runAnimation(false); + } + }, [runAnimation, visible]); const handleVerticalTooltipLayout = (event: LayoutChangeEvent) => { const { width, height } = event.nativeEvent.layout;