From 1cf0c1237851caef2a4dbc233bc20e44ac1bc813 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Sat, 13 May 2023 00:50:58 +0500 Subject: [PATCH] fix: add a timeout on BaseGenericPressable for disabled cursor style --- .../GenericPressable/BaseGenericPressable.js | 99 ++++++++++++------- .../Pressable/PressableWithFeedback.js | 8 +- 2 files changed, 67 insertions(+), 40 deletions(-) diff --git a/src/components/Pressable/GenericPressable/BaseGenericPressable.js b/src/components/Pressable/GenericPressable/BaseGenericPressable.js index 80664f2d3521..efa1ba1468f5 100644 --- a/src/components/Pressable/GenericPressable/BaseGenericPressable.js +++ b/src/components/Pressable/GenericPressable/BaseGenericPressable.js @@ -1,4 +1,4 @@ -import React, {useCallback, useEffect, useMemo, forwardRef} from 'react'; +import React, {useCallback, useEffect, useState, useMemo, forwardRef} from 'react'; import {Pressable} from 'react-native'; import _ from 'underscore'; import Accessibility from '../../../libs/Accessibility'; @@ -63,45 +63,70 @@ const GenericPressable = forwardRef((props, ref) => { return props.disabled || shouldBeDisabledByScreenReader; }, [isScreenReaderActive, enableInScreenReaderStates, props.disabled]); - const onLongPressHandler = useCallback((event) => { - if (isDisabled) { - return; - } - if (!onLongPress) { - return; - } - if (shouldUseHapticsOnLongPress) { - HapticFeedback.longPress(); - } - if (ref && ref.current) { - ref.current.blur(); - } - onLongPress(event); - - Accessibility.moveAccessibilityFocus(nextFocusRef); - }, [shouldUseHapticsOnLongPress, onLongPress, nextFocusRef, ref, isDisabled]); + const [shouldUseDisabledCursor, setShouldUseDisabledCursor] = useState(isDisabled); + + const onLongPressHandler = useCallback( + (event) => { + if (isDisabled) { + return; + } + if (!onLongPress) { + return; + } + if (shouldUseHapticsOnLongPress) { + HapticFeedback.longPress(); + } + if (ref && ref.current) { + ref.current.blur(); + } + onLongPress(event); + + Accessibility.moveAccessibilityFocus(nextFocusRef); + }, + [shouldUseHapticsOnLongPress, onLongPress, nextFocusRef, ref, isDisabled], + ); - const onPressHandler = useCallback((event) => { - if (isDisabled) { - return; - } - if (shouldUseHapticsOnPress) { - HapticFeedback.press(); - } - if (ref && ref.current) { - ref.current.blur(); - } - onPress(event); + const onPressHandler = useCallback( + (event) => { + if (isDisabled) { + return; + } + if (shouldUseHapticsOnPress) { + HapticFeedback.press(); + } + if (ref && ref.current) { + ref.current.blur(); + } + onPress(event); + + Accessibility.moveAccessibilityFocus(nextFocusRef); + }, + [shouldUseHapticsOnPress, onPress, nextFocusRef, ref, isDisabled], + ); - Accessibility.moveAccessibilityFocus(nextFocusRef); - }, [shouldUseHapticsOnPress, onPress, nextFocusRef, ref, isDisabled]); + const onKeyPressHandler = useCallback( + (event) => { + if (event.key !== 'Enter') { + return; + } + onPressHandler(event); + }, + [onPressHandler], + ); - const onKeyPressHandler = useCallback((event) => { - if (event.key !== 'Enter') { - return; + useEffect(() => { + let timer = null; + if (!isDisabled) { + setShouldUseDisabledCursor(false); + } else { + timer = setTimeout(() => setShouldUseDisabledCursor(true), 1000); } - onPressHandler(event); - }, [onPressHandler]); + + return () => { + if (!timer) return; + clearTimeout(timer); + }; + }, [isDisabled]); useEffect(() => { if (!keyboardShortcut) { @@ -122,7 +147,7 @@ const GenericPressable = forwardRef((props, ref) => { onPressIn={!isDisabled ? onPressIn : undefined} onPressOut={!isDisabled ? onPressOut : undefined} style={(state) => [ - getCursorStyle(isDisabled, [props.accessibilityRole, props.role].includes('text')), + getCursorStyle(shouldUseDisabledCursor, [props.accessibilityRole, props.role].includes('text')), StyleUtils.parseStyleFromFunction(props.style, state), isScreenReaderActive && StyleUtils.parseStyleFromFunction(props.screenReaderActiveStyle, state), state.focused && StyleUtils.parseStyleFromFunction(props.focusStyle, state), diff --git a/src/components/Pressable/PressableWithFeedback.js b/src/components/Pressable/PressableWithFeedback.js index d113798e9c6e..e2d227c80472 100644 --- a/src/components/Pressable/PressableWithFeedback.js +++ b/src/components/Pressable/PressableWithFeedback.js @@ -46,9 +46,11 @@ const PressableWithFeedback = forwardRef((props, ref) => { setDisabled(props.disabled); return; } - onPress.then(() => { - setDisabled(props.disabled); - }); + onPress + .then(() => { + setDisabled(props.disabled); + }) + .catch(() => setDisabled(props.disabled)); }); }} >