Skip to content

Commit

Permalink
Support setting hitSlop with single value
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javache authored and facebook-github-bot committed Nov 22, 2021
1 parent 00bb2ba commit a96bdb7
Showing 1 changed file with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
Expand Down Expand Up @@ -126,22 +127,35 @@ public void setBorderStyle(ReactViewGroup view, @Nullable String borderStyle) {
}

@ReactProp(name = "hitSlop")
public void setHitSlop(final ReactViewGroup view, @Nullable ReadableMap hitSlop) {
if (hitSlop == null) {
view.setHitSlopRect(null);
} else {
view.setHitSlopRect(
new Rect(
hitSlop.hasKey("left")
? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("left"))
: 0,
hitSlop.hasKey("top") ? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("top")) : 0,
hitSlop.hasKey("right")
? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("right"))
: 0,
hitSlop.hasKey("bottom")
? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("bottom"))
: 0));
public void setHitSlop(final ReactViewGroup view, Dynamic hitSlop) {
switch (hitSlop.getType()) {
case Null:
view.setHitSlopRect(null);
break;
case Map:
ReadableMap hitSlopMap = hitSlop.asMap();
view.setHitSlopRect(
new Rect(
hitSlopMap.hasKey("left")
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("left"))
: 0,
hitSlopMap.hasKey("top")
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("top"))
: 0,
hitSlopMap.hasKey("right")
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("right"))
: 0,
hitSlopMap.hasKey("bottom")
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("bottom"))
: 0));
break;
case Number:
int hitSlopValue = (int) PixelUtil.toPixelFromDIP(hitSlop.asDouble());
view.setHitSlopRect(new Rect(hitSlopValue, hitSlopValue, hitSlopValue, hitSlopValue));
break;
default:
throw new JSApplicationIllegalArgumentException(
"Invalid type for 'hitSlop' value " + hitSlop.getType());
}
}

Expand Down

0 comments on commit a96bdb7

Please sign in to comment.