Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace contextType with createContext for isInAParentText #1248

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[version]
^0.63.0
^0.78.0

[ignore]
<PROJECT_ROOT>/.*/__tests__/.*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-promise": "^3.7.0",
"eslint-plugin-react": "^7.7.0",
"flow-bin": "^0.63.1",
"flow-bin": "^0.78.0",
"glob": "^7.1.2",
"husky": "^0.14.3",
"jest": "^22.4.3",
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-web/src/exports/Clipboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class Clipboard {
static isAvailable() {
if (clipboardAvailable === undefined) {
clipboardAvailable =
/* $FlowFixMe(<0.93.0) */
typeof document.queryCommandSupported === 'function' &&
document.queryCommandSupported('copy');
}
Expand Down
20 changes: 9 additions & 11 deletions packages/react-native-web/src/exports/Image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ImageStylePropTypes from './ImageStylePropTypes';
import ImageUriCache from './ImageUriCache';
import StyleSheet from '../StyleSheet';
import StyleSheetPropType from '../../modules/StyleSheetPropType';
import TextAncestor from '../TextAncestor';
import View from '../View';
import ViewPropTypes from '../ViewPropTypes';
import { bool, func, number, oneOf, shape } from 'prop-types';
Expand Down Expand Up @@ -96,10 +97,6 @@ type State = {
class Image extends Component<*, State> {
static displayName = 'Image';

static contextTypes = {
isInAParentText: bool
};

static propTypes = {
...ViewPropTypes,
blurRadius: number,
Expand Down Expand Up @@ -179,7 +176,7 @@ class Image extends Component<*, State> {
this._isMounted = false;
}

render() {
renderInner(hasTextAncestor) {
const { shouldDisplaySource } = this.state;
const {
accessibilityLabel,
Expand Down Expand Up @@ -267,12 +264,7 @@ class Image extends Component<*, State> {
accessibilityLabel={accessibilityLabel}
accessible={accessible}
onLayout={this._createLayoutHandler(finalResizeMode)}
style={[
styles.root,
this.context.isInAParentText && styles.inline,
imageSizeStyle,
flatStyle
]}
style={[styles.root, hasTextAncestor && styles.inline, imageSizeStyle, flatStyle]}
testID={testID}
>
<View
Expand All @@ -290,6 +282,12 @@ class Image extends Component<*, State> {
);
}

render() {
return createElement(TextAncestor.Consumer, null, hasTextAncestor => {
return this.renderInner(hasTextAncestor);
});
}

_createImageLoader() {
const { source } = this.props;
this._destroyImageLoader();
Expand Down
28 changes: 11 additions & 17 deletions packages/react-native-web/src/exports/Text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,18 @@

import applyLayout from '../../modules/applyLayout';
import applyNativeMethods from '../../modules/applyNativeMethods';
import { bool } from 'prop-types';
import { Component } from 'react';
import createElement from '../createElement';
import StyleSheet from '../StyleSheet';
import TextAncestor from '../TextAncestor';
import TextPropTypes from './TextPropTypes';

class Text extends Component<*> {
static displayName = 'Text';

static propTypes = TextPropTypes;

static childContextTypes = {
isInAParentText: bool
};

static contextTypes = {
isInAParentText: bool
};

getChildContext() {
return { isInAParentText: true };
}

render() {
renderInner(isInAParentText) {
const {
dir,
numberOfLines,
Expand All @@ -57,8 +45,6 @@ class Text extends Component<*> {
...otherProps
} = this.props;

const { isInAParentText } = this.context;

if (onPress) {
otherProps.accessible = true;
otherProps.onClick = this._createPressHandler(onPress);
Expand All @@ -69,7 +55,7 @@ class Text extends Component<*> {
otherProps.dir = dir !== undefined ? dir : 'auto';
otherProps.style = [
styles.initial,
this.context.isInAParentText === true && styles.isInAParentText,
isInAParentText === true && styles.isInAParentText,
style,
selectable === false && styles.notSelectable,
numberOfLines === 1 && styles.singleLineStyle,
Expand All @@ -81,6 +67,14 @@ class Text extends Component<*> {
return createElement(component, otherProps);
}

render() {
return createElement(TextAncestor.Consumer, null, hasTextAncestor => {
return hasTextAncestor
? this.renderInner(hasTextAncestor)
: createElement(TextAncestor.Provider, { value: true }, this.renderInner(hasTextAncestor));
});
}

_createEnterHandler(fn) {
return e => {
if (e.keyCode === 13) {
Expand Down
19 changes: 19 additions & 0 deletions packages/react-native-web/src/exports/TextAncestor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) 2015-present, Nicolas Gallagher.
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import { createContext } from 'react';

/**
* Whether the current element is the descendant of a <Text> element.
**/
/* $FlowFixMe(>=0.85.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.85 was deployed. To see the error, delete this comment
* and run Flow. */
export default createContext(false);
21 changes: 12 additions & 9 deletions packages/react-native-web/src/exports/View/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

import applyLayout from '../../modules/applyLayout';
import applyNativeMethods from '../../modules/applyNativeMethods';
import { bool } from 'prop-types';
import createElement from '../createElement';
import filterSupportedProps from './filterSupportedProps';
import invariant from 'fbjs/lib/invariant';
import StyleSheet from '../StyleSheet';
import TextAncestor from '../TextAncestor';
import ViewPropTypes, { type ViewProps } from './ViewPropTypes';
import React, { Component } from 'react';

Expand All @@ -30,13 +30,9 @@ const calculateHitSlopStyle = hitSlop => {
class View extends Component<ViewProps> {
static displayName = 'View';

static contextTypes = {
isInAParentText: bool
};

static propTypes = ViewPropTypes;

render() {
renderInner(hasTextAncestor) {
const hitSlop = this.props.hitSlop;
const supportedProps = filterSupportedProps(this.props);

Expand All @@ -49,11 +45,12 @@ class View extends Component<ViewProps> {
});
}

const { isInAParentText } = this.context;

supportedProps.style = StyleSheet.compose(
styles.initial,
StyleSheet.compose(isInAParentText && styles.inline, this.props.style)
StyleSheet.compose(
hasTextAncestor && styles.inline,
this.props.style
)
);

if (hitSlop) {
Expand All @@ -64,6 +61,12 @@ class View extends Component<ViewProps> {

return createElement('div', supportedProps);
}

render() {
return createElement(TextAncestor.Consumer, null, hasTextAncestor => {
return this.renderInner(hasTextAncestor);
});
}
}

const styles = StyleSheet.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const normalizeTouches = touches => {
return emptyArray;
}

// $FlowFixMe
return Array.prototype.slice.call(touches).map(touch => {
const identifier = touch.identifier > 20 ? touch.identifier % 20 : touch.identifier;
let rect;
Expand Down
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4678,9 +4678,10 @@ flatten@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"

flow-bin@^0.63.1:
version "0.63.1"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.63.1.tgz#ab00067c197169a5fb5b4996c8f6927b06694828"
flow-bin@^0.78.0:
version "0.78.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.78.0.tgz#df9fe7f9c9a2dfaff39083949fe2d831b41627b7"
integrity sha512-LV55tE+ItkC9HQAbEK+VxpBe54Ryp/dj4q9KmqDIfhV7mtP+hbvc/1AUf/AaWFIve3eURO0cxoGN4ZQQ3o2HTg==

flow-parser@^0.*:
version "0.76.0"
Expand Down