/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {
RNTesterModule,
RNTesterModuleExample,
} from '../../types/RNTesterTypes';
import type {KeyboardTypeOptions} from 'react-native';
import RNTesterText from '../../components/RNTesterText';
import ExampleTextInput from './ExampleTextInput';
import TextInputSharedExamples from './TextInputSharedExamples';
import * as React from 'react';
import {useRef} from 'react';
import {
Alert,
Button,
InputAccessoryView,
StyleSheet,
Switch,
Text,
TextInput,
View,
} from 'react-native';
class WithLabel extends React.Component<$FlowFixMe> {
render(): React.Node {
return (
{this.props.label}
{this.props.children}
);
}
}
class TextInputAccessoryViewChangeTextExample extends React.Component<
{...},
{text: string},
> {
constructor(props: void | {...}) {
// $FlowFixMe[incompatible-type]
super(props);
this.state = {text: 'Placeholder Text'};
}
render(): React.Node {
const inputAccessoryViewID = 'inputAccessoryView1';
return (
Set InputAccessoryView with ID & reset text:
this.setState({text})}
value={this.state.text}
/>
);
}
}
class TextInputAccessoryViewChangeKeyboardExample extends React.Component<
{...},
{keyboardType: string, text: string},
> {
constructor(props: void | {...}) {
// $FlowFixMe[incompatible-type]
super(props);
this.state = {text: '', keyboardType: 'default'};
}
_switchKeyboard = () => {
this.setState({
keyboardType:
this.state.keyboardType === 'default' ? 'number-pad' : 'default',
});
};
render(): React.Node {
const inputAccessoryViewID = 'inputAccessoryView2';
return (
Set InputAccessoryView with ID & switch keyboard:
{/* $FlowFixMe[incompatible-use] */}
this.setState({text})}
value={this.state.text}
// $FlowFixMe[incompatible-type]
keyboardType={this.state.keyboardType}
returnKeyType="done"
/>
);
}
}
class TextInputAccessoryViewDefaultDoneButtonExample extends React.Component<
Readonly<{
keyboardType: KeyboardTypeOptions,
}>,
{text: string},
> {
constructor(props: void | Readonly<{keyboardType: KeyboardTypeOptions}>) {
// $FlowFixMe[incompatible-type]
super(props);
this.state = {text: ''};
}
render(): React.Node {
return (
this.setState({text})}
value={this.state.text}
keyboardType={this.props.keyboardType}
returnKeyType="done"
/>
);
}
}
class RewriteExampleKana extends React.Component<$FlowFixMe, any> {
constructor(props: any | void) {
super(props);
this.state = {text: ''};
}
render(): React.Node {
return (
{
this.setState({text: text.replace(/ひ/g, '日')});
}}
value={this.state.text}
/>
);
}
}
class SecureEntryExample extends React.Component<$FlowFixMe, any> {
constructor(props: any | void) {
super(props);
this.state = {
text: '',
password: '',
isSecureTextEntry: true,
};
}
render(): React.Node {
return (
this.setState({text})}
value={this.state.text}
/>
Current text is: {this.state.text}
this.setState({password: text})}
secureTextEntry={this.state.isSecureTextEntry}
value={this.state.password}
/>
{
this.setState({isSecureTextEntry: value});
}}
style={{marginLeft: 4}}
value={this.state.isSecureTextEntry}
/>
);
}
}
const TextInputWithFocusButton = () => {
const inputToFocusRef = useRef | null>(
null,
);
return (
);
};
function KeyboardShortcutsExample() {
return (
Single line:
Multiline:
);
}
const styles = StyleSheet.create({
multiline: {
height: 50,
},
multilinePlaceholderStyles: {
letterSpacing: 10,
lineHeight: 20,
textAlign: 'center',
},
multilineExpandable: {
height: 'auto',
maxHeight: 100,
},
multilineWithFontStyles: {
color: 'blue',
fontWeight: 'bold',
fontSize: 18,
fontFamily: 'Cochin',
height: 60,
},
singlelinePlaceholderStyles: {
letterSpacing: 10,
textAlign: 'center',
},
labelContainer: {
flexDirection: 'row',
marginVertical: 2,
},
label: {
width: 115,
alignItems: 'flex-end',
marginRight: 10,
paddingTop: 2,
},
rewriteContainer: {
flexDirection: 'row',
alignItems: 'center',
},
remainder: {
textAlign: 'right',
width: 24,
},
});
const textInputExamples: Array = [
...TextInputSharedExamples,
{
title: 'Live Re-Write (ひ -> 日)',
render: function (): React.Node {
return ;
},
},
{
title: 'Keyboard Input Accessory View',
render: function (): React.Node {
return (
);
},
},
{
title: "Default Input Accessory View with returnKeyType = 'done'",
render: function (): React.Node {
const keyboardTypesWithDoneButton = [
'number-pad',
'phone-pad',
'decimal-pad',
'ascii-capable-number-pad',
] as const;
const examples = keyboardTypesWithDoneButton.map(type => {
return (
);
});
return {examples};
},
},
{
title: 'Custom Input Accessory View Button Label',
render: function (): React.Node {
return (
);
},
},
{
title: 'Nested content and `value` property',
render: function (): React.Node {
return (
(first raw text node)
(internal raw text node)
(last raw text node)
(first raw text node)
(internal raw text node)
(last raw text node)
);
},
},
{
title: 'Keyboard appearance',
render: function (): React.Node {
const keyboardAppearance = ['default', 'light', 'dark'] as const;
const examples = keyboardAppearance.map(type => {
return (
);
});
return {examples};
},
},
{
title: 'Return key types',
render: function (): React.Node {
const returnKeyTypes = [
'default',
'go',
'google',
'join',
'next',
'route',
'search',
'send',
'yahoo',
'done',
'emergency-call',
] as const;
const examples = returnKeyTypes.map(type => {
return (
);
});
return {examples};
},
},
{
title: 'Enable return key automatically',
render: function (): React.Node {
return (
);
},
},
{
title: 'Secure text entry',
render: function (): React.Node {
return ;
},
},
{
title: 'Colored input text',
render: function (): React.Node {
return (
);
},
},
{
title: 'Colored highlight/cursor for text input',
render: function (): React.Node {
return (
);
},
},
{
title: 'Clear button mode',
render: function (): React.Node {
const clearButtonModes = [
'never',
'while-editing',
'unless-editing',
'always',
] as const;
const examples = clearButtonModes.map(mode => {
return (
);
});
return {examples};
},
},
{
title: 'Clear and select',
render: function (): React.Node {
return (
);
},
},
{
title: 'Multiline blur on submit',
render: function (): React.Node {
return (
Alert.alert('Alert', event.nativeEvent.text)
}
/>
);
},
},
{
title: 'Multiline',
render: function (): React.Node {
return (
);
},
},
{
title: 'Editable and Read only',
render: function (): React.Node {
return (
);
},
},
{
title: 'TextInput Intrinsic Size',
render: function (): React.Node {
return (
Singleline TextInput
Multiline TextInput
Multiline TextInput with flex
);
},
},
{
title: 'allowFontScaling attribute',
render: function (): React.Node {
return (
By default, text will respect Text Size accessibility setting on
iOS. It means that all font sizes will be increased or decreased
depending on the value of Text Size setting in{' '}
Settings.app - Display & Brightness - Text Size
);
},
},
{
title: 'maxFontSizeMultiplier attribute',
name: 'maxFontSizeMultiplier',
render(): React.Node {
return (
When allowFontScaling is enabled, you can use the
maxFontSizeMultiplier prop to set an upper limit on how much the
font size will be scaled.
);
},
},
{
title: 'Auto-expanding',
render: function (): React.Node {
return (
);
},
},
{
title: 'TextInput maxLength',
render: function (): React.Node {
return (
);
},
},
{
title: 'Text Auto Complete',
render: function (): React.Node {
return (
);
},
},
{
title: 'Text Content Type',
render: function (): React.Node {
return (
);
},
},
{
title: 'TextInput Placeholder Styles',
render: function (): React.Node {
return (
);
},
},
{
title: 'Line Break Strategy',
render: function (): React.Node {
const lineBreakStrategy = [
'none',
'standard',
'hangul-word',
'push-out',
] as const;
const textByCode = {
en: 'lineBreakStrategy lineBreakStrategy lineBreakStrategy lineBreakStrategy',
ko: '한글개행한글개행 한글개행한글개행 한글개행한글개행 한글개행한글개행 한글개행한글개행 한글개행한글개행',
ja: 'かいぎょう かいぎょう かいぎょう かいぎょう かいぎょう かいぎょう',
cn: '改行 改行 改行 改行 改行 改行 改行 改行 改行 改行 改行 改行',
};
return (
{lineBreakStrategy.map(strategy => {
return (
{`Strategy: ${strategy}`}
{Object.keys(textByCode).map(code => {
return (
{`[${code}]`}
);
})}
);
})}
);
},
},
{
title: 'iOS autoformatting behaviors',
render: function (): React.Node {
return (
);
},
},
{
title: 'Auto scroll cursor into view when focusing',
render: function (): React.Node {
return ;
},
},
{
title: 'Keyboard shortcuts',
render: function (): React.Node {
return ;
},
},
{
title: 'Line Break Mode',
render: function (): React.Node {
const lineBreakMode = [
'wordWrapping',
'char',
'clip',
'head',
'middle',
'tail',
] as const;
const textByCode = {
en: 'verylongtext-dummydummydummydummydummydummydummydummydummydummydummydummy',
ko: '한글개행한글개행-한글개행한글개행한글개행한글개행한글개행한글개행한글개행한글개행한글개행한글개행',
};
return (
{lineBreakMode.map(strategy => {
return (
{`Mode: ${strategy}`}
{Object.keys(textByCode).map(code => {
return (
{`[${code}]`}
);
})}
);
})}
);
},
},
{
title: 'Accessibility',
render: function (): React.Node {
return (
);
},
},
];
module.exports = {
displayName: undefined as ?string,
title: 'TextInput',
documentationURL: 'https://reactnative.dev/docs/TextInput',
category: 'Basic',
description: 'Single and multi-line text inputs.',
examples: textInputExamples,
} as RNTesterModule;