Add display:none support to react native

Summary:
This diff adds display:none support to React Native. This enables hiding components which still calling their render method and keeping them within the state of your application. This enables preserving state in a component even though the component is not visible. Previously this was often implemented by rendering a component off screen as a work around. See below playground for usage.

```
class Playground extends React.Component {
  render() {
    return (
      <View style={{width: '100%', height: '100%', flexDirection: 'row', backgroundColor: 'white'}}>
        <View style={{width: 100, height: 100, display: 'none', backgroundColor: 'red'}}/>
        <View style={{width: 100, height: 100, backgroundColor: 'blue'}}/>
      </View>
    );
  }
}
```

Reviewed By: astreet

Differential Revision: D4611771

fbshipit-source-id: 0dbe0494d989df42994ab9ad5125d47f3233cc5a
This commit is contained in:
Emil Sjolander
2017-03-01 09:12:50 -08:00
committed by Facebook Github Bot
parent 31f848a5fa
commit 4d69f4b2d1
9 changed files with 33 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ import com.facebook.react.bridge.ReadableType;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaConstants;
import com.facebook.yoga.YogaDisplay;
import com.facebook.yoga.YogaFlexDirection;
import com.facebook.yoga.YogaJustify;
import com.facebook.yoga.YogaOverflow;
@@ -311,6 +312,15 @@ public class LayoutShadowNode extends ReactShadowNode {
overflow.toUpperCase(Locale.US).replace("-", "_")));
}
@ReactProp(name = ViewProps.DISPLAY)
public void setDisplay(@Nullable String display) {
if (isVirtual()) {
return;
}
setDisplay(display == null ? YogaDisplay.FLEX : YogaDisplay.valueOf(
display.toUpperCase(Locale.US).replace("-", "_")));
}
@ReactPropGroup(names = {
ViewProps.MARGIN,
ViewProps.MARGIN_VERTICAL,

View File

@@ -15,6 +15,7 @@ import java.util.Arrays;
import java.util.ArrayList;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaDisplay;
import com.facebook.yoga.YogaEdge;
import com.facebook.yoga.YogaConstants;
import com.facebook.yoga.YogaDirection;
@@ -662,6 +663,10 @@ public class ReactShadowNode {
mYogaNode.setOverflow(overflow);
}
public void setDisplay(YogaDisplay display) {
mYogaNode.setDisplay(display);
}
public void setMargin(int spacingType, float margin) {
mYogaNode.setMargin(YogaEdge.fromInt(spacingType), margin);
}

View File

@@ -27,6 +27,7 @@ public class ViewProps {
public static final String ALIGN_SELF = "alignSelf";
public static final String ALIGN_CONTENT = "alignContent";
public static final String OVERFLOW = "overflow";
public static final String DISPLAY = "display";
public static final String BOTTOM = "bottom";
public static final String COLLAPSABLE = "collapsable";
public static final String FLEX = "flex";
@@ -124,6 +125,7 @@ public class ViewProps {
JUSTIFY_CONTENT,
OVERFLOW,
ALIGN_CONTENT,
DISPLAY,
/* position */
POSITION,