From a2a03bc68ba062a96a6971d3791d291f49794dfd Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Mon, 13 May 2019 07:55:38 -0700 Subject: [PATCH] Add inline view examples to RNTester (#24814) Summary: Now that inline views are supported on iOS and Android, we can add some examples to RNTester. I brought back examples from https://github.com/facebook/react-native/commit/03663491c6e68409f73d5c9bbc1a2e3c02ee0966. I also added some new inline view examples in TextInlineView.js. Note that some examples are only supported on iOS because they rely on the inline view being able to size to content. Android's implementation requires that a width and height be specified on the inline view. Here are the known bugs illustrated by these examples: - ClippedByText - Expected: The image/view wraps to the next line (because it is too wide) and gets clipped vertically (because it is too tall). - iOS bug: The image/view does not get wrapped to the next line - Android bug: The view gets wrapped to the next line but doesn't get clipped vertically. The image appears to be positioned too low. - ChangeImageSize/ChangeViewSize: - Expected: The "Change Image/View Width" button causes the image/view to toggle between a width of 50 and 100. - iOS bug: First update works. Subsequent updates don't get rendered. - Android bug: No updates get rendered. - ChangeInnerViewSize: - Expected: The "Change Pink View Width" button causes the pink inner view to toggle between a width of 50 and 100. - iOS bug: First update works but second update **CRASHES** the app. - Android bug: No updates get rendered. [Internal] [Added] - Added inline view examples to RNTester Pull Request resolved: https://github.com/facebook/react-native/pull/24814 Differential Revision: D15318070 Pulled By: cpojer fbshipit-source-id: 35a4aaab88e477d627456eeb4208c509c42927df --- RNTester/js/Shared/TextInlineView.js | 205 +++++++++++++++++++++++++++ RNTester/js/TextExample.android.js | 20 ++- RNTester/js/TextExample.ios.js | 83 +++++++++++ 3 files changed, 303 insertions(+), 5 deletions(-) create mode 100644 RNTester/js/Shared/TextInlineView.js diff --git a/RNTester/js/Shared/TextInlineView.js b/RNTester/js/Shared/TextInlineView.js new file mode 100644 index 00000000000000..9cb8e13aded1ff --- /dev/null +++ b/RNTester/js/Shared/TextInlineView.js @@ -0,0 +1,205 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow + */ + +'use strict'; + +const React = require('react'); +const {Image, Text, TouchableHighlight, View} = require('react-native'); + +function Basic() { + return ( + + This text contains an inline blue view{' '} + and + an inline image . Neat, huh? + + ); +} + +function ClippedByText() { + return ( + + {/* + * Inline View + **/} + + The inline view below is + taller than its Text parent and should be clipped. + + + This is an inline view + {/* Render a red border around the steelblue rectangle to make it clear how the inline view is being clipped */} + + + + + + {/* + * Inline Image + **/} + + The inline image below is + taller than its Text parent and should be clipped. + + + This is an inline image + + + + ); +} + +type ChangeSizeState = {| + width: number, +|}; + +class ChangeImageSize extends React.Component<*, ChangeSizeState> { + state = { + width: 50, + }; + + render() { + return ( + + { + this.setState({width: this.state.width === 50 ? 100 : 50}); + }}> + + Change Image Width (width={this.state.width}) + + + + This is an + + inline image + + + ); + } +} + +class ChangeViewSize extends React.Component<*, ChangeSizeState> { + state = { + width: 50, + }; + + render() { + return ( + + { + this.setState({width: this.state.width === 50 ? 100 : 50}); + }}> + + Change View Width (width={this.state.width}) + + + + This is an + + inline view + + + ); + } +} + +class ChangeInnerViewSize extends React.Component<*, ChangeSizeState> { + state = { + width: 50, + }; + + render() { + return ( + + { + this.setState({width: this.state.width === 50 ? 100 : 50}); + }}> + {/* When updating `state.width`, it's important that the only thing that + changes is the width of the pink inline view. When we do this, we + demonstrate a bug in RN Android where the pink view doesn't get + rerendered and remains at its old size. If other things change + (e.g. we display `state.width` as text somewhere) it could circumvent + the bug and cause the pink view to be rerendered at its new size. */} + Change Pink View Width + + + This is an + + + + inline view + + + ); + } +} + +module.exports = { + Basic, + ClippedByText, + ChangeImageSize, + ChangeViewSize, + ChangeInnerViewSize, +}; diff --git a/RNTester/js/TextExample.android.js b/RNTester/js/TextExample.android.js index 82d77d3427217e..efc53aa8d4a54b 100644 --- a/RNTester/js/TextExample.android.js +++ b/RNTester/js/TextExample.android.js @@ -16,6 +16,7 @@ const React = require('react'); const {Image, StyleSheet, Text, View} = require('react-native'); const RNTesterBlock = require('./RNTesterBlock'); const RNTesterPage = require('./RNTesterPage'); +const TextInlineView = require('./Shared/TextInlineView'); const TextLegend = require('./Shared/TextLegend'); class Entity extends React.Component<{|children: React.Node|}> { @@ -506,11 +507,20 @@ class TextExample extends React.Component<{}> { This text will have a orange highlight on selection. - - - This text contains an inline image{' '} - . Neat, huh? - + + + + + + + + + + + + + + + + + ); +} + type TextAlignExampleRTLState = {| isRTL: boolean, |}; @@ -274,6 +287,28 @@ class TextBaseLineLayoutExample extends React.Component<*, *> { {marker} + {/* iOS-only because it relies on inline views being able to size to content. + * Android's implementation requires that a width and height be specified + * on the inline view. */} + {'Interleaving and :'} + + {marker} + + Some text. + + {marker} + Text inside View. + {marker} + + + {marker} + + {':'} {marker} @@ -914,6 +949,26 @@ exports.examples = [ ); }, }, + { + title: 'Inline views', + render: () => , + }, + { + title: 'Inline image/view clipped by ', + render: () => , + }, + { + title: 'Relayout inline image', + render: () => , + }, + { + title: 'Relayout inline view', + render: () => , + }, + { + title: 'Relayout nested inline view', + render: () => , + }, { title: 'Text shadow', render: function() { @@ -987,6 +1042,34 @@ exports.examples = [ ); }, }, + { + title: 'Nested content', + render: function() { + // iOS-only because it relies on inline views being able to size to content. + // Android's implementation requires that a width and height be specified + // on the inline view. + return ( + + This text has a view + + which has + + another text inside. + + + And moreover, it has another view + + + with another text inside! + + + + + Because we need to go deeper. + + ); + }, + }, { title: 'Dynamic Font Size Adjustment', render: function(): React.Element {