Skip to content

Commit a96bdb7

Browse files
javachefacebook-github-bot
authored andcommitted
Support setting hitSlop with single value
Summary: The native side supports this in https://github.com/facebook/react-native/blob/main/ReactCommon/react/renderer/graphics/conversions.h#L156 but it fails on the Java side. Changelog: [Android][Fixed] Enable hitSlop to be set using a single number. Reviewed By: yungsters Differential Revision: D32138347 fbshipit-source-id: 266ec061c6849d845b592cf245cc0599055b8522
1 parent 00bb2ba commit a96bdb7

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java

+30-16
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.view.View;
1414
import androidx.annotation.NonNull;
1515
import androidx.annotation.Nullable;
16+
import com.facebook.react.bridge.Dynamic;
1617
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
1718
import com.facebook.react.bridge.ReactContext;
1819
import com.facebook.react.bridge.ReadableArray;
@@ -126,22 +127,35 @@ public void setBorderStyle(ReactViewGroup view, @Nullable String borderStyle) {
126127
}
127128

128129
@ReactProp(name = "hitSlop")
129-
public void setHitSlop(final ReactViewGroup view, @Nullable ReadableMap hitSlop) {
130-
if (hitSlop == null) {
131-
view.setHitSlopRect(null);
132-
} else {
133-
view.setHitSlopRect(
134-
new Rect(
135-
hitSlop.hasKey("left")
136-
? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("left"))
137-
: 0,
138-
hitSlop.hasKey("top") ? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("top")) : 0,
139-
hitSlop.hasKey("right")
140-
? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("right"))
141-
: 0,
142-
hitSlop.hasKey("bottom")
143-
? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("bottom"))
144-
: 0));
130+
public void setHitSlop(final ReactViewGroup view, Dynamic hitSlop) {
131+
switch (hitSlop.getType()) {
132+
case Null:
133+
view.setHitSlopRect(null);
134+
break;
135+
case Map:
136+
ReadableMap hitSlopMap = hitSlop.asMap();
137+
view.setHitSlopRect(
138+
new Rect(
139+
hitSlopMap.hasKey("left")
140+
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("left"))
141+
: 0,
142+
hitSlopMap.hasKey("top")
143+
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("top"))
144+
: 0,
145+
hitSlopMap.hasKey("right")
146+
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("right"))
147+
: 0,
148+
hitSlopMap.hasKey("bottom")
149+
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("bottom"))
150+
: 0));
151+
break;
152+
case Number:
153+
int hitSlopValue = (int) PixelUtil.toPixelFromDIP(hitSlop.asDouble());
154+
view.setHitSlopRect(new Rect(hitSlopValue, hitSlopValue, hitSlopValue, hitSlopValue));
155+
break;
156+
default:
157+
throw new JSApplicationIllegalArgumentException(
158+
"Invalid type for 'hitSlop' value " + hitSlop.getType());
145159
}
146160
}
147161

0 commit comments

Comments
 (0)