From 817948e0aa25fa55c26c338384af236bf8ed0027 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 21 Feb 2023 15:03:14 -0800 Subject: [PATCH] Add "Text Styles" TextInput Example Summary: This adds a series of examples for TextInput styles to screenshot test, specifically the ones which are projected to Android as spans. This will be used in refactoring to verify we do not change visual output. Changelog: [Internal][Added] - Add "Text Styles" TextInput Example Reviewed By: cortinico Differential Revision: D43158004 fbshipit-source-id: adaecf0e37941e66e280db282e2631a95b08b27a --- .../rn-tester/js/components/RNTesterButton.js | 3 +- .../TextInput/TextInputSharedExamples.js | 267 +++++++++++++++++- 2 files changed, 258 insertions(+), 12 deletions(-) diff --git a/packages/rn-tester/js/components/RNTesterButton.js b/packages/rn-tester/js/components/RNTesterButton.js index 0e5acdf2946..0bff3bf6da6 100644 --- a/packages/rn-tester/js/components/RNTesterButton.js +++ b/packages/rn-tester/js/components/RNTesterButton.js @@ -18,6 +18,7 @@ import type {PressEvent} from 'react-native/Libraries/Types/CoreEventTypes'; type Props = $ReadOnly<{| testID?: string, + textTestID?: string, children?: React.Node, onPress?: ?(event: PressEvent) => mixed, |}>; @@ -30,7 +31,7 @@ class RNTesterButton extends React.Component { onPress={this.props.onPress} style={styles.button} underlayColor="grey"> - {this.props.children} + {this.props.children} ); } diff --git a/packages/rn-tester/js/examples/TextInput/TextInputSharedExamples.js b/packages/rn-tester/js/examples/TextInput/TextInputSharedExamples.js index e5b1c4f580b..353379409ff 100644 --- a/packages/rn-tester/js/examples/TextInput/TextInputSharedExamples.js +++ b/packages/rn-tester/js/examples/TextInput/TextInputSharedExamples.js @@ -10,17 +10,20 @@ 'use strict'; -const React = require('react'); - -const { +import * as React from 'react'; +import {useContext, useState} from 'react'; +import { Button, Platform, Text, TextInput, View, StyleSheet, -} = require('react-native'); +} from 'react-native'; +import type {TextStyle} from 'react-native/Libraries/StyleSheet/StyleSheet'; +import RNTesterButton from '../../components/RNTesterButton'; +import {RNTesterThemeContext} from '../../components/RNTesterTheme'; import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; const styles = StyleSheet.create({ @@ -49,9 +52,10 @@ const styles = StyleSheet.create({ }, label: { width: 115, - alignItems: 'flex-end', + textAlign: 'right', marginRight: 10, paddingTop: 2, + fontSize: 12, }, inputContainer: { flex: 1, @@ -80,15 +84,20 @@ const styles = StyleSheet.create({ fontSize: 13, padding: 4, }, + screenshotArea: { + position: 'absolute', + top: -5, + left: 120, + right: -5, + bottom: -5, + }, }); class WithLabel extends React.Component<$FlowFixMeProps> { render(): React.Node { return ( - - {this.props.label} - + {this.props.label} {this.props.children} ); @@ -592,6 +601,239 @@ function UncontrolledExample() { ); } +const TextStylesExample = React.memo(() => { + const theme = useContext(RNTesterThemeContext); + + return ( + + ); +}); + +type TextStylesContainerProps = { + examples: $ReadOnlyArray<{ + name: string, + textStyles: $ReadOnlyArray, + multiline?: boolean, + }>, +}; + +function TextStylesContainer({examples}: TextStylesContainerProps) { + const [offset, setOffset] = useState(0); + + const MAX_CYCLES = 6; + + return ( + + setOffset((offset + 1) % MAX_CYCLES)}> + Cycle {offset + 1}/{MAX_CYCLES} + + + + {examples.map(({name, multiline, textStyles}) => ( + + {multiline ? ( + + ) : ( + + )} + + ))} + + + ); +} + +type StyledTextInputProps = { + name: string, + textStyles: $ReadOnlyArray, + styleOffset: number, +}; + +function StyledTextInput({ + name, + textStyles, + styleOffset, +}: StyledTextInputProps) { + return ( + + He + ll + + o, + + + Wo + rl + d! + + ); +} + +function MultilineStyledTextInput({ + name, + textStyles, + styleOffset, +}: StyledTextInputProps) { + return ( + + Hel{'\n'} + + lo {'\n'} + + + Wor{'\n'} + + ld! + + ); +} + module.exports = ([ { title: 'Auto-focus', @@ -867,8 +1109,11 @@ module.exports = ([ { title: 'Uncontrolled component with layout changes', name: 'uncontrolledComponent', - render: function (): React.Node { - return ; - }, + render: () => , + }, + { + title: 'Text styles', + name: 'textStyles', + render: () => , }, ]: Array);