Blame view

js/common/components/customComponents/View.js.bak 8.89 KB
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @providesModule View
 * @flow
 */
'use strict';
14 15 16
const NativeMethodsMixin = require('NativeMethodsMixin');
const NativeModules = require('NativeModules');
const Platform = require('Platform');
17
const React = require('React');
18 19
const PropTypes = require('prop-types');
const ReactNativeFeatureFlags = require('ReactNativeFeatureFlags');
20 21
const ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
22
const ViewPropTypes = require('ViewPropTypes');
23
24 25
const invariant = require('fbjs/lib/invariant');
const warning = require('fbjs/lib/warning');
26
27 28 29 30
const {
  AccessibilityComponentTypes,
  AccessibilityTraits,
} = require('ViewAccessibility');
31
32
const requireNativeComponent = require('requireNativeComponent');
33
34 35
const forceTouchAvailable = (NativeModules.PlatformConstants &&
  NativeModules.PlatformConstants.forceTouchAvailable) || false;
36 37 38

/**
 * The most fundamental component for building a UI, `View` is a container that supports layout with
39 40 41
 * [flexbox](docs/flexbox.html), [style](docs/style.html),
 * [some touch handling](docs/handling-touches.html), and
 * [accessibility](docs/accessibility.html) controls. `View` maps directly to the
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
 * native view equivalent on whatever platform React Native is running on, whether that is a
 * `UIView`, `<div>`, `android.view`, etc.
 *
 * `View` is designed to be nested inside other views and can have 0 to many children of any type.
 *
 * This example creates a `View` that wraps two colored boxes and a text component in a row with
 * padding.
 *
 * ```javascript
 * class ViewColoredBoxesWithText extends Component {
 *   render() {
 *     return (
 *       <View style={{flexDirection: 'row', height: 100, padding: 20}}>
 *         <View style={{backgroundColor: 'blue', flex: 0.3}} />
 *         <View style={{backgroundColor: 'red', flex: 0.5}} />
 *         <Text>Hello World!</Text>
 *       </View>
 *     );
 *   }
 * }
 * ```
 *
64
 * > `View`s are designed to be used with [`StyleSheet`](docs/style.html) for clarity
65 66 67 68
 * > and performance, although inline styles are also supported.
 *
 * ### Synthetic Touch Events
 *
69
 * For `View` responder props (e.g., `onResponderMove`), the synthetic touch event passed to them
70 71 72 73 74 75 76 77 78 79 80 81 82
 * are of the following form:
 *
 * - `nativeEvent`
 *   - `changedTouches` - Array of all touch events that have changed since the last event.
 *   - `identifier` - The ID of the touch.
 *   - `locationX` - The X position of the touch, relative to the element.
 *   - `locationY` - The Y position of the touch, relative to the element.
 *   - `pageX` - The X position of the touch, relative to the root element.
 *   - `pageY` - The Y position of the touch, relative to the root element.
 *   - `target` - The node id of the element receiving the touch event.
 *   - `timestamp` - A time identifier for the touch, useful for velocity calculation.
 *   - `touches` - Array of all current touches on the screen.
 */
83
// $FlowFixMe(>=0.41.0)
84 85 86 87 88 89 90
const View = React.createClass({
  // TODO: We should probably expose the mixins, viewConfig, and statics publicly. For example,
  // one of the props is of type AccessibilityComponentType. That is defined as a const[] above,
  // but it is not rendered by the docs, since `statics` below is not rendered. So its Possible
  // values had to be hardcoded.
  mixins: [NativeMethodsMixin],
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  // `propTypes` should not be accessed directly on View since this wrapper only
  // exists for DEV mode. However it's important for them to be declared.
  // If the object passed to `createClass` specifies `propTypes`, Flow will
  // create a static type from it. This property will be over-written below with
  // a warn-on-use getter though.
  // TODO (bvaughn) Remove the warn-on-use comment after April 1.
  propTypes: {
    yh_exposureData: PropTypes.object,

    yh_viewVisible: PropTypes.bool,
    
    ...ViewPropTypes
  },

  // ReactElementValidator will (temporarily) use this private accessor when
  // detected to avoid triggering the warning message.
  // TODO (bvaughn) Remove this after April 1 ReactNative RC is tagged.
  statics: {
    __propTypesSecretDontUseThesePlease: ViewPropTypes
  },
112 113 114 115 116 117 118 119 120
  /**
   * `NativeMethodsMixin` will look for this when invoking `setNativeProps`. We
   * make `this` look like an actual native component class.
   */
  viewConfig: {
    uiViewClassName: 'RCTView',
    validAttributes: ReactNativeViewAttributes.RCTView
  },
121 122
  contextTypes: {
    isInAParentText: PropTypes.bool,
123 124 125
  },

  render: function() {
126 127 128 129
    invariant(
      !(this.context.isInAParentText && Platform.OS === 'android'),
      'Nesting of <View> within <Text> is not supported on Android.');
130 131 132 133 134 135 136 137
    // WARNING: This method will not be used in production mode as in that mode we
    // replace wrapper component View with generated native wrapper RCTView. Avoid
    // adding functionality this component that you'd want to be available in both
    // dev and prod modes.
    return <RCTView {...this.props} />;
  },
});
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
// Warn about unsupported use of View static properties as these will no longer
// be supported with React fiber. This warning message will go away in the next
// ReactNative release. Use defineProperty() rather than createClass() statics
// because the mixin process auto-triggers the 1-time warning message.
// TODO (bvaughn) Remove this after April 1 ReactNative RC is tagged.
function mixinStatics (target) {
  let warnedAboutAccessibilityTraits = false;
  let warnedAboutAccessibilityComponentType = false;
  let warnedAboutForceTouchAvailable = false;
  let warnedAboutPropTypes = false;

  // $FlowFixMe https://github.com/facebook/flow/issues/285
  Object.defineProperty(target, 'AccessibilityTraits', {
    get: function() {
      warning(
        warnedAboutAccessibilityTraits,
        'View.AccessibilityTraits has been deprecated and will be ' +
        'removed in a future version of ReactNative. Use ' +
        'ViewAccessibility.AccessibilityTraits instead.'
      );
      warnedAboutAccessibilityTraits = true;
      return AccessibilityTraits;
    }
  });
  // $FlowFixMe https://github.com/facebook/flow/issues/285
  Object.defineProperty(target, 'AccessibilityComponentType', {
    get: function() {
      warning(
        warnedAboutAccessibilityComponentType,
        'View.AccessibilityComponentType has been deprecated and will be ' +
        'removed in a future version of ReactNative. Use ' +
        'ViewAccessibility.AccessibilityComponentTypes instead.'
      );
      warnedAboutAccessibilityComponentType = true;
      return AccessibilityComponentTypes;
    }
  });
  // $FlowFixMe https://github.com/facebook/flow/issues/285
  Object.defineProperty(target, 'forceTouchAvailable', {
    get: function() {
      warning(
        warnedAboutForceTouchAvailable,
        'View.forceTouchAvailable has been deprecated and will be removed ' +
        'in a future version of ReactNative. Use ' +
        'NativeModules.PlatformConstants.forceTouchAvailable instead.'
      );
      warnedAboutForceTouchAvailable = true;
      return forceTouchAvailable;
    }
  });
  // $FlowFixMe https://github.com/facebook/flow/issues/285
  Object.defineProperty(target, 'propTypes', {
    get: function() {
      warning(
        warnedAboutPropTypes,
        'View.propTypes has been deprecated and will be removed in a future ' +
        'version of ReactNative. Use ViewPropTypes instead.'
      );
      warnedAboutPropTypes = true;
      return ViewPropTypes;
    }
  });
}
202 203 204
const RCTView = requireNativeComponent('RCTView', View, {
  nativeOnly: {
    nativeBackgroundAndroid: true,
205
    nativeForegroundAndroid: true,
206 207 208 209
  }
});

if (__DEV__) {
210
  const UIManager = require('UIManager');
211 212 213 214 215 216 217 218 219 220 221
  const viewConfig = UIManager.viewConfigs && UIManager.viewConfigs.RCTView || {};
  for (const prop in viewConfig.nativeProps) {
    const viewAny: any = View; // Appease flow
    if (!viewAny.propTypes[prop] && !ReactNativeStyleAttributes[prop]) {
      throw new Error(
        'View is missing propType for native prop `' + prop + '`'
      );
    }
  }
}
222 223 224
// TODO (bvaughn) Remove feature flags once all static View accessors are gone.
// We temporarily wrap fiber native views with the create-class View above,
// Because external code sometimes accesses static properties of this view.
225
let ViewToExport = RCTView;
226 227 228 229 230
if (
  __DEV__ ||
  ReactNativeFeatureFlags.useFiber
) {
  mixinStatics(View);
231 232
  ViewToExport = View;
} else {
233 234
  // TODO (bvaughn) Remove this mixin once all static View accessors are gone.
  mixinStatics((RCTView : any));
235 236
}
237 238 239
// TODO (bvaughn) Temporarily mask Flow warnings for View property accesses.
// We're wrapping the string type (Fiber) for now to avoid any actual problems.
module.exports = ((ViewToExport : any) : typeof View);