Skip to content

Commit 5264832

Browse files
sherginfacebook-github-bot
authored andcommittedJan 15, 2018
Generalization of isInAParentText context
Summary: Currently `isInAParentText` context works as imaginary `isInAAncestorText` context (not like a real `isInAParentText`). Let's imagine we have hierarchy like: `View -> Text -> Text* -> View* -> Text* -> Text* -> View*` With current implementation all nodes marked with asterisk have `isInAParentText` context, which is incorrect (because some of them actually in View context). With the new implemetations it will work like this: `View -> Text -> Text* -> View* -> Text -> Text* -> View*` So, only nodes which have <Text> (or <TextInput>) as a parent will have `isInAParentText` context. This change allows to select proper `Text` vs. `VirtualText` component in cases where <Text> and <View> components can interleave each other. Reviewed By: sahrens Differential Revision: D6690495 fbshipit-source-id: f7c59b23d0eaf68a1d08036b858d99c9547f7878
1 parent 9532062 commit 5264832

File tree

5 files changed

+56
-25
lines changed

5 files changed

+56
-25
lines changed
 

‎Libraries/Components/TextInput/TextInput.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const TimerMixin = require('react-timer-mixin');
3131
const TouchableWithoutFeedback = require('TouchableWithoutFeedback');
3232
const UIManager = require('UIManager');
3333
const ViewPropTypes = require('ViewPropTypes');
34+
const {ViewContextTypes} = require('ViewContext');
3435

3536
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
3637
* found when Flow v0.54 was deployed. To see the error delete this comment and
@@ -48,6 +49,8 @@ const onlyMultiline = {
4849
children: true,
4950
};
5051

52+
import type {ViewChildContext} from 'ViewContext';
53+
5154
if (Platform.OS === 'android') {
5255
var AndroidTextInput = requireNativeComponent('AndroidTextInput', null);
5356
} else if (Platform.OS === 'ios') {
@@ -612,11 +615,6 @@ const TextInput = createReactClass({
612615
);
613616
},
614617

615-
contextTypes: {
616-
onFocusRequested: PropTypes.func,
617-
focusEmitter: PropTypes.instanceOf(EventEmitter),
618-
},
619-
620618
_inputRef: (undefined: any),
621619
_focusSubscription: (undefined: ?Function),
622620
_lastNativeText: (undefined: ?string),
@@ -652,12 +650,18 @@ const TextInput = createReactClass({
652650
}
653651
},
654652

655-
getChildContext: function(): Object {
656-
return {isInAParentText: true};
653+
getChildContext(): ViewChildContext {
654+
return {
655+
isInAParentText: true,
656+
};
657657
},
658658

659-
childContextTypes: {
660-
isInAParentText: PropTypes.bool,
659+
childContextTypes: ViewContextTypes,
660+
661+
contextTypes: {
662+
...ViewContextTypes,
663+
onFocusRequested: PropTypes.func,
664+
focusEmitter: PropTypes.instanceOf(EventEmitter),
661665
},
662666

663667
/**

‎Libraries/Components/View/View.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414

1515
const NativeMethodsMixin = require('NativeMethodsMixin');
1616
const Platform = require('Platform');
17-
const PropTypes = require('prop-types');
1817
const React = require('React');
1918
const ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
2019
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
2120
const ViewPropTypes = require('ViewPropTypes');
21+
const {ViewContextTypes} = require('ViewContext');
2222

2323
const createReactClass = require('create-react-class');
2424
const invariant = require('fbjs/lib/invariant');
2525
const requireNativeComponent = require('requireNativeComponent');
2626

2727
import type {ViewProps} from 'ViewPropTypes';
28+
import type {ViewChildContext} from 'ViewContext';
2829

2930
export type Props = ViewProps;
3031

@@ -56,8 +57,12 @@ const View = createReactClass({
5657
validAttributes: ReactNativeViewAttributes.RCTView,
5758
},
5859

59-
contextTypes: {
60-
isInAParentText: PropTypes.bool,
60+
childContextTypes: ViewContextTypes,
61+
62+
getChildContext(): ViewChildContext {
63+
return {
64+
isInAParentText: false,
65+
};
6166
},
6267

6368
render() {
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ViewContext
10+
* @flow
11+
* @format
12+
*/
13+
'use strict';
14+
15+
const PropTypes = require('prop-types');
16+
17+
export type ViewChildContext = {|
18+
+isInAParentText: boolean,
19+
|};
20+
21+
export const ViewContextTypes = {
22+
isInAParentText: PropTypes.bool,
23+
};

‎Libraries/Image/Image.android.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
2222
var Set = require('Set');
2323
var StyleSheet = require('StyleSheet');
2424
var StyleSheetPropType = require('StyleSheetPropType');
25-
var View = require('View');
2625
var ViewPropTypes = require('ViewPropTypes');
2726
var ViewStylePropTypes = require('ViewStylePropTypes');
2827

2928
var createReactClass = require('create-react-class');
30-
var filterObject = require('fbjs/lib/filterObject');
3129
var flattenStyle = require('flattenStyle');
3230
var merge = require('merge');
3331
var requireNativeComponent = require('requireNativeComponent');
3432
var resolveAssetSource = require('resolveAssetSource');
3533

34+
const {ViewContextTypes} = require('ViewContext');
35+
3636
var {ImageLoader} = NativeModules;
3737

3838
let _requestId = 1;
@@ -261,9 +261,7 @@ var Image = createReactClass({
261261
validAttributes: ReactNativeViewAttributes.RCTView,
262262
},
263263

264-
contextTypes: {
265-
isInAParentText: PropTypes.bool,
266-
},
264+
contextTypes: ViewContextTypes,
267265

268266
render: function() {
269267
const source = resolveAssetSource(this.props.source);

‎Libraries/Text/Text.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ const createReactClass = require('create-react-class');
2626
const requireNativeComponent = require('requireNativeComponent');
2727
const mergeFast = require('mergeFast');
2828
const processColor = require('processColor');
29+
const {ViewContextTypes} = require('ViewContext');
2930

3031
const stylePropType = StyleSheetPropType(TextStylePropTypes);
3132

33+
import type {ViewChildContext} from 'ViewContext';
34+
3235
/**
3336
* A React component for displaying text.
3437
*
@@ -401,15 +404,13 @@ const Text = createReactClass({
401404
});
402405
},
403406
mixins: [NativeMethodsMixin],
404-
getChildContext(): Object {
405-
return {isInAParentText: true};
406-
},
407-
childContextTypes: {
408-
isInAParentText: PropTypes.bool,
409-
},
410-
contextTypes: {
411-
isInAParentText: PropTypes.bool,
407+
getChildContext(): ViewChildContext {
408+
return {
409+
isInAParentText: true,
410+
};
412411
},
412+
childContextTypes: ViewContextTypes,
413+
contextTypes: ViewContextTypes,
413414
/**
414415
* Only assigned if touch is needed.
415416
*/

0 commit comments

Comments
 (0)