From b552d39f8633f38482fd7fce941a67db117cde1b Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Fri, 23 Jun 2023 12:37:08 +0800 Subject: [PATCH 1/4] handle the disable state correctly --- .../Pressable/PressableWithFeedback.js | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/components/Pressable/PressableWithFeedback.js b/src/components/Pressable/PressableWithFeedback.js index 7596c9bf8945..737017b4b3d4 100644 --- a/src/components/Pressable/PressableWithFeedback.js +++ b/src/components/Pressable/PressableWithFeedback.js @@ -1,4 +1,4 @@ -import React, {forwardRef, useEffect, useState} from 'react'; +import React, {forwardRef, useMemo, useState} from 'react'; import _ from 'underscore'; import propTypes from 'prop-types'; import {InteractionManager} from 'react-native'; @@ -39,25 +39,22 @@ const PressableWithFeedbackDefaultProps = { const PressableWithFeedback = forwardRef((props, ref) => { const propsWithoutWrapperStyles = _.omit(props, omittedProps); - const [disabled, setDisabled] = useState(props.disabled); - const [isPressed, setIsPressed] = useState(false); + const [isPressed, setPressed] = useState(false); + const [isPressedIn, setPressedIn] = useState(false); const [isHovered, setIsHovered] = useState(false); - - useEffect(() => { - setDisabled(props.disabled); - }, [props.disabled]); + const isDisabled = useMemo(() => props.disabled || isPressed, [props.disabled, isPressed]); return ( { setIsHovered(true); if (props.onHoverIn) props.onHoverIn(); @@ -67,23 +64,23 @@ const PressableWithFeedback = forwardRef((props, ref) => { if (props.onHoverOut) props.onHoverOut(); }} onPressIn={() => { - setIsPressed(true); + setPressedIn(true); if (props.onPressIn) props.onPressIn(); }} onPressOut={() => { - setIsPressed(false); + setPressedIn(false); if (props.onPressOut) props.onPressOut(); }} onPress={(e) => { - setDisabled(true); + setPressed(true); const onPress = props.onPress(e); InteractionManager.runAfterInteractions(() => { if (!(onPress instanceof Promise)) { - setDisabled(props.disabled); + setPressed(false); return; } onPress.finally(() => { - setDisabled(props.disabled); + setPressed(false); }); }); }} From f20e7d01a40007358fa21f2b2ab4ef82d91b7948 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Fri, 23 Jun 2023 18:58:41 +0800 Subject: [PATCH 2/4] remove memoization --- src/components/Pressable/PressableWithFeedback.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Pressable/PressableWithFeedback.js b/src/components/Pressable/PressableWithFeedback.js index 737017b4b3d4..5682cf0760ba 100644 --- a/src/components/Pressable/PressableWithFeedback.js +++ b/src/components/Pressable/PressableWithFeedback.js @@ -1,4 +1,4 @@ -import React, {forwardRef, useMemo, useState} from 'react'; +import React, {forwardRef, useState} from 'react'; import _ from 'underscore'; import propTypes from 'prop-types'; import {InteractionManager} from 'react-native'; @@ -42,7 +42,7 @@ const PressableWithFeedback = forwardRef((props, ref) => { const [isPressed, setPressed] = useState(false); const [isPressedIn, setPressedIn] = useState(false); const [isHovered, setIsHovered] = useState(false); - const isDisabled = useMemo(() => props.disabled || isPressed, [props.disabled, isPressed]); + const isDisabled = props.disabled || isPressed; return ( Date: Fri, 23 Jun 2023 21:00:20 +0800 Subject: [PATCH 3/4] rename state --- .../Pressable/PressableWithFeedback.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/components/Pressable/PressableWithFeedback.js b/src/components/Pressable/PressableWithFeedback.js index 5682cf0760ba..e77848c7c326 100644 --- a/src/components/Pressable/PressableWithFeedback.js +++ b/src/components/Pressable/PressableWithFeedback.js @@ -39,15 +39,15 @@ const PressableWithFeedbackDefaultProps = { const PressableWithFeedback = forwardRef((props, ref) => { const propsWithoutWrapperStyles = _.omit(props, omittedProps); + const [isExecuting, setExecuting] = useState(false); const [isPressed, setPressed] = useState(false); - const [isPressedIn, setPressedIn] = useState(false); const [isHovered, setIsHovered] = useState(false); - const isDisabled = props.disabled || isPressed; + const isDisabled = props.disabled || isExecuting; return ( { if (props.onHoverOut) props.onHoverOut(); }} onPressIn={() => { - setPressedIn(true); + setPressed(true); if (props.onPressIn) props.onPressIn(); }} onPressOut={() => { - setPressedIn(false); + setPressed(false); if (props.onPressOut) props.onPressOut(); }} onPress={(e) => { - setPressed(true); + setExecuting(true); const onPress = props.onPress(e); InteractionManager.runAfterInteractions(() => { if (!(onPress instanceof Promise)) { - setPressed(false); + setExecuting(false); return; } onPress.finally(() => { - setPressed(false); + setExecuting(false); }); }); }} From b019b361da05fed8640311ad0654f15c0ab3c269 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Fri, 23 Jun 2023 21:19:57 +0800 Subject: [PATCH 4/4] rename state callback --- src/components/Pressable/PressableWithFeedback.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/components/Pressable/PressableWithFeedback.js b/src/components/Pressable/PressableWithFeedback.js index e77848c7c326..5119bb0898cd 100644 --- a/src/components/Pressable/PressableWithFeedback.js +++ b/src/components/Pressable/PressableWithFeedback.js @@ -39,8 +39,8 @@ const PressableWithFeedbackDefaultProps = { const PressableWithFeedback = forwardRef((props, ref) => { const propsWithoutWrapperStyles = _.omit(props, omittedProps); - const [isExecuting, setExecuting] = useState(false); - const [isPressed, setPressed] = useState(false); + const [isExecuting, setIsExecuting] = useState(false); + const [isPressed, setIsPressed] = useState(false); const [isHovered, setIsHovered] = useState(false); const isDisabled = props.disabled || isExecuting; @@ -64,23 +64,23 @@ const PressableWithFeedback = forwardRef((props, ref) => { if (props.onHoverOut) props.onHoverOut(); }} onPressIn={() => { - setPressed(true); + setIsPressed(true); if (props.onPressIn) props.onPressIn(); }} onPressOut={() => { - setPressed(false); + setIsPressed(false); if (props.onPressOut) props.onPressOut(); }} onPress={(e) => { - setExecuting(true); + setIsExecuting(true); const onPress = props.onPress(e); InteractionManager.runAfterInteractions(() => { if (!(onPress instanceof Promise)) { - setExecuting(false); + setIsExecuting(false); return; } onPress.finally(() => { - setExecuting(false); + setIsExecuting(false); }); }); }}