mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Use ViewManagerDelegate if provided instead of $$PropsSetter to update view props
Summary:
This diff introduces an interface `ViewManagerDelegate` and its base implementation `BaseViewManagerDelegate`, which is used as a parent class for all view manager delegates generated by the JS codegen. Before the changes in this diff, generated delegates didn't support setting the base view properties such as background color, rotation, opacity, etc. Now it's possible to do by using `BaseViewManagerDelegate.setProperty(...)`, and since all generated delegates extend BaseViewManagerDelegate, they can just call `super.setProperty(...)` for properties they don't want to handle.
This diff also introduced a new method `ViewManager.getDelegate()`. This will allow view managers to return an instance of the delegate generated by JS and ensure that the view properties are set in a type-safe manner. If this method returns null (it does by default), we fall back to the default implementation of setting view properties using Java-generated `$$PropsSetter`
classes.
This is an example of an interface class generated by JS:
```
public interface RCTAxialGradientViewViewManagerInterface<T extends View> {
void setColors(T view, Nullable ReadableArray value);
void setLocations(T view, Nullable ReadableArray value);
void setEndX(T view, Float value);
void setEndY(T view, Float value);
void setStartX(T view, Float value);
void setStartY(T view, Float value);
}
```
This is an example of a delegate class generated by JS:
```
public class RCTAxialGradientViewManagerDelegate<T extends View, U extends BaseViewManager<T, ? extends LayoutShadowNode> & RCTAxialGradientViewManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
public RCTAxialGradientViewManagerDelegate(U viewManager) {
super(viewManager);
}
Override
public void setProperty(T view, String propName, Nullable Object value) {
switch (propName) {
case "colors":
mViewManager.setColors(view, (ReadableArray) value);
break;
case "locations":
mViewManager.setLocations(view, (ReadableArray) value);
break;
case "endX":
mViewManager.setEndX(view, value == null ? Float.NaN : ((Double) value).floatValue());
break;
case "endY":
mViewManager.setEndY(view, value == null ? Float.NaN : ((Double) value).floatValue());
break;
case "startX":
mViewManager.setStartX(view, value == null ? Float.NaN : ((Double) value).floatValue());
break;
case "startY":
mViewManager.setStartY(view, value == null ? Float.NaN : ((Double) value).floatValue());
break;
default:
super.setProperty(view, propName, value);
}
}
}
```
NOTE: What if a view manager, for instance ReactAxialGradientManager, wanted to add support for the borderRadius prop? In the old Java codegen, it would just need to create a method and annotate it with ReactProp (name = ViewProps.BORDER_RADIUS) and $$PropsSetter would call this method when a property with this name must be set. With the new JS codegen, borderRadius is a part of the basic view props, so setBorderRadius is not generated as a part of the ViewManagerInterface, so it’s not possible to set this value. I see two options: 1) add a method boolean setProperty (String propName, Object value) and let the view manager handle it in a non-type safe way (return true if it’s been handled). 2) Generate BaseViewManagerInterface which will include all basic view props and make BaseViewManager implement this interface, leaving all methods empty so that it stays compatible with the current implementation. Override these methods in a view manager that needs to handle a specific property in a custom way (so we would override setBorderRadius in ReactAxialGradientManager).
Reviewed By: mdvacca
Differential Revision: D16667686
fbshipit-source-id: 06a15a92f8af55640b7a53c5a34f40366d1be2a8
This commit is contained in:
committed by
Facebook Github Bot
parent
bef87b648c
commit
50fe8119de
@@ -131,6 +131,31 @@ public class ViewProps {
|
||||
public static final String BORDER_END_COLOR = "borderEndColor";
|
||||
public static final String ON_LAYOUT = "onLayout";
|
||||
|
||||
public static final String TRANSFORM = "transform";
|
||||
public static final String ELEVATION = "elevation";
|
||||
public static final String Z_INDEX = "zIndex";
|
||||
public static final String RENDER_TO_HARDWARE_TEXTURE = "renderToHardwareTextureAndroid";
|
||||
public static final String ACCESSIBILITY_LABEL = "accessibilityLabel";
|
||||
public static final String ACCESSIBILITY_HINT = "accessibilityHint";
|
||||
public static final String ACCESSIBILITY_LIVE_REGION = "accessibilityLiveRegion";
|
||||
public static final String ACCESSIBILITY_ROLE = "accessibilityRole";
|
||||
public static final String ACCESSIBILITY_STATES = "accessibilityStates";
|
||||
public static final String ACCESSIBILITY_STATE = "accessibilityState";
|
||||
public static final String ACCESSIBILITY_ACTIONS = "accessibilityActions";
|
||||
public static final String IMPORTANT_FOR_ACCESSIBILITY = "importantForAccessibility";
|
||||
|
||||
// DEPRECATED
|
||||
public static final String ROTATION = "rotation";
|
||||
public static final String SCALE_X = "scaleX";
|
||||
public static final String SCALE_Y = "scaleY";
|
||||
public static final String TRANSLATE_X = "translateX";
|
||||
public static final String TRANSLATE_Y = "translateY";
|
||||
|
||||
/** Used to locate views in end-to-end (UI) tests. */
|
||||
public static final String TEST_ID = "testID";
|
||||
|
||||
public static final String NATIVE_ID = "nativeID";
|
||||
|
||||
public static final int[] BORDER_SPACING_TYPES = {
|
||||
Spacing.ALL,
|
||||
Spacing.START,
|
||||
|
||||
Reference in New Issue
Block a user