From 6e993262b669df0199a2c58c8f202109b0093728 Mon Sep 17 00:00:00 2001 From: Website Deployment Script Date: Wed, 8 Aug 2018 21:22:57 +0000 Subject: [PATCH] Deploy website Deploy website version based on 904ebf71561659760671101b25819505adb68a86 --- docs/next/native-components-android.html | 126 +++++++++++++++--- .../next/native-components-android/index.html | 126 +++++++++++++++--- 2 files changed, 218 insertions(+), 34 deletions(-) diff --git a/docs/next/native-components-android.html b/docs/next/native-components-android.html index 87611e497a2..17af7608fc0 100644 --- a/docs/next/native-components-android.html +++ b/docs/next/native-components-android.html @@ -44,23 +44,47 @@
  • Implement the JavaScript module
  • 1. Create the ViewManager subclass

    -

    In this example we create view manager class ReactImageManager that extends SimpleViewManager of type ReactImageView. ReactImageView is the type of object managed by the manager, this will be the custom native view. Name returned by getName is used to reference the native view type from JavaScript.

    -
    ...
    +

    In this example we create view manager class ReactImageManager that extends SimpleViewManager of type ReactImageView. ReactImageView is the type of object managed by the manager, this will be the custom native view. Set the initializers and set the name REACT_CLASS to CustomRCTImageView as RCTImageView is already used by React. Name returned by getName is used to reference the native view type from JavaScript.

    +
    // ReactImageManager.java
    +
    +package com.your-app-name;
    +
    +import javax.annotation.Nullable;
    +
    +import com.facebook.drawee.backends.pipeline.Fresco;
    +import com.facebook.react.bridge.ReadableArray;
    +import com.facebook.react.uimanager.SimpleViewManager;
    +import com.facebook.react.uimanager.ThemedReactContext;
    +import com.facebook.react.uimanager.ViewProps;
    +import com.facebook.react.uimanager.annotations.ReactProp;
    +import com.facebook.react.views.image.ImageResizeMode;
    +import com.facebook.react.views.image.ReactImageView;
     
     public class ReactImageManager extends SimpleViewManager<ReactImageView> {
     
    -  public static final String REACT_CLASS = "RCTImageView";
    +  public static final String REACT_CLASS = "CustomRCTImageView";
    +    private Object mCallerContext;
    +
    +  public ReactImageManagerModule() {
    +  }
    +
    +  public ReactImageManagerModule(Object mCallerContext) {
    +      this.mCallerContext = mCallerContext;
    +  }
     
       @Override
       public String getName() {
    -    return REACT_CLASS;
    +      return REACT_CLASS;
       }
    +}
     

    2. Implement method createViewInstance

    Views are created in the createViewInstance method, the view should initialize itself in its default state, any properties will be set via a follow up call to updateView.

    -
      @Override
    +
    // ReactImageManager.java
    +
    +  @Override
       public ReactImageView createViewInstance(ThemedReactContext context) {
    -    return new ReactImageView(context, Fresco.newDraweeControllerBuilder(), mCallerContext);
    +    return new ReactImageView(context, Fresco.newDraweeControllerBuilder(), null, mCallerContext);
       }
     

    3. Expose view property setters using @ReactProp (or @ReactPropGroup) annotation

    @@ -69,7 +93,9 @@

    Except from name, @ReactProp annotation may take following optional arguments: defaultBoolean, defaultInt, defaultFloat. Those arguments should be of the corresponding primitive type (accordingly boolean, int, float) and the value provided will be passed to the setter method in case when the property that the setter is referencing has been removed from the component. Note that "default" values are only provided for primitive types, in case when setter is of some complex type, null will be provided as a default value in case when corresponding property gets removed.

    Setter declaration requirements for methods annotated with @ReactPropGroup are different than for @ReactProp, please refer to the @ReactPropGroup annotation class docs for more information about it.

    IMPORTANT! in ReactJS updating the property value will result in setter method call. Note that one of the ways we can update component is by removing properties that have been set before. In that case setter method will be called as well to notify view manager that property has changed. In that case "default" value will be provided (for primitive types "default" can value can be specified using defaultBoolean, defaultFloat, etc. arguments of @ReactProp annotation, for complex types setter will be called with value set to null).

    -
      @ReactProp(name = "src")
    +
    // ReactImageManager.java
    +
    +  @ReactProp(name = "src")
       public void setSrc(ReactImageView view, @Nullable ReadableArray sources) {
         view.setSource(sources);
       }
    @@ -86,34 +112,100 @@
     

    4. Register the ViewManager

    The final Java step is to register the ViewManager to the application, this happens in a similar way to Native Modules, via the applications package member function createViewManagers.

    -
      @Override
    +
    //ReactImageManagerPackage.java
    +
    +package com.your-app-name;
    +
    +import com.facebook.react.ReactPackage;
    +import com.facebook.react.bridge.NativeModule;
    +import com.facebook.react.bridge.ReactApplicationContext;
    +import com.facebook.react.uimanager.ViewManager;
    +
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.List;
    +
    +public class ReactImageManagerPackage implements ReactPackage {
    +
    +  @Override
       public List<ViewManager> createViewManagers(
                                 ReactApplicationContext reactContext) {
         return Arrays.<ViewManager>asList(
    -      new ReactImageManager()
    +      new MainReactPackage(),
    +      new ReactImageManager() // <- new UI Component here
         );
       }
    +  
    +  @Override
    +    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    +        List<NativeModule> modules = new ArrayList<>();
    +
    +        modules.add(new ReactImageManagerModule(reactContext));
    +
    +        return modules;
    +    }
    + }
     

    5. Implement the JavaScript module

    The very final step is to create the JavaScript module that defines the interface layer between Java and JavaScript for the users of your new view. Much of the effort is handled by internal React code in Java and JavaScript and all that is left for you is to describe the propTypes.

    // ImageView.js
     
     import PropTypes from 'prop-types';
    -import {requireNativeComponent, ViewPropTypes} from 'react-native';
    +import {
    +    requireNativeComponent,
    +    ViewPropTypes
    +} from 'react-native';
     
     var iface = {
    -  name: 'ImageView',
    +  name: 'CustomImageView',
       propTypes: {
    -    src: PropTypes.string,
    -    borderRadius: PropTypes.number,
    -    resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch']),
    -    ...ViewPropTypes, // include the default view properties
    +      src: PropTypes.arrayOf(
    +              PropTypes.shape({
    +                  uri: PropTypes.string,
    +          })
    +      ),
    +      borderRadius: PropTypes.number,
    +      resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch']),
    +      ...ViewPropTypes, // include the default view properties
       },
     };
     
    -module.exports = requireNativeComponent('RCTImageView', iface);
    +module.exports = requireNativeComponent('CustomRCTImageView', iface);
     
    -

    requireNativeComponent commonly takes two parameters, the first is the name of the native view and the second is an object that describes the component interface. The component interface should declare a friendly name for use in debug messages and must declare the propTypes reflected by the Native View. The propTypes are used for checking the validity of a user's use of the native view. Note that if you need your JavaScript component to do more than just specify a name and propTypes, like do custom event handling, you can wrap the native component in a normal react component. In that case, you want to pass in the wrapper component instead of iface to requireNativeComponent. This is illustrated in the MyCustomView example below.

    +

    requireNativeComponent commonly takes two parameters, the first is the name of the native view and the second is an object that describes the component interface. The component interface should declare a friendly name for use in debug messages and must declare the propTypes reflected by the Native View. The propTypes are used for checking the validity of a user's use of the native view. Now we can use the created component in our React-Native application. Dont worry why we have to set wrap the uri-Object into [] for now. When you use already creatred <Image /> component you do not have to do that.

    +
    // App.js
    +
    +import React, { Component } from 'react';
    +import {
    +  Platform,
    +  StyleSheet,
    +  Text,
    +  View
    +} from 'react-native';
    +import CustomImageView from './bridge/ImageView.js';
    +
    +type Props = {};
    +export default class App extends Component<Props> {
    +  render() {
    +    return (
    +      <View style={styles.container}>
    +        <CustomImageView src={[{ uri: 'https://facebook.github.io/react/logo-og.png'}]}
    +          style={{ width: 400, height: 400 }} />
    +      </View>
    +    );
    +  }
    +}
    +
    +const styles = StyleSheet.create({
    +  container: {
    +    flex: 1,
    +    justifyContent: 'center',
    +    alignItems: 'center',
    +    backgroundColor: '#F5FCFF',
    +  },
    +});
    +
    +

    Note that if you need your JavaScript component to do more than just specify a name and propTypes, like do custom event handling, you can wrap the native component in a normal react component. In that case, you want to pass in the wrapper component instead of iface to requireNativeComponent. This is illustrated in the MyCustomView example below.

    Events

    So now we know how to expose native view components that we can control easily from JS, but how do we deal with events from the user, like pinch-zooms or panning? When a native event occurs the native code should issue an event to the JavaScript representation of the View, and the two views are linked with the value returned from the getId() method.

    class MyCustomView extends View {
    diff --git a/docs/next/native-components-android/index.html b/docs/next/native-components-android/index.html
    index 87611e497a2..17af7608fc0 100644
    --- a/docs/next/native-components-android/index.html
    +++ b/docs/next/native-components-android/index.html
    @@ -44,23 +44,47 @@
     
  • Implement the JavaScript module
  • 1. Create the ViewManager subclass

    -

    In this example we create view manager class ReactImageManager that extends SimpleViewManager of type ReactImageView. ReactImageView is the type of object managed by the manager, this will be the custom native view. Name returned by getName is used to reference the native view type from JavaScript.

    -
    ...
    +

    In this example we create view manager class ReactImageManager that extends SimpleViewManager of type ReactImageView. ReactImageView is the type of object managed by the manager, this will be the custom native view. Set the initializers and set the name REACT_CLASS to CustomRCTImageView as RCTImageView is already used by React. Name returned by getName is used to reference the native view type from JavaScript.

    +
    // ReactImageManager.java
    +
    +package com.your-app-name;
    +
    +import javax.annotation.Nullable;
    +
    +import com.facebook.drawee.backends.pipeline.Fresco;
    +import com.facebook.react.bridge.ReadableArray;
    +import com.facebook.react.uimanager.SimpleViewManager;
    +import com.facebook.react.uimanager.ThemedReactContext;
    +import com.facebook.react.uimanager.ViewProps;
    +import com.facebook.react.uimanager.annotations.ReactProp;
    +import com.facebook.react.views.image.ImageResizeMode;
    +import com.facebook.react.views.image.ReactImageView;
     
     public class ReactImageManager extends SimpleViewManager<ReactImageView> {
     
    -  public static final String REACT_CLASS = "RCTImageView";
    +  public static final String REACT_CLASS = "CustomRCTImageView";
    +    private Object mCallerContext;
    +
    +  public ReactImageManagerModule() {
    +  }
    +
    +  public ReactImageManagerModule(Object mCallerContext) {
    +      this.mCallerContext = mCallerContext;
    +  }
     
       @Override
       public String getName() {
    -    return REACT_CLASS;
    +      return REACT_CLASS;
       }
    +}
     

    2. Implement method createViewInstance

    Views are created in the createViewInstance method, the view should initialize itself in its default state, any properties will be set via a follow up call to updateView.

    -
      @Override
    +
    // ReactImageManager.java
    +
    +  @Override
       public ReactImageView createViewInstance(ThemedReactContext context) {
    -    return new ReactImageView(context, Fresco.newDraweeControllerBuilder(), mCallerContext);
    +    return new ReactImageView(context, Fresco.newDraweeControllerBuilder(), null, mCallerContext);
       }
     

    3. Expose view property setters using @ReactProp (or @ReactPropGroup) annotation

    @@ -69,7 +93,9 @@

    Except from name, @ReactProp annotation may take following optional arguments: defaultBoolean, defaultInt, defaultFloat. Those arguments should be of the corresponding primitive type (accordingly boolean, int, float) and the value provided will be passed to the setter method in case when the property that the setter is referencing has been removed from the component. Note that "default" values are only provided for primitive types, in case when setter is of some complex type, null will be provided as a default value in case when corresponding property gets removed.

    Setter declaration requirements for methods annotated with @ReactPropGroup are different than for @ReactProp, please refer to the @ReactPropGroup annotation class docs for more information about it.

    IMPORTANT! in ReactJS updating the property value will result in setter method call. Note that one of the ways we can update component is by removing properties that have been set before. In that case setter method will be called as well to notify view manager that property has changed. In that case "default" value will be provided (for primitive types "default" can value can be specified using defaultBoolean, defaultFloat, etc. arguments of @ReactProp annotation, for complex types setter will be called with value set to null).

    -
      @ReactProp(name = "src")
    +
    // ReactImageManager.java
    +
    +  @ReactProp(name = "src")
       public void setSrc(ReactImageView view, @Nullable ReadableArray sources) {
         view.setSource(sources);
       }
    @@ -86,34 +112,100 @@
     

    4. Register the ViewManager

    The final Java step is to register the ViewManager to the application, this happens in a similar way to Native Modules, via the applications package member function createViewManagers.

    -
      @Override
    +
    //ReactImageManagerPackage.java
    +
    +package com.your-app-name;
    +
    +import com.facebook.react.ReactPackage;
    +import com.facebook.react.bridge.NativeModule;
    +import com.facebook.react.bridge.ReactApplicationContext;
    +import com.facebook.react.uimanager.ViewManager;
    +
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.List;
    +
    +public class ReactImageManagerPackage implements ReactPackage {
    +
    +  @Override
       public List<ViewManager> createViewManagers(
                                 ReactApplicationContext reactContext) {
         return Arrays.<ViewManager>asList(
    -      new ReactImageManager()
    +      new MainReactPackage(),
    +      new ReactImageManager() // <- new UI Component here
         );
       }
    +  
    +  @Override
    +    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    +        List<NativeModule> modules = new ArrayList<>();
    +
    +        modules.add(new ReactImageManagerModule(reactContext));
    +
    +        return modules;
    +    }
    + }
     

    5. Implement the JavaScript module

    The very final step is to create the JavaScript module that defines the interface layer between Java and JavaScript for the users of your new view. Much of the effort is handled by internal React code in Java and JavaScript and all that is left for you is to describe the propTypes.

    // ImageView.js
     
     import PropTypes from 'prop-types';
    -import {requireNativeComponent, ViewPropTypes} from 'react-native';
    +import {
    +    requireNativeComponent,
    +    ViewPropTypes
    +} from 'react-native';
     
     var iface = {
    -  name: 'ImageView',
    +  name: 'CustomImageView',
       propTypes: {
    -    src: PropTypes.string,
    -    borderRadius: PropTypes.number,
    -    resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch']),
    -    ...ViewPropTypes, // include the default view properties
    +      src: PropTypes.arrayOf(
    +              PropTypes.shape({
    +                  uri: PropTypes.string,
    +          })
    +      ),
    +      borderRadius: PropTypes.number,
    +      resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch']),
    +      ...ViewPropTypes, // include the default view properties
       },
     };
     
    -module.exports = requireNativeComponent('RCTImageView', iface);
    +module.exports = requireNativeComponent('CustomRCTImageView', iface);
     
    -

    requireNativeComponent commonly takes two parameters, the first is the name of the native view and the second is an object that describes the component interface. The component interface should declare a friendly name for use in debug messages and must declare the propTypes reflected by the Native View. The propTypes are used for checking the validity of a user's use of the native view. Note that if you need your JavaScript component to do more than just specify a name and propTypes, like do custom event handling, you can wrap the native component in a normal react component. In that case, you want to pass in the wrapper component instead of iface to requireNativeComponent. This is illustrated in the MyCustomView example below.

    +

    requireNativeComponent commonly takes two parameters, the first is the name of the native view and the second is an object that describes the component interface. The component interface should declare a friendly name for use in debug messages and must declare the propTypes reflected by the Native View. The propTypes are used for checking the validity of a user's use of the native view. Now we can use the created component in our React-Native application. Dont worry why we have to set wrap the uri-Object into [] for now. When you use already creatred <Image /> component you do not have to do that.

    +
    // App.js
    +
    +import React, { Component } from 'react';
    +import {
    +  Platform,
    +  StyleSheet,
    +  Text,
    +  View
    +} from 'react-native';
    +import CustomImageView from './bridge/ImageView.js';
    +
    +type Props = {};
    +export default class App extends Component<Props> {
    +  render() {
    +    return (
    +      <View style={styles.container}>
    +        <CustomImageView src={[{ uri: 'https://facebook.github.io/react/logo-og.png'}]}
    +          style={{ width: 400, height: 400 }} />
    +      </View>
    +    );
    +  }
    +}
    +
    +const styles = StyleSheet.create({
    +  container: {
    +    flex: 1,
    +    justifyContent: 'center',
    +    alignItems: 'center',
    +    backgroundColor: '#F5FCFF',
    +  },
    +});
    +
    +

    Note that if you need your JavaScript component to do more than just specify a name and propTypes, like do custom event handling, you can wrap the native component in a normal react component. In that case, you want to pass in the wrapper component instead of iface to requireNativeComponent. This is illustrated in the MyCustomView example below.

    Events

    So now we know how to expose native view components that we can control easily from JS, but how do we deal with events from the user, like pinch-zooms or panning? When a native event occurs the native code should issue an event to the JavaScript representation of the View, and the two views are linked with the value returned from the getId() method.

    class MyCustomView extends View {